-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03.rs
More file actions
57 lines (44 loc) · 1.32 KB
/
Copy path03.rs
File metadata and controls
57 lines (44 loc) · 1.32 KB
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
51
52
53
54
55
56
57
advent_of_code::solution!(3);
fn parse_data(input: &str) -> Vec<Vec<u8>> {
input.lines().map(|line| line.bytes().map(|x| x - b'0').collect()).collect()
}
fn part_x<const COUNT: usize>(bank: &[u8]) -> u64 {
let mut result = 0;
let mut index = 0;
for c in (0..COUNT).rev() {
let (max_i, &max_v) = bank[index..bank.len() - c]
.iter()
.enumerate()
.max_by_key(|&(i, v)| (v, usize::MAX - i))
.unwrap();
index += max_i + 1;
result = result * 10 + max_v as u64;
}
result
}
pub fn part_one(input: &str) -> Option<u64> {
let data = parse_data(input);
let result = data.iter().map(|bank| part_x::<2>(bank)).sum();
Some(result)
}
pub fn part_two(input: &str) -> Option<u64> {
let data = parse_data(input);
let result = data.iter().map(|bank| part_x::<12>(bank)).sum();
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let input = advent_of_code::template::read_file("examples", DAY);
let result = part_one(&input);
assert_eq!(result, Some(357));
}
#[test]
fn test_part_two() {
let input = advent_of_code::template::read_file("examples", DAY);
let result = part_two(&input);
assert_eq!(result, Some(3121910778619));
}
}