From f38f4206f4d2959594926b1ae13c1a4a9600dee7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9gory=20Burri?= Date: Mon, 2 Dec 2019 08:39:41 +0100 Subject: [PATCH] Day 02 --- src/common.rs | 4 ++-- src/day01.rs | 4 ++-- src/day02.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 15 ++++++++++---- 4 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 src/day02.rs diff --git a/src/common.rs b/src/common.rs index 24c0c32..e87a0a2 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,6 +1,6 @@ use std::fs; use std::path::Path; -pub fn read_list_of_numbers>(file: P) -> Vec { - fs::read_to_string(file).unwrap().lines().map(|line| line.parse::().unwrap()).collect() +pub fn read_list_of_numbers>(file: P, sep: &str) -> Vec { + fs::read_to_string(file).unwrap().split(sep).map(|line| line.parse::().unwrap()).collect() } \ No newline at end of file diff --git a/src/day01.rs b/src/day01.rs index d62a298..ca1d470 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -31,13 +31,13 @@ mod tests { assert_eq!(mass_to_fuel(12), 2); assert_eq!(mass_to_fuel(14), 2); assert_eq!(mass_to_fuel(1969), 654); - assert_eq!(mass_to_fuel(100756), 33583); + assert_eq!(mass_to_fuel(100_756), 33583); } #[test] fn simple_cases_2() { assert_eq!(mass_to_fuel_2(14), 2); assert_eq!(mass_to_fuel_2(1969), 966); - assert_eq!(mass_to_fuel_2(100756), 50346); + assert_eq!(mass_to_fuel_2(100_756), 50346); } } \ No newline at end of file diff --git a/src/day02.rs b/src/day02.rs new file mode 100644 index 0000000..76bc33e --- /dev/null +++ b/src/day02.rs @@ -0,0 +1,57 @@ +pub fn execute_op_code_with_state_fixed(code : &mut [i32]) -> i32 { + code[1] = 12; + code[2] = 2; + execute_op_code(code) +} + +fn execute_op_code(code : &mut [i32]) -> i32 { + let mut cursor = 0; + loop { + match code[cursor] { + 1 => code[code[cursor + 3] as usize] = code[code[cursor + 1] as usize] + code[code[cursor + 2] as usize], + 2 => code[code[cursor + 3] as usize] = code[code[cursor + 1] as usize] * code[code[cursor + 2] as usize], + 99 => return code[0], + _ => panic!("Unkown code: {}", code[cursor]) + } + cursor += 4; + } +} + +pub fn find_noun_and_verb(code : &[i32]) -> i32 { + loop { + for noun in 0..=99 { + for verb in 0..=99 { + let mut code_copy = Vec::from(code); + code_copy[1] = noun; + code_copy[2] = verb; + if execute_op_code(&mut code_copy) == 19_690_720 { + return 100 * noun + verb + } + } + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn simple_cases() { + let mut c1 = [1, 0, 0, 0, 99]; + execute_op_code(&mut c1); + assert_eq!(c1[0], 2); + + let mut c2 = [2, 3, 0, 3, 99]; + execute_op_code(&mut c2); + assert_eq!(c2[3], 6); + + let mut c3 = [2, 4, 4, 5, 99, 0]; + execute_op_code(&mut c3); + assert_eq!(c3[5], 9801); + + let mut c4 = [1, 1, 1, 4, 99, 5, 6, 0, 99]; + execute_op_code(&mut c4); + assert_eq!(c4[0], 30); + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 5592192..83918d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,20 @@ use std::env; use std::time::Instant; mod day01; +mod day02; mod common; fn day01() -> String { - let masses = common::read_list_of_numbers("data/day01.input"); + let masses = common::read_list_of_numbers("data/day01.input", "\n"); format!("part1: {}, part2: {}", day01::sum_mass_to_fuel(&masses), day01::sum_mass_to_fuel_2(&masses)) } -fn do_day(days: &Vec String>, day: usize) { +fn day02() -> String { + let code = common::read_list_of_numbers("data/day02.input", ","); + format!("part1: {}, part2: {}", day02::execute_op_code_with_state_fixed(&mut Vec::from(&code[..])), day02::find_noun_and_verb(&code)) +} + +fn do_day(days: &[fn() -> String], day: usize) { let now = Instant::now(); println!("Result of day {}: {} (time: {} μs)", day, days[day - 1](), now.elapsed().as_micros()); } @@ -18,13 +24,14 @@ fn main() { println!("https://adventofcode.com/2019"); let days: Vec String> = vec!( - day01 + day01, + day02 ); let args: Vec = env::args().skip(1).collect(); // No argument -> execute all day problems. - if args.len() == 0 { + if args.is_empty() { for i in 1..=days.len() { do_day(&days, i) } -- 2.45.2