-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
157 lines (134 loc) · 3.86 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
156
157
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
use itertools::Itertools;
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one::<93>(data));
println!("Part 2: {}", part_two::<93>(data));
}
fn part_one<const N: usize>(data: &str) -> usize {
let firewall = Firewall::<N>::new(data);
firewall.severity()
}
fn part_two<const N: usize>(data: &str) -> usize {
let mut cycles = find_main_cycles(data);
let mut count = 0;
let mut step = 2;
remove_solved_cycles(&mut cycles, &mut step);
while !cycles.is_empty() {
for (cycle, position) in &mut cycles {
*position = (*position + step) % *cycle;
}
count += step;
remove_solved_cycles(&mut cycles, &mut step);
}
let mut firewall = Firewall::<N>::new(data);
firewall.step_by(count);
while firewall.blocked() {
firewall.step_by(step);
count += step;
}
count
}
fn find_main_cycles(data: &str) -> Vec<(usize, usize)> {
let mut main_cycles = Vec::new();
for (cycle, positions) in &data
.lines()
.map(|l| l.split_once(": ").unwrap())
.map(|(d, r)| (d.parse::<usize>().unwrap(), r.parse::<usize>().unwrap()))
.map(|(d, r)| (2 * (r - 1), d))
.map(|(c, d)| (c, d % c))
.sorted_by_key(|&(c, _)| c)
.group_by(|&(c, _)| c)
{
let group: Vec<_> = positions.map(|(_, d)| d).sorted().collect();
if group.len() == cycle / 2 - 1 {
let cycle_gap = match (0..).step_by(2).zip(group.iter()).find(|&(c, &g)| c != g) {
Some((c, _)) => c,
None => cycle - 2,
};
main_cycles.push((cycle, cycle_gap));
}
}
main_cycles
}
fn remove_solved_cycles(cycles: &mut Vec<(usize, usize)>, step: &mut usize) {
if let Some(steps) = cycles
.iter()
.filter(|(_, p)| p == &0)
.map(|(c, _)| c / 2)
.optional_product()
{
*step *= steps;
cycles.retain(|(_, p)| p != &0);
}
}
trait OptionalProduct {
fn optional_product(self) -> Option<usize>
where
Self: Sized + std::iter::Iterator<Item = usize>,
{
let mut product = 1;
let mut any = false;
for item in self {
any = true;
product *= item;
}
if any {
Some(product)
} else {
None
}
}
}
impl<I> OptionalProduct for I where I: Iterator {}
#[derive(Debug)]
struct Firewall<const N: usize> {
ranges: [usize; N],
positions: [Option<usize>; N],
}
impl<const N: usize> Firewall<N> {
fn new(input: &str) -> Self {
let mut ranges = [0; N];
let mut positions = [None; N];
for (depth, range) in input
.lines()
.map(|l| l.split_once(": ").unwrap())
.map(|(d, r)| (d.parse::<usize>().unwrap(), r.parse::<usize>().unwrap()))
{
ranges[depth] = range;
positions[depth] = Some(depth % (2 * (range - 1)));
}
Self { ranges, positions }
}
fn severity(&self) -> usize {
self.positions
.iter()
.zip(self.ranges.iter())
.enumerate()
.filter(|&(_, (&p, _))| p == Some(0))
.map(|(i, (_, &r))| i * r)
.sum()
}
fn step_by(&mut self, val: usize) {
for (p, &r) in self.positions.iter_mut().zip(self.ranges.iter()) {
*p = p.map(|v| (v + val) % (2 * (r - 1)));
}
}
fn blocked(&self) -> bool {
self.positions.iter().any(|&p| p == Some(0))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(24, part_one::<7>(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(10, part_two::<7>(data));
}
}