mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 13:03:19 -09:00
init day2
This commit is contained in:
parent
13cd1bfb64
commit
38fc7ef94c
65
src/day2.rs
Executable file
65
src/day2.rs
Executable file
@ -0,0 +1,65 @@
|
|||||||
|
pub fn run(lines: Vec<String>) {
|
||||||
|
let mut h1 = 0;
|
||||||
|
let mut d1 = 0;
|
||||||
|
|
||||||
|
let mut aim = 0;
|
||||||
|
let mut d2 = 0;
|
||||||
|
let mut h2 = 0;
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
let parts = line.split(" ");
|
||||||
|
let command = parts.clone().nth(0).unwrap();
|
||||||
|
let num: i32 = parts.clone().nth(1).unwrap().parse().unwrap();
|
||||||
|
match command {
|
||||||
|
"forward" => {
|
||||||
|
h1 += num;
|
||||||
|
h2 += num;
|
||||||
|
d2 += aim * num;
|
||||||
|
},
|
||||||
|
"up" => {
|
||||||
|
d1 -= num;
|
||||||
|
aim -= num;
|
||||||
|
},
|
||||||
|
"down" => {
|
||||||
|
d1 += num;
|
||||||
|
aim += num;
|
||||||
|
},
|
||||||
|
_ => println!("unknown command: '{}'", command)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("{}, {}", h1 * d1, h2 * d2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod part1 {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_sample() {
|
||||||
|
let input = include_str!("../inputs/2_test.txt");
|
||||||
|
assert_eq!(part_1_solution(input), 150);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run() {
|
||||||
|
let input = include_str!("../inputs/2.txt");
|
||||||
|
assert_eq!(part_1_solution(input), 1660158);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod part2 {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_sample() {
|
||||||
|
let input = include_str!("../inputs/2_test.txt");
|
||||||
|
assert_eq!(part_2_solution(input), 900);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run() {
|
||||||
|
let input = include_str!("../inputs/2.txt");
|
||||||
|
assert_eq!(part_2_solution(input), 1604592846);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user