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
|
struct Foo {
acc: u32,
max: [u32;3],
}
pub fn run(input: String) {
let init = Foo{
acc: 0,
max: [0,0,0],
};
let res = input.lines().fold(init, |foo, line| {
return if line.is_empty() {
let mut arr = [foo.acc, foo.max[0], foo.max[1], foo.max[2]];
arr.sort();
Foo {
acc: 0,
max: [arr[1], arr[2], arr[3]],
}
} else {
Foo {
acc: foo.acc + line.parse::<u32>().expect("input line wasn't a u32!"),
max: foo.max,
}
}
});
println!("day 1: {}", res.max.into_iter().sum::<u32>());
}
|