generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.rs
127 lines (113 loc) · 3.19 KB
/
15.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
advent_of_code::solution!(15);
pub fn part_one(input: &str) -> Option<u32> {
let mut hash_sum = 0u32;
let mut cur_hash = 0u8;
for ch in input.as_bytes() {
match *ch {
b',' | b'\n' => {
hash_sum += cur_hash as u32;
cur_hash = 0;
}
ch => cur_hash = cur_hash.wrapping_add(ch).wrapping_mul(17),
}
}
Some(hash_sum)
}
pub fn part_two(input: &str) -> Option<u32> {
let mut map = LensMap::new();
for op in input
.trim()
.as_bytes()
.split(|&ch| ch == b',')
.map(Operation::from)
{
match op {
Operation::Remove(label) => map.remove(label),
Operation::Add(lens) => map.upsert(lens),
}
}
Some(map.focusing_power())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Lens<'a> {
label: &'a [u8],
focal_length: u8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Operation<'a> {
Remove(&'a [u8]),
Add(Lens<'a>),
}
impl<'a> From<&'a [u8]> for Operation<'a> {
fn from(value: &'a [u8]) -> Self {
let op_index = value
.iter()
.position(|&ch| ch == b'-' || ch == b'=')
.unwrap();
match value[op_index] {
b'-' => Self::Remove(&value[..op_index]),
b'=' => Self::Add(Lens {
label: &value[..op_index],
focal_length: (value[op_index + 1] as char).to_digit(10).unwrap() as u8,
}),
_ => unreachable!("invalid operation"),
}
}
}
struct LensMap<'a> {
boxes: [Vec<Lens<'a>>; 256],
}
impl<'a> LensMap<'a> {
pub const fn new() -> Self {
const EMPTY_VEC: Vec<Lens> = Vec::new();
Self {
boxes: [EMPTY_VEC; 256],
}
}
pub fn upsert(&mut self, lens: Lens<'a>) {
let b = &mut self.boxes[self.hash(lens.label) as usize];
if let Some(el) = b.iter_mut().find(|el| el.label == lens.label) {
el.focal_length = lens.focal_length;
return;
}
b.push(lens);
}
pub fn remove(&mut self, label: &[u8]) {
let b = &mut self.boxes[self.hash(label) as usize];
if let Some(index) = b.iter().position(|el| el.label == label) {
b.remove(index);
}
}
pub fn focusing_power(&self) -> u32 {
(0..256)
.map(|b| {
self.boxes[b]
.iter()
.enumerate()
.map(|(s, el)| (b as u32 + 1) * (s as u32 + 1) * (el.focal_length as u32))
.sum::<u32>()
})
.sum()
}
fn hash(&self, label: &[u8]) -> u8 {
label
.iter()
.copied()
.filter(|&ch| ch != b'\n')
.fold(0, |acc, ch| ((acc + ch as u16) * 17) % 256) as u8
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(1320));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(145));
}
}