From 3645aedf29c25b314a4882a213d607563f57eb51 Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Sat, 9 Dec 2023 09:28:57 +0100 Subject: [PATCH 1/7] day 9 part1 --- inputs/2023/day9_test.txt | 3 ++ src/aoc2023/day9.rs | 58 +++++++++++++++++++++++++++++++++++++++ src/aoc2023/mod.rs | 2 ++ 3 files changed, 63 insertions(+) create mode 100644 inputs/2023/day9_test.txt create mode 100644 src/aoc2023/day9.rs diff --git a/inputs/2023/day9_test.txt b/inputs/2023/day9_test.txt new file mode 100644 index 0000000..70c5595 --- /dev/null +++ b/inputs/2023/day9_test.txt @@ -0,0 +1,3 @@ +0 3 6 9 12 15 +1 3 6 10 15 21 +10 13 16 21 30 45 \ No newline at end of file diff --git a/src/aoc2023/day9.rs b/src/aoc2023/day9.rs new file mode 100644 index 0000000..c429887 --- /dev/null +++ b/src/aoc2023/day9.rs @@ -0,0 +1,58 @@ +use crate::aoc2023::Aoc2023; +use crate::traits::days::Day9; +use crate::traits::ParseInput; +use crate::traits::Solution; + +impl ParseInput for Aoc2023 { + type Parsed = Vec>; + + fn parse_input(input: &str) -> Self::Parsed { + input + .lines() + .map(|line| { + line.split_ascii_whitespace() + .map(|value| value.parse().unwrap()) + .collect() + }) + .collect() + } +} + +impl Solution for Aoc2023 { + type Part1Output = i32; + type Part2Output = u32; + + fn part1(input: &Vec>) -> i32 { + let mut sum = 0; + for line in input { + let mut history = Vec::new(); + let mut current = line.clone(); + + while !current.iter().all(|&v| v == 0) { + history.push(current.clone()); + let mut next_line: Vec = Vec::with_capacity(current.len() - 1); + for [a, b] in current.array_windows() { + let delta = b - a; + next_line.push(delta); + } + current = next_line; + } + + for i in (0..history.len()).rev() { + let under_value = if i + 1 < history.len() { + *history[i+1].last().unwrap() + } else { + 0 + }; + let new_value = history[i].last().unwrap() + under_value; + history[i].push(new_value); + } + sum += history[0].last().unwrap(); + } + sum + } + + fn part2(_input: &Vec>) -> u32 { + todo!() + } +} diff --git a/src/aoc2023/mod.rs b/src/aoc2023/mod.rs index 192a264..21b4018 100644 --- a/src/aoc2023/mod.rs +++ b/src/aoc2023/mod.rs @@ -11,6 +11,7 @@ pub mod day5; pub mod day6; pub mod day7; pub mod day8; +pub mod day9; pub fn run_solution_for_day(day: u32, input: &str, results: Option) -> Option { let r = results @@ -26,6 +27,7 @@ pub fn run_solution_for_day(day: u32, input: &str, results: Option) -> 6 => run::(input, r), 7 => run::(input, r), 8 => run::(input, r), + 9 => run::(input, r), _ => return None, }; Some(elapsed) From 77cd6e28fd677a42b4b59748fecc2bbf3d0f4873 Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Sat, 9 Dec 2023 09:32:55 +0100 Subject: [PATCH 2/7] and part2 --- src/aoc2023/day9.rs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/src/aoc2023/day9.rs b/src/aoc2023/day9.rs index c429887..5286f1d 100644 --- a/src/aoc2023/day9.rs +++ b/src/aoc2023/day9.rs @@ -20,7 +20,7 @@ impl ParseInput for Aoc2023 { impl Solution for Aoc2023 { type Part1Output = i32; - type Part2Output = u32; + type Part2Output = i32; fn part1(input: &Vec>) -> i32 { let mut sum = 0; @@ -52,7 +52,36 @@ impl Solution for Aoc2023 { sum } - fn part2(_input: &Vec>) -> u32 { - todo!() + fn part2(input: &Vec>) -> i32 { + let mut sum = 0; + for line in input { + let mut history = Vec::new(); + let mut current = line.clone(); + + while !current.iter().all(|&v| v == 0) { + history.push(current.clone()); + let mut next_line: Vec = Vec::with_capacity(current.len() - 1); + for [a, b] in current.array_windows() { + let delta = b - a; + next_line.push(delta); + } + current = next_line; + } + + for i in (0..history.len()).rev() { + let under_value = if i + 1 < history.len() { + *history[i+1].first().unwrap() + } else { + 0 + }; + let new_value = history[i].first().unwrap() - under_value; + + let mut new_line = vec![new_value]; + new_line.extend_from_slice(&history[i]); + history[i] = new_line; + } + sum += history[0].first().unwrap(); + } + sum } } From 2a903ee895dd31e29e2f5ebe3aebdd7ba6c82cae Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Sat, 9 Dec 2023 09:33:09 +0100 Subject: [PATCH 3/7] fmt --- src/aoc2023/day9.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aoc2023/day9.rs b/src/aoc2023/day9.rs index 5286f1d..06c1f5e 100644 --- a/src/aoc2023/day9.rs +++ b/src/aoc2023/day9.rs @@ -40,7 +40,7 @@ impl Solution for Aoc2023 { for i in (0..history.len()).rev() { let under_value = if i + 1 < history.len() { - *history[i+1].last().unwrap() + *history[i + 1].last().unwrap() } else { 0 }; @@ -70,7 +70,7 @@ impl Solution for Aoc2023 { for i in (0..history.len()).rev() { let under_value = if i + 1 < history.len() { - *history[i+1].first().unwrap() + *history[i + 1].first().unwrap() } else { 0 }; From 3a49cfa7ff5eccba254b45885f3ab2d4e07a706b Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Sat, 9 Dec 2023 09:38:52 +0100 Subject: [PATCH 4/7] add results --- results/2023.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/results/2023.json b/results/2023.json index 72dbab8..f8813db 100644 --- a/results/2023.json +++ b/results/2023.json @@ -38,5 +38,10 @@ "day": 8, "part1": "13771", "part2": "13129439557681" + }, + { + "day": 9, + "part1": "1702218515", + "part2": "925" } ] \ No newline at end of file From f257690afef8fab77d9dd555677240faa7f7fc12 Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Sat, 9 Dec 2023 09:40:43 +0100 Subject: [PATCH 5/7] small opt --- src/aoc2023/day9.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/aoc2023/day9.rs b/src/aoc2023/day9.rs index 06c1f5e..2263e5f 100644 --- a/src/aoc2023/day9.rs +++ b/src/aoc2023/day9.rs @@ -29,7 +29,7 @@ impl Solution for Aoc2023 { let mut current = line.clone(); while !current.iter().all(|&v| v == 0) { - history.push(current.clone()); + history.push(*current.last().unwrap()); let mut next_line: Vec = Vec::with_capacity(current.len() - 1); for [a, b] in current.array_windows() { let delta = b - a; @@ -40,14 +40,14 @@ impl Solution for Aoc2023 { for i in (0..history.len()).rev() { let under_value = if i + 1 < history.len() { - *history[i + 1].last().unwrap() + history[i + 1] } else { 0 }; - let new_value = history[i].last().unwrap() + under_value; - history[i].push(new_value); + let new_value = history[i] + under_value; + history[i] = new_value; } - sum += history[0].last().unwrap(); + sum += history[0]; } sum } @@ -59,7 +59,7 @@ impl Solution for Aoc2023 { let mut current = line.clone(); while !current.iter().all(|&v| v == 0) { - history.push(current.clone()); + history.push(*current.first().unwrap()); let mut next_line: Vec = Vec::with_capacity(current.len() - 1); for [a, b] in current.array_windows() { let delta = b - a; @@ -70,17 +70,14 @@ impl Solution for Aoc2023 { for i in (0..history.len()).rev() { let under_value = if i + 1 < history.len() { - *history[i + 1].first().unwrap() + history[i + 1] } else { 0 }; - let new_value = history[i].first().unwrap() - under_value; - - let mut new_line = vec![new_value]; - new_line.extend_from_slice(&history[i]); - history[i] = new_line; + let new_value = history[i] - under_value; + history[i] = new_value; } - sum += history[0].first().unwrap(); + sum += history[0]; } sum } From a35d26f1771477e25c805f28185d4c91e76d7951 Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Sat, 9 Dec 2023 09:44:11 +0100 Subject: [PATCH 6/7] common solve code --- src/aoc2023/day9.rs | 88 ++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 49 deletions(-) diff --git a/src/aoc2023/day9.rs b/src/aoc2023/day9.rs index 2263e5f..472b930 100644 --- a/src/aoc2023/day9.rs +++ b/src/aoc2023/day9.rs @@ -23,62 +23,52 @@ impl Solution for Aoc2023 { type Part2Output = i32; fn part1(input: &Vec>) -> i32 { - let mut sum = 0; - for line in input { - let mut history = Vec::new(); - let mut current = line.clone(); - - while !current.iter().all(|&v| v == 0) { - history.push(*current.last().unwrap()); - let mut next_line: Vec = Vec::with_capacity(current.len() - 1); - for [a, b] in current.array_windows() { - let delta = b - a; - next_line.push(delta); - } - current = next_line; - } - - for i in (0..history.len()).rev() { - let under_value = if i + 1 < history.len() { - history[i + 1] - } else { - 0 - }; - let new_value = history[i] + under_value; - history[i] = new_value; - } - sum += history[0]; - } - sum + solve(input, false) } fn part2(input: &Vec>) -> i32 { - let mut sum = 0; - for line in input { - let mut history = Vec::new(); - let mut current = line.clone(); + solve(input, true) + } +} - while !current.iter().all(|&v| v == 0) { - history.push(*current.first().unwrap()); - let mut next_line: Vec = Vec::with_capacity(current.len() - 1); - for [a, b] in current.array_windows() { - let delta = b - a; - next_line.push(delta); - } - current = next_line; - } +fn solve(input: &Vec>, part2: bool) -> i32 { + let mut sum = 0; + for line in input { + let mut history = Vec::new(); + let mut current = line.clone(); - for i in (0..history.len()).rev() { - let under_value = if i + 1 < history.len() { - history[i + 1] + while !current.iter().all(|&v| v == 0) { + history.push( + *(if part2 { + current.first() } else { - 0 - }; - let new_value = history[i] - under_value; - history[i] = new_value; + current.last() + }) + .unwrap(), + ); + + let mut next_line: Vec = Vec::with_capacity(current.len() - 1); + for [a, b] in current.array_windows() { + let delta = b - a; + next_line.push(delta); } - sum += history[0]; + current = next_line; + } + + for i in (0..history.len()).rev() { + let under_value = if i + 1 < history.len() { + history[i + 1] + } else { + 0 + }; + + history[i] = if part2 { + history[i] - under_value + } else { + history[i] + under_value + }; } - sum + sum += history[0]; } + sum } From 7b76ba2a8a0fb1c27ae81d0c5b45e1630237b12f Mon Sep 17 00:00:00 2001 From: Paul Cacheux Date: Sat, 9 Dec 2023 09:48:10 +0100 Subject: [PATCH 7/7] cleanup --- src/aoc2023/day9.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/aoc2023/day9.rs b/src/aoc2023/day9.rs index 472b930..982d954 100644 --- a/src/aoc2023/day9.rs +++ b/src/aoc2023/day9.rs @@ -55,20 +55,16 @@ fn solve(input: &Vec>, part2: bool) -> i32 { current = next_line; } - for i in (0..history.len()).rev() { - let under_value = if i + 1 < history.len() { - history[i + 1] - } else { - 0 - }; - - history[i] = if part2 { - history[i] - under_value - } else { - history[i] + under_value - }; - } - sum += history[0]; + sum += history.into_iter().rev().fold( + 0, + |acc, value| { + if part2 { + value - acc + } else { + value + acc + } + }, + ); } sum }