--- /dev/null
+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
use std::fs;\r
use std::path::Path;\r
\r
-pub fn read_list_of_numbers<P: AsRef<Path>>(file: P) -> Vec<i32> {\r
- fs::read_to_string(file).unwrap().lines().map(|line| line.parse::<i32>().unwrap()).collect()\r
+pub fn read_list_of_numbers<P: AsRef<Path>>(file: P, sep: &str) -> Vec<i32> {\r
+ fs::read_to_string(file).unwrap().split(sep).map(|line| line.parse::<i32>().unwrap()).collect()\r
}
\ No newline at end of file
assert_eq!(mass_to_fuel(12), 2);\r
assert_eq!(mass_to_fuel(14), 2);\r
assert_eq!(mass_to_fuel(1969), 654);\r
- assert_eq!(mass_to_fuel(100756), 33583);\r
+ assert_eq!(mass_to_fuel(100_756), 33583);\r
}\r
\r
#[test]\r
fn simple_cases_2() {\r
assert_eq!(mass_to_fuel_2(14), 2);\r
assert_eq!(mass_to_fuel_2(1969), 966);\r
- assert_eq!(mass_to_fuel_2(100756), 50346);\r
+ assert_eq!(mass_to_fuel_2(100_756), 50346);\r
}\r
}
\ No newline at end of file
--- /dev/null
+pub fn execute_op_code_with_state_fixed(code : &mut [i32]) -> i32 {\r
+ code[1] = 12;\r
+ code[2] = 2;\r
+ execute_op_code(code)\r
+}\r
+\r
+fn execute_op_code(code : &mut [i32]) -> i32 {\r
+ let mut cursor = 0;\r
+ loop {\r
+ match code[cursor] {\r
+ 1 => code[code[cursor + 3] as usize] = code[code[cursor + 1] as usize] + code[code[cursor + 2] as usize],\r
+ 2 => code[code[cursor + 3] as usize] = code[code[cursor + 1] as usize] * code[code[cursor + 2] as usize],\r
+ 99 => return code[0],\r
+ _ => panic!("Unkown code: {}", code[cursor])\r
+ }\r
+ cursor += 4;\r
+ }\r
+}\r
+\r
+pub fn find_noun_and_verb(code : &[i32]) -> i32 {\r
+ loop {\r
+ for noun in 0..=99 {\r
+ for verb in 0..=99 {\r
+ let mut code_copy = Vec::from(code);\r
+ code_copy[1] = noun;\r
+ code_copy[2] = verb;\r
+ if execute_op_code(&mut code_copy) == 19_690_720 {\r
+ return 100 * noun + verb\r
+ }\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+#[cfg(test)]\r
+mod tests {\r
+ use super::*;\r
+\r
+ #[test]\r
+ fn simple_cases() {\r
+ let mut c1 = [1, 0, 0, 0, 99];\r
+ execute_op_code(&mut c1);\r
+ assert_eq!(c1[0], 2);\r
+\r
+ let mut c2 = [2, 3, 0, 3, 99];\r
+ execute_op_code(&mut c2);\r
+ assert_eq!(c2[3], 6);\r
+\r
+ let mut c3 = [2, 4, 4, 5, 99, 0];\r
+ execute_op_code(&mut c3);\r
+ assert_eq!(c3[5], 9801);\r
+\r
+ let mut c4 = [1, 1, 1, 4, 99, 5, 6, 0, 99];\r
+ execute_op_code(&mut c4);\r
+ assert_eq!(c4[0], 30);\r
+ }\r
+}
\ No newline at end of file
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<fn() -> 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());
}
println!("https://adventofcode.com/2019");
let days: Vec<fn() -> String> = vec!(
- day01
+ day01,
+ day02
);
let args: Vec<String> = 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)
}