From: Grégory Burri Date: Mon, 2 Dec 2019 07:39:41 +0000 (+0100) Subject: Day 02 X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=bfe75315ed99486f5d34d2848699ea91eb663adb;p=advent_of_code_2019.git Day 02 --- diff --git a/data/day02.input b/data/day02.input new file mode 100644 index 0000000..bf38f88 --- /dev/null +++ b/data/day02.input @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,1,10,19,2,9,19,23,1,9,23,27,2,27,9,31,1,31,5,35,2,35,9,39,1,39,10,43,2,43,13,47,1,47,6,51,2,51,10,55,1,9,55,59,2,6,59,63,1,63,6,67,1,67,10,71,1,71,10,75,2,9,75,79,1,5,79,83,2,9,83,87,1,87,9,91,2,91,13,95,1,95,9,99,1,99,6,103,2,103,6,107,1,107,5,111,1,13,111,115,2,115,6,119,1,119,5,123,1,2,123,127,1,6,127,0,99,2,14,0,0 \ No newline at end of file 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) }