-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from paulcacheux/day9-2023
Day9 2023
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
0 3 6 9 12 15 | ||
1 3 6 10 15 21 | ||
10 13 16 21 30 45 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,5 +38,10 @@ | |
"day": 8, | ||
"part1": "13771", | ||
"part2": "13129439557681" | ||
}, | ||
{ | ||
"day": 9, | ||
"part1": "1702218515", | ||
"part2": "925" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use crate::aoc2023::Aoc2023; | ||
use crate::traits::days::Day9; | ||
use crate::traits::ParseInput; | ||
use crate::traits::Solution; | ||
|
||
impl ParseInput<Day9> for Aoc2023 { | ||
type Parsed = Vec<Vec<i32>>; | ||
|
||
fn parse_input(input: &str) -> Self::Parsed { | ||
input | ||
.lines() | ||
.map(|line| { | ||
line.split_ascii_whitespace() | ||
.map(|value| value.parse().unwrap()) | ||
.collect() | ||
}) | ||
.collect() | ||
} | ||
} | ||
|
||
impl Solution<Day9> for Aoc2023 { | ||
type Part1Output = i32; | ||
type Part2Output = i32; | ||
|
||
fn part1(input: &Vec<Vec<i32>>) -> i32 { | ||
solve(input, false) | ||
} | ||
|
||
fn part2(input: &Vec<Vec<i32>>) -> i32 { | ||
solve(input, true) | ||
} | ||
} | ||
|
||
fn solve(input: &Vec<Vec<i32>>, part2: bool) -> 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( | ||
*(if part2 { | ||
current.first() | ||
} else { | ||
current.last() | ||
}) | ||
.unwrap(), | ||
); | ||
|
||
let mut next_line: Vec<i32> = Vec::with_capacity(current.len() - 1); | ||
for [a, b] in current.array_windows() { | ||
let delta = b - a; | ||
next_line.push(delta); | ||
} | ||
current = next_line; | ||
} | ||
|
||
sum += history.into_iter().rev().fold( | ||
0, | ||
|acc, value| { | ||
if part2 { | ||
value - acc | ||
} else { | ||
value + acc | ||
} | ||
}, | ||
); | ||
} | ||
sum | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters