-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
131 lines (111 loc) · 3.08 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
#![warn(clippy::all, clippy::pedantic, clippy::nursery)]
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one::<6>(data));
println!("Part 2: {}", part_two::<7>(data));
}
fn part_one<const N: usize>(data: &str) -> u32 {
let mut s = Sculpture::<N>::new(data);
s.solve()
}
fn part_two<const N: usize>(data: &str) -> u32 {
let additional = data.to_owned() + "Disc #7 has 11 positions; at time=0, it is at position 0.";
let mut s = Sculpture::<N>::new(&additional);
s.solve()
}
#[derive(Debug)]
struct Sculpture<const N: usize> {
discs: [Disc; N],
step: u32,
base: u32,
}
impl<const N: usize> Sculpture<N> {
fn new(data: &str) -> Self {
let mut lines = data.lines();
let discs = std::array::from_fn(|_| Disc::new(lines.next().unwrap()));
let base = discs
.iter()
.filter(|&d| d.aligned())
.map(|d| d.modulo)
.chain(std::iter::once(1))
.product();
Self {
discs,
step: 0,
base,
}
}
fn solve(&mut self) -> u32 {
while self.discs.iter().any(|d| !d.aligned()) {
self.solve_next();
}
self.step
}
fn solve_next(&mut self) {
let mut solved = false;
let mut rotation = 0;
while !solved {
rotation += self.base;
for disc in self.discs.iter_mut().filter(|d| !d.aligned()) {
disc.rotate(self.base);
if disc.aligned() {
solved = true;
}
}
}
self.step += rotation;
self.base = self
.discs
.iter()
.filter(|&d| d.aligned())
.map(|d| d.modulo)
.product();
}
}
#[derive(Debug, Clone, Copy)]
struct Disc {
//id: u32,
modulo: u32,
position: u32,
}
impl Disc {
fn new(line: &str) -> Self {
let (id, rest) = line
.trim_start_matches("Disc #")
.split_once(" has ")
.unwrap();
let (modulo, rest) = rest.split_once(" positions;").unwrap();
let (_, position) = rest.split_once(" position ").unwrap();
let id = id.parse::<u32>().unwrap();
let modulo = modulo.parse().unwrap();
let position = position.trim_end_matches('.').parse::<u32>().unwrap();
// Normalise position based on how far away from the drop it it
let position = (position + id) % modulo;
Self {
//id,
modulo,
position,
}
}
fn rotate(&mut self, steps: u32) {
let mod_steps = steps % self.modulo;
self.position = (self.position + mod_steps) % self.modulo;
}
const fn aligned(&self) -> bool {
self.position == 0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(5, part_one::<2>(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(15, part_two::<3>(data));
}
}