From f10f5d05d2cbb744cf9f058b7c8d4c06da91b306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Wed, 7 Apr 2021 15:29:32 +0200 Subject: [PATCH] Solve day 1 part 2 --- src/days/day01.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/days/day01.rs b/src/days/day01.rs index b323016..ea28107 100644 --- a/src/days/day01.rs +++ b/src/days/day01.rs @@ -11,8 +11,25 @@ fn calc_solution1(input: &Vec) -> i32 { panic!("No solution"); } +fn calc_solution2(input: &Vec) -> i32 { + for i in 0..input.len() { + for j in (i + 1)..input.len() { + for k in (j + 1)..input.len() { + if (input[i] + input[j] + input[k]) == 2020 { + return input[i] * input[j] * input[k]; + } + } + } + } + panic!("No solution"); +} + pub fn get_solution() { let contents = fs::read_to_string("data/day01.dat").expect("Could not read data"); let input: Vec = contents.lines().flat_map(str::parse).collect(); - println!("Day 1, part 1: {}", calc_solution1(&input)); + println!( + "Day 1, part 1: {}, part 2: {}", + calc_solution1(&input), + calc_solution2(&input) + ); } -- 2.44.0