-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.rs
136 lines (117 loc) · 3.44 KB
/
day15.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
pub struct Parser {
pub simple: Vec<String>,
pub steps: Vec<(String, Operation)>
}
impl Parser {
fn hashmapper(&self) -> Vec<Vec<Lens>> {
let mut boxes: Vec<Vec<Lens>> = vec![vec![]; 256];
for (label, operation) in self.steps.iter() {
let box_num = hasher(label);
match operation {
Operation::Insert(folen) => {
if let Some(lens) = boxes[box_num].iter_mut().find(|bx| bx.label == *label) {
lens.folen = *folen;
} else {
boxes[box_num].push( Lens { label: label.clone(), folen: *folen } );
}
},
Operation::Remove => {
if let Some(index) = boxes[box_num].iter().position(|bx| bx.label == *label) {
boxes[box_num].remove(index);
}
},
}
}
boxes
}
fn hash_score(&self) -> usize {
self.simple
.iter()
.map(|step| hasher(step))
.sum()
}
}
pub enum Operation {
Insert(usize),
Remove
}
#[derive(Debug, Clone)]
pub struct Lens {
label: String,
folen: usize,
}
trait LensBox {
fn focusing_power(&self) -> usize;
}
impl LensBox for Vec<Vec<Lens>> {
fn focusing_power(&self) -> usize {
self.iter()
.enumerate()
.fold(0, |power, (bx, slots)| {
let box_score = bx + 1;
power + slots
.iter()
.enumerate()
.map(|(slot, lens)| (slot + 1) * lens.folen)
.sum::<usize>()
* box_score
})
}
}
pub fn hasher(label: &str) -> usize {
label.chars()
.fold(0, |cv, char| (cv + char as usize) * 17 % 256)
}
pub fn initialiser(step: &str) -> (String, Operation) {
if step.ends_with('-') {
let label = step[..step.len() - 1].to_string();
(label, Operation::Remove)
} else if let Some((label, num)) = step.split_once('=') {
(label.to_string(), Operation::Insert(num.parse::<usize>().unwrap()))
} else {
panic!("Invalid instruction.");
}
}
#[aoc_generator(day15)]
// Modified for Part 2 to offload processing to the generator. There's a bit too much redundancy for my liking.
pub fn input_generator(input: &str) -> Parser {
Parser {
simple: input
.lines()
.flat_map(|line| line.trim().split(',').map(String::from))
.collect(),
steps: input
.lines()
.flat_map(|line| line.trim().split(',').map(String::from))
.map(|step| initialiser(&step))
.collect(),
}
}
#[aoc(day15, part1)]
pub fn solve_part1(input: &Parser) -> usize {
input.hash_score()
}
#[aoc(day15, part2)]
pub fn solve_part2(input: &Parser) -> usize {
input.hashmapper().focusing_power()
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7";
#[test]
fn hash_test() {
assert_eq!(hasher("rn=1"), 30);
assert_eq!(hasher("cm-"), 253);
assert_eq!(hasher("qp=3"), 97);
assert_eq!(hasher("cm=2"), 47);
}
#[test]
fn part1_test() {
assert_eq!(solve_part1(&input_generator(TEST)), 1320);
}
#[test]
fn part2_test() {
assert_eq!(solve_part2(&input_generator(TEST)), 145);
}
}