summaryrefslogtreecommitdiffstats
path: root/src/bin
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2023-12-09 21:12:17 -0500
committerJesse Luehrs <doy@tozt.net>2023-12-09 21:12:17 -0500
commit9c091ebd551f620c09d518c0f2d5eb9dac8b09fd (patch)
tree806da53d33054faa3a9397614492d5373b745a00 /src/bin
parent97296bee681f3ab61313c02ad394fd73e46d2f0a (diff)
downloadadvent-of-code-9c091ebd551f620c09d518c0f2d5eb9dac8b09fd.tar.gz
advent-of-code-9c091ebd551f620c09d518c0f2d5eb9dac8b09fd.zip
day 9
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/2023/day9.rs49
-rw-r--r--src/bin/2023/main.rs2
2 files changed, 51 insertions, 0 deletions
diff --git a/src/bin/2023/day9.rs b/src/bin/2023/day9.rs
new file mode 100644
index 0000000..d04c209
--- /dev/null
+++ b/src/bin/2023/day9.rs
@@ -0,0 +1,49 @@
+#![allow(dead_code)]
+#![allow(unused_variables)]
+
+use advent_of_code::prelude::*;
+
+fn calculate_next(v: Vec<i64>) -> i64 {
+ if v.iter().all(|n| *n == 0) {
+ return 0;
+ }
+ calculate_next((1..v.len()).map(|i| v[i] - v[i - 1]).collect())
+ + v[v.len() - 1]
+}
+
+fn calculate_prev(v: Vec<i64>) -> i64 {
+ if v.iter().all(|n| *n == 0) {
+ return 0;
+ }
+ v[0] - calculate_prev((1..v.len()).map(|i| v[i] - v[i - 1]).collect())
+}
+
+pub fn parse(fh: File) -> Result<Vec<Vec<i64>>> {
+ Ok(parse::raw_lines(fh)
+ .map(|line| {
+ line.split_whitespace()
+ .map(|s| s.parse().unwrap())
+ .collect()
+ })
+ .collect())
+}
+
+pub fn part1(report: Vec<Vec<i64>>) -> Result<i64> {
+ Ok(report.into_iter().map(calculate_next).sum())
+}
+
+pub fn part2(report: Vec<Vec<i64>>) -> Result<i64> {
+ Ok(report.into_iter().map(calculate_prev).sum())
+}
+
+#[test]
+fn test() {
+ assert_eq!(
+ part1(parse(parse::data(2023, 9).unwrap()).unwrap()).unwrap(),
+ 0
+ );
+ assert_eq!(
+ part2(parse(parse::data(2023, 9).unwrap()).unwrap()).unwrap(),
+ 0
+ );
+}
diff --git a/src/bin/2023/main.rs b/src/bin/2023/main.rs
index 569886c..2a5c575 100644
--- a/src/bin/2023/main.rs
+++ b/src/bin/2023/main.rs
@@ -19,6 +19,7 @@ mod day5;
mod day6;
mod day7;
mod day8;
+mod day9;
// NEXT MOD
#[paw::main]
@@ -33,6 +34,7 @@ fn main(opt: Opt) -> Result<()> {
6 => advent_of_code::day!(2023, opt.day, opt.puzzle, day6),
7 => advent_of_code::day!(2023, opt.day, opt.puzzle, day7),
8 => advent_of_code::day!(2023, opt.day, opt.puzzle, day8),
+ 9 => advent_of_code::day!(2023, opt.day, opt.puzzle, day9),
// NEXT PART
_ => panic!("unknown day {}", opt.day),
}