Skip to content

Commit

Permalink
day9 tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
bricooke committed Dec 9, 2023
1 parent f1268b8 commit 9a12724
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions src/day9.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use itertools::Itertools;

fn find_next_value(line: Vec<i32>) -> i32 {
fn find_next_value(line: &Vec<i32>, front: bool) -> i32 {
if line.iter().all(|i| *i == 0) {
return 0;
}
Expand All @@ -11,45 +11,36 @@ fn find_next_value(line: Vec<i32>) -> i32 {
.map(|(a, b)| b - a)
.collect::<Vec<i32>>();

line.last().unwrap() + find_next_value(next_row)
}

fn find_prev_value(line: Vec<i32>) -> i32 {
if line.iter().all(|i| *i == 0) {
return 0;
if front {
line.first().unwrap() - find_next_value(&next_row, front)
} else {
line.last().unwrap() + find_next_value(&next_row, front)
}
}

let next_row = line
.iter()
.tuple_windows()
.map(|(a, b)| b - a)
.collect::<Vec<i32>>();

line.first().unwrap() - find_prev_value(next_row)
fn parse(input: &str) -> Vec<Vec<i32>> {
input
.lines()
.map(|line| {
line.split(" ")
.map(|v| v.parse::<i32>().unwrap())
.collect::<Vec<i32>>()
})
.collect::<Vec<_>>()
}

pub fn day9_part1(input: &str) -> String {
let lines = input.lines().map(|line| {
line.split(" ")
.map(|v| v.parse::<i32>().unwrap())
.collect::<Vec<i32>>()
});

lines
.map(|line| find_next_value(line))
parse(input)
.iter()
.map(|line| find_next_value(line, false))
.sum::<i32>()
.to_string()
}

pub fn day9_part2(input: &str) -> String {
let lines = input.lines().map(|line| {
line.split(" ")
.map(|v| v.parse::<i32>().unwrap())
.collect::<Vec<i32>>()
});

lines
.map(|line| find_prev_value(line))
parse(input)
.iter()
.map(|line| find_next_value(line, true))
.sum::<i32>()
.to_string()
}
Expand Down

0 comments on commit 9a12724

Please sign in to comment.