From fe33824026ba8f2ab9c50a64274d5068a4686830 Mon Sep 17 00:00:00 2001 From: spectre Date: Sat, 9 Dec 2023 12:38:12 +0100 Subject: [PATCH] removed redundancy --- aoc2023/src/day09.rs | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/aoc2023/src/day09.rs b/aoc2023/src/day09.rs index 447fdc3..1cb47d0 100644 --- a/aoc2023/src/day09.rs +++ b/aoc2023/src/day09.rs @@ -10,22 +10,22 @@ impl Solution for Day09 { ).collect::>>(); Answer::from( - lines.iter().map(solve1).sum::() + lines.iter().map(solve).sum::() ) } fn part_2(&self, input: &str) -> Answer { let lines = input.split("\n") - .map(|l| l.split_whitespace().map(|x| x.parse::().unwrap()).collect() + .map(|l| l.split_whitespace().map(|x| x.parse::().unwrap()).rev().collect() ).collect::>>(); Answer::from( - lines.iter().map(solve2).sum::() + lines.iter().map(solve).sum::() ) } } -fn solve1(seq: &Vec) -> i64 { +fn solve(seq: &Vec) -> i64 { if seq.iter().all(|x| *x==0) { return 0 } @@ -34,18 +34,5 @@ fn solve1(seq: &Vec) -> i64 { .map(|(a, b)| b - a ) .collect(); - return seq.last().unwrap() + solve1(&diffs) + return seq.last().unwrap() + solve(&diffs) } - -fn solve2(seq: &Vec) -> i64 { - if seq.iter().all(|x| *x==0) { - return 0 - } - - let diffs = zip(seq, seq.iter().skip(1)) - .map(|(a, b)| b - a ) - .collect(); - - return seq.first().unwrap() - solve2(&diffs) -} -