-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
73 lines (66 loc) · 1.66 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
struct Register {
value: i32,
add_queue: Vec<i32>,
}
impl Register {
pub const fn new() -> Self {
Self {
value: 1,
add_queue: Vec::new(),
}
}
pub fn is_done(&self) -> bool {
self.add_queue.is_empty()
}
pub fn push(&mut self, add_value: i32) {
if add_value != 0 {
self.add_queue.push(0);
}
self.add_queue.push(add_value);
}
pub fn cycle(&mut self) {
if self.is_done() {
return;
}
self.value += self.add_queue.remove(0);
}
}
fn main() {
let input = include_str!("../input.txt");
let mut register = Register::new();
for line in input.lines() {
let add_value = match &line[..4] {
"noop" => 0,
"addx" => line[5..].parse().unwrap(),
_ => panic!("{line} is not a valid instruction"),
};
register.push(add_value);
}
let mut cycle = 1;
let mut strength_sum = 0;
let mut crt = String::new();
while !register.is_done() {
// Part 1
if matches!(cycle, 20 | 60 | 100 | 140 | 180 | 220) {
strength_sum += register.value * cycle;
}
// Part 2
let crt_pos = cycle % 40 - 1;
if register.value == crt_pos
|| register.value + 1 == crt_pos
|| register.value - 1 == crt_pos
{
crt.push('#');
} else {
crt.push(' ');
}
if cycle % 40 == 0 {
crt.push('\n');
}
// Cycle
register.cycle();
cycle += 1;
}
println!("Part 1: {strength_sum}");
println!("Part 2: \n{crt}");
}