-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
171 lines (148 loc) · 3.65 KB
/
mod.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
use anyhow::{Context, Error, Result};
use itertools::Itertools;
use std::str::FromStr;
use crate::utils::AocError::*;
#[derive(Debug, Clone)]
pub struct Lens {
label: String,
focal: u32,
}
impl FromStr for Lens {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let mut split = s.split('=');
let label = split
.next()
.ok_or(GenericError)
.context("No label")?
.to_string();
let focal = split
.next()
.ok_or(GenericError)
.context("No focal")?
.parse::<u32>()?;
Ok(Lens { label, focal })
}
}
#[derive(Debug, Clone)]
pub struct Bin {
lenses: Vec<Lens>,
}
impl Bin {
pub fn new() -> Bin {
Bin { lenses: vec![] }
}
pub fn power(&self) -> usize {
self.lenses
.iter()
.enumerate()
.map(|(i, v)| (i + 1) * (v.focal as usize))
.sum()
}
}
impl Default for Bin {
fn default() -> Self {
Self::new()
}
}
pub fn hash(s: &str) -> u32 {
let mut val = 0;
for c in s.chars() {
let ascii = c as u8 as u32;
val += ascii;
val *= 17;
val %= 256;
}
val
}
#[aoc_generator(day15)]
pub fn input_generator(input: &str) -> Result<Vec<String>> {
Ok(input
.lines()
.find(|s| !s.is_empty())
.ok_or(GenericError)
.context("Empty input")?
.split(',')
.map(|v| v.to_string())
.collect_vec())
}
#[aoc(day15, part1)]
pub fn solve_part1(input: &[String]) -> Result<u32> {
Ok(input.iter().map(|s| hash(s)).sum())
}
pub fn index(s: &str) -> Result<usize> {
if s.contains('=') {
Ok(hash(
s.split('=')
.next()
.ok_or(GenericError)
.context("No label")?,
) as usize)
} else if s.contains('-') {
Ok(hash(
s.split('-')
.next()
.ok_or(GenericError)
.context("No label2")?,
) as usize)
} else {
Err(GenericError).context("No valid lens")
}
}
#[aoc(day15, part2)]
pub fn solve_part2(input: &[String]) -> Result<usize> {
let mut boxes = vec![Bin::new(); 256];
for lens in input {
let index = index(lens)?;
let mut b = boxes[index].clone();
if lens.contains('=') {
let l = Lens::from_str(lens)?;
if let Some(pos) = b
.lenses
.clone()
.iter()
.find_position(|v| v.label == l.label)
{
b.lenses[pos.0].focal = l.focal;
} else {
b.lenses.push(l);
}
} else {
let label = lens
.split('-')
.next()
.ok_or(GenericError)
.context("No label")?;
if let Some(pos) = b.lenses.iter().find_position(|v| v.label == label) {
b.lenses.remove(pos.0);
}
}
boxes[index] = b;
}
let power = boxes
.iter()
.enumerate()
.map(|(i, b)| (i + 1) * b.power())
.sum();
Ok(power)
}
#[cfg(test)]
mod test {
use super::*;
fn sample() -> &'static str {
"rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7"
}
fn input() -> Result<Vec<String>> {
input_generator(sample())
}
#[test]
fn part1_sample() -> Result<()> {
let data = input()?;
Ok(assert_eq!(1320, solve_part1(&data)?))
}
#[test]
fn part2_sample() -> Result<()> {
let data = input()?;
Ok(assert_eq!(145, solve_part2(&data)?))
}
}