-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.rs
50 lines (41 loc) · 924 Bytes
/
09.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#![feature(test)]
type Input = Vec<Vec<i32>>;
fn setup(input: &str) -> Input {
input
.lines()
.map(|line| {
line.split_whitespace()
.map(|x| x.parse().unwrap())
.collect()
})
.collect()
}
fn solve(mut nums: Vec<i32>) -> i32 {
for j in 1.. {
let mut all_zero = true;
let mut prev = nums[j - 1];
for i in nums.iter_mut().skip(j) {
*i = prev - *i;
prev -= *i;
all_zero &= *i == 0;
}
if all_zero {
break;
}
}
nums.into_iter().sum()
}
fn part1(input: &Input) -> i32 {
input
.clone()
.into_iter()
.map(|mut nums| {
nums.reverse();
solve(nums)
})
.sum()
}
fn part2(input: &Input) -> i32 {
input.clone().into_iter().map(solve).sum()
}
aoc::main!(2023, 9, ex: 1);