-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
155 lines (134 loc) · 3.45 KB
/
main.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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> usize {
data.trim().split(',').map(hash).sum()
}
fn part_two(data: &str) -> usize {
let mut fac = Facility::new(data);
fac.run();
fac.focussing_power()
}
struct Facility<'a> {
boxes: [Box<'a>; 256],
instructions: Vec<Instruction<'a>>,
}
impl<'a> Facility<'a> {
fn new(data: &'a str) -> Self {
let boxes = std::array::from_fn(|i| Box::new(i + 1));
let mut instructions = Vec::new();
instructions.extend(data.trim().split(',').map(Instruction::new));
Self {
boxes,
instructions,
}
}
fn run(&mut self) {
self.instructions.iter().for_each(|i| match i.operation {
Op::Add => self.boxes[i.box_num].add(i.label, i.lens.unwrap()),
Op::Remove => self.boxes[i.box_num].remove(i.label),
});
}
fn focussing_power(&self) -> usize {
self.boxes.iter().map(Box::focussing_power).sum()
}
}
struct Instruction<'a> {
label: &'a str,
box_num: usize,
operation: Op,
lens: Option<usize>,
}
impl<'a> Instruction<'a> {
fn new(instruction: &'a str) -> Self {
if let Some((label, lens)) = instruction.split_once('=') {
let lens = lens.parse::<usize>().unwrap();
let box_num = hash(label);
Self {
label,
box_num,
operation: Op::Add,
lens: Some(lens),
}
} else {
let label = instruction.trim_end_matches('-');
let box_num = hash(label);
Self {
label,
box_num,
operation: Op::Remove,
lens: None,
}
}
}
}
enum Op {
Add,
Remove,
}
struct Box<'a> {
number: usize,
lenses: Vec<Lens<'a>>,
}
impl<'a> Box<'a> {
const fn new(number: usize) -> Self {
Self {
number,
lenses: Vec::new(),
}
}
fn add(&mut self, label: &'a str, focal_length: usize) {
if let Some(idx) = self.lenses.iter().position(|l| l.label == label) {
self.lenses[idx].focal_length = focal_length;
} else {
self.lenses.push(Lens::new(label, focal_length));
}
}
fn remove(&mut self, label: &'a str) {
if let Some(idx) = self.lenses.iter().position(|l| l.label == label) {
self.lenses.remove(idx);
}
}
fn focussing_power(&self) -> usize {
self.lenses
.iter()
.enumerate()
.map(|(i, l)| self.number * l.focal_length * (i + 1))
.sum()
}
}
struct Lens<'a> {
label: &'a str,
focal_length: usize,
}
impl<'a> Lens<'a> {
const fn new(label: &'a str, focal_length: usize) -> Self {
Self {
label,
focal_length,
}
}
}
fn hash(input: &str) -> usize {
input
.as_bytes()
.iter()
.fold(0, |acc, &inp| ((acc + usize::from(inp)) * 17) % 256)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(1320, part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(145, part_two(data));
}
}