This commit is contained in:
parent
7187fad1a1
commit
2b884fab65
1000
input/day02
Normal file
1000
input/day02
Normal file
File diff suppressed because it is too large
Load diff
102
src/day02.rs
Normal file
102
src/day02.rs
Normal file
|
@ -0,0 +1,102 @@
|
|||
use crate::Day;
|
||||
|
||||
pub struct Day02(Vec<Instruction>);
|
||||
|
||||
enum Instruction {
|
||||
Forward(i32),
|
||||
Down(i32),
|
||||
Up(i32),
|
||||
}
|
||||
|
||||
impl Day for Day02 {
|
||||
fn init(content: String) -> Self where Self: Sized {
|
||||
let lines = content.split("\n").map(str_to_instr).collect::<Vec<Instruction>>();
|
||||
Day02(lines)
|
||||
}
|
||||
|
||||
fn part1(&self) -> String {
|
||||
let mut depth = 0;
|
||||
let mut fw = 0;
|
||||
for i in &self.0 {
|
||||
match i {
|
||||
Instruction::Forward(n) => fw += n,
|
||||
Instruction::Down(n) => depth += n,
|
||||
Instruction::Up(n) => depth -= n,
|
||||
}
|
||||
}
|
||||
format!("{}", depth * fw)
|
||||
}
|
||||
|
||||
fn part2(&self) -> String {
|
||||
let mut depth = 0;
|
||||
let mut fw = 0;
|
||||
let mut aim = 0;
|
||||
for i in &self.0 {
|
||||
match i {
|
||||
Instruction::Forward(n) => {
|
||||
fw += n;
|
||||
depth += aim * n;
|
||||
}
|
||||
Instruction::Down(n) => {
|
||||
aim += n;
|
||||
}
|
||||
Instruction::Up(n) => {
|
||||
aim -= n;
|
||||
}
|
||||
}
|
||||
}
|
||||
format!("{}", depth * fw)
|
||||
}
|
||||
}
|
||||
|
||||
fn str_to_instr(s: &str) -> Instruction {
|
||||
let v = s.split(' ').collect::<Vec<&str>>();
|
||||
let name = v[0];
|
||||
let height = v[1].parse().expect("This should be an int :p");
|
||||
match name {
|
||||
"forward" => Instruction::Forward(height),
|
||||
"up" => Instruction::Up(height),
|
||||
"down" => Instruction::Down(height),
|
||||
_ => panic!("How did this happen???")
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fs::read_to_string;
|
||||
use crate::day02::Day02;
|
||||
use crate::day::Day;
|
||||
|
||||
const D: &str = r"forward 5
|
||||
down 5
|
||||
forward 8
|
||||
up 3
|
||||
down 8
|
||||
forward 2";
|
||||
|
||||
#[test]
|
||||
fn part1_test() {
|
||||
let d2 = Day02::init(String::from(D));
|
||||
assert_eq!("150", d2.part1())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_test() {
|
||||
let d2 = Day02::init(String::from(D));
|
||||
assert_eq!("900", d2.part2())
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn part1_real() {
|
||||
let f = read_to_string("./input/day02").expect("Could not load input");
|
||||
let d = Day02::init(f);
|
||||
assert_eq!("1524750", d.part1())
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn part2_real() {
|
||||
let f = read_to_string("./input/day02").expect("Could not load input");
|
||||
let d = Day02::init(f);
|
||||
assert_eq!("1592426537", d.part2())
|
||||
}
|
||||
}
|
|
@ -2,10 +2,12 @@ use std::fs::read_to_string;
|
|||
use chrono::Datelike;
|
||||
use clap::{App, Arg};
|
||||
use crate::day01::Day01;
|
||||
use crate::day02::Day02;
|
||||
use crate::day::Day;
|
||||
|
||||
mod day;
|
||||
mod day01;
|
||||
mod day02;
|
||||
|
||||
fn load_input(day: &str) -> String {
|
||||
read_to_string(format!("./input/day{}", day)).expect("Could not load input")
|
||||
|
@ -40,7 +42,7 @@ fn main() {
|
|||
.multiple(true))
|
||||
.get_matches();
|
||||
|
||||
let days: Vec<Box<dyn Day>> = vec![Box::new(Day01::init(load_input("01")))];
|
||||
let days: Vec<Box<dyn Day>> = vec![Box::new(Day01::init(load_input("01"))), Box::new(Day02::init(load_input("02")))];
|
||||
|
||||
let _verbosity = matches.occurrences_of("v");
|
||||
if matches.is_present("all") {
|
||||
|
|
Loading…
Reference in a new issue