-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.rs
37 lines (32 loc) · 760 Bytes
/
07.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
#![feature(test)]
type Input = Vec<i32>;
fn setup(input: &str) -> Input {
input
.trim()
.split(',')
.map(|x| x.parse().unwrap())
.collect()
}
fn part1(input: &Input) -> String {
(0..=input.len())
.map(|pos| input.iter().map(|n| (pos as i32 - *n).abs()).sum::<i32>())
.min()
.unwrap()
.to_string()
}
fn part2(input: &Input) -> String {
(0..=input.len())
.map(|pos| {
input
.iter()
.map(|n| {
let x = (pos as i32 - *n).abs();
(x * (x + 1)) >> 1
})
.sum::<i32>()
})
.min()
.unwrap()
.to_string()
}
aoc::main!(2021, 7, ex: 1);