-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15.rs
147 lines (124 loc) · 4.03 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
137
138
139
140
141
142
143
144
145
146
147
use crate::solutions::Solution;
use std::collections::HashMap;
use std::ops::Mul;
pub struct Day15;
impl Solution for Day15 {
fn part_one(&self, input: &str) -> String {
let steps: Vec<&str> = input.split_terminator(',').collect();
steps
.into_iter()
.map(Day15::hash)
.sum::<usize>()
.to_string()
}
fn part_two(&self, input: &str) -> String {
let steps: Vec<&str> = input.split_terminator(',').collect();
let mut boxes: HashMap<usize, Vec<Lens>> = HashMap::with_capacity(steps.len());
for step in steps {
let lens = Lens::try_from(step).unwrap();
let box_number = Self::hash(lens.label.as_str());
let current_box = boxes.entry(box_number).or_default();
if lens.operation == Operation::Equal {
if let Some(position) = current_box.iter().position(|l| l == &lens) {
*current_box.get_mut(position).unwrap() = lens;
} else {
current_box.push(lens);
}
} else if let Some(position) = current_box.iter().position(|l| l == &lens) {
current_box.remove(position);
}
}
boxes
.iter()
.map(|(i, current_box)| {
current_box
.iter()
.enumerate()
.map(|(p, lens)| (i + 1) * (p + 1) * lens.focal_length)
.sum::<usize>()
})
.sum::<usize>()
.to_string()
}
}
impl Day15 {
fn hash(step: &str) -> usize {
step.as_bytes()
.iter()
.fold(0, |current, char| (current + *char as usize).mul(17) % 256)
}
}
#[derive(Debug, PartialEq, Clone)]
enum Operation {
Dash,
Equal,
}
#[derive(Debug, Clone)]
struct Lens {
label: String,
operation: Operation,
focal_length: usize,
}
impl TryFrom<&str> for Lens {
type Error = String;
fn try_from(value: &str) -> Result<Self, Self::Error> {
if value.contains('=') {
let vec = value.split_terminator('=').collect::<Vec<&str>>();
let mut parts = vec.iter();
return Ok(Self {
label: parts.next().unwrap().to_string(),
focal_length: parts.next().unwrap().parse().unwrap(),
operation: Operation::Equal,
});
} else if value.contains('-') {
let without_dash = value.replace('-', "");
return Ok(Self {
label: without_dash,
focal_length: 0,
operation: Operation::Dash,
});
}
Err(String::from("Unrecognized operation"))
}
}
impl PartialEq<Self> for Lens {
fn eq(&self, other: &Self) -> bool {
self.label == other.label
}
}
#[cfg(test)]
mod tests {
use crate::file_system::read_example;
use crate::solutions::day15::Day15;
use crate::solutions::Solution;
#[test]
fn part_one_example_test() {
let input = read_example("15");
assert_eq!("1320", Day15.part_one(input.as_str()));
}
#[test]
fn part_two_example_test() {
let input = read_example("15");
assert_eq!("145", Day15.part_two(input.as_str()));
}
#[test]
fn hash_test() {
assert_eq!(52, Day15::hash("HASH"));
assert_eq!(30, Day15::hash("rn=1"));
assert_eq!(253, Day15::hash("cm-"));
assert_eq!(97, Day15::hash("qp=3"));
assert_eq!(14, Day15::hash("qp-"));
assert_eq!(180, Day15::hash("pc=4"));
assert_eq!(9, Day15::hash("ot=9"));
assert_eq!(197, Day15::hash("ab=5"));
assert_eq!(48, Day15::hash("pc-"));
assert_eq!(214, Day15::hash("pc=6"));
assert_eq!(231, Day15::hash("ot=7"));
assert_eq!(0, Day15::hash("rn"));
assert_eq!(0, Day15::hash("cm"));
assert_eq!(1, Day15::hash("qp"));
assert_eq!(3, Day15::hash("pc"));
assert_eq!(3, Day15::hash("ot"));
assert_eq!(3, Day15::hash("ab"));
}
}