This repository has been archived by the owner on Jan 6, 2024. It is now read-only.
/
15.rs
192 lines (157 loc) · 4.53 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
advent_of_code::solution!(15);
use std::collections::HashMap;
use itertools::Itertools;
/* == Definitions == */
const HASH_FACTOR: u8 = 17;
const NUMBER_BOXES: usize = 256;
#[derive(Clone)]
struct LensBox {
lenses: Vec<Lens>,
}
struct Instruction<'a> {
label: &'a str,
operation: Operation,
}
#[derive(Clone)]
struct Lens {
atom: Atom,
focal_length: u8,
}
enum Operation {
Add(u8),
Subtract,
}
/// Lookup table for atoms associated with string labels.
struct AtomTable<'a> {
counter: u32,
labels: HashMap<&'a str, Atom>,
}
/// Represents a string label as a unique integer instead, with the
/// mapping stored in `AtomTable`. Allows for fast comparison without
/// heap-allocated strings.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct Atom(u32, u8);
/* == Solutions == */
pub fn part_one(input: &str) -> Option<u32> {
let result = input
.lines()
.flat_map(|line| line.split(','))
.map(|line| hash(line) as u32)
.sum();
Some(result)
}
pub fn part_two(input: &str) -> Option<u32> {
let mut atoms = AtomTable::new();
let mut boxes = vec![LensBox::new(); NUMBER_BOXES];
let instructions = input
.lines()
.flat_map(|line| line.split(','))
.map(parse_instruction);
for instruction in instructions {
let box_index = hash(instruction.label) as usize;
let bx = &mut boxes[box_index];
let atom = atoms.get_label(instruction.label);
match instruction.operation {
Operation::Add(focal_length) => bx.add_lens(Lens { atom, focal_length }),
Operation::Subtract => bx.remove_lens(&atom),
}
}
let result = boxes
.iter()
.enumerate()
.map(|(i, b)| b.focal_power(i))
.sum();
Some(result)
}
/* == Input parsing == */
fn parse_instruction(instruction: &str) -> Instruction {
let index = instruction.find(|x| x == '=' || x == '-').unwrap();
let (label, op_str) = instruction.split_at(index);
let mut op_iterator = op_str.bytes();
match op_iterator.next().unwrap() {
b'=' => Instruction {
label,
operation: Operation::Add(op_iterator.next().unwrap() - b'0'),
},
b'-' => Instruction {
label,
operation: Operation::Subtract,
},
_ => unreachable!(),
}
}
/* == Implementations == */
impl LensBox {
fn new() -> LensBox {
LensBox { lenses: Vec::new() }
}
/// Adds a lens to the back of the box, or if a lens with the same label (atom)
/// is already present, swap it out with the new focal length.
fn add_lens(&mut self, lens: Lens) {
match self.lenses.iter_mut().find(|l| l.atom == lens.atom) {
Some(l) => l.focal_length = lens.focal_length,
None => self.lenses.push(lens),
}
}
/// Removes a lens from a table.
fn remove_lens(&mut self, atom: &Atom) {
if let Some((i, _)) = self.lenses.iter().find_position(|l| l.atom == *atom) {
self.lenses.remove(i);
}
}
// Computes the focal power of the box.
fn focal_power(&self, box_index: usize) -> u32 {
self.lenses
.iter()
.enumerate()
.map(|(i, lens)| (box_index as u32 + 1) * (i as u32 + 1) * lens.focal_length as u32)
.sum()
}
}
impl<'a> AtomTable<'a> {
fn new() -> AtomTable<'a> {
AtomTable {
counter: 0,
labels: HashMap::new(),
}
}
/// Returns the atom associated with a label, or creates a new one if
/// the label is not yet present.
fn get_label<'b: 'a>(&mut self, label: &'b str) -> Atom {
match self.labels.get(label) {
Some(label) => *label,
None => {
self.counter += 1;
let atom = Atom(self.counter, hash(label));
self.labels.insert(label, atom);
atom
}
}
}
}
/* == Utility == */
fn hash(input: &str) -> u8 {
input.bytes().fold(0, |acc, byte| {
acc.wrapping_add(byte).wrapping_mul(HASH_FACTOR)
})
}
#[cfg(test)]
mod tests {
use super::*;
use advent_of_code::template::*;
#[test]
fn test_part_one() {
let result = part_one(&read_example(DAY));
assert_eq!(result, Some(1320));
}
#[test]
fn test_part_two() {
let result = part_two(&read_example(DAY));
assert_eq!(result, Some(145));
}
#[test]
fn test_hash() {
let result = hash("HASH");
assert_eq!(result, 52);
}
}