-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
152 lines (125 loc) · 3.26 KB
/
lib.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
extern crate core;
pub fn signal1(input: &str) -> i32 {
let mut current_instruction = None;
let mut cycle = 0;
let mut cycle_left = 0;
let mut res = 0;
let mut reg_x = 1;
input.lines().for_each(|ins| {
if ins == "noop" {
cycle_left = 0;
current_instruction = None;
} else if ins.starts_with("addx") {
let add: i32 = ins[5..].parse().unwrap();
current_instruction = Some(Add { add });
cycle_left = 1;
}
cycle += 1;
res += increase_res(cycle, reg_x);
while cycle_left > 0 {
cycle_left -= 1;
cycle += 1;
res += increase_res(cycle, reg_x);
match ¤t_instruction {
None => (),
Some(a) => reg_x += a.add,
};
}
});
res
}
fn increase_res(cycle: i32, reg_x: i32) -> i32 {
if (cycle - 20) % 40 == 0 {
return reg_x * cycle;
}
return 0;
}
pub fn signal2(input: &str) {
let mut crt = CRT::new(6, 40);
let mut current_instruction = None;
let mut cycle = 0;
let mut cycle_left = 0;
let mut reg_x = 1;
input.lines().for_each(|ins| {
if ins == "noop" {
cycle_left = 0;
current_instruction = None;
} else if ins.starts_with("addx") {
let add: i32 = ins[5..].parse().unwrap();
current_instruction = Some(Add { add });
cycle_left = 1;
}
crt.update(cycle, reg_x);
cycle += 1;
while cycle_left > 0 {
cycle_left -= 1;
crt.update(cycle, reg_x);
cycle += 1;
match ¤t_instruction {
None => (),
Some(a) => reg_x += a.add,
};
}
});
crt.print();
}
struct CRT {
lines: Vec<Vec<char>>,
row: usize,
col: usize,
}
impl CRT {
fn new(row: usize, col: usize) -> Self {
let mut lines = Vec::new();
for _ in 0..row {
let mut line = Vec::new();
for _ in 0..col {
line.push('.');
}
lines.push(line);
}
CRT { lines, row, col }
}
fn print(&self) {
self.lines.iter().for_each(|line| {
line.iter().for_each(|c| print!("{}", c));
println!();
})
}
fn update(&mut self, cycle: i32, reg_x: i32) {
let pos = cycle % 40;
if pos == reg_x || pos - 1 == reg_x || pos + 1 == reg_x {
let idx = (cycle / 40) as usize;
self.lines[idx][pos as usize] = '#';
}
}
}
struct Add {
add: i32,
}
struct Noop {}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_signal1_unit() {
let s = fs::read_to_string("test.txt").unwrap();
assert_eq!(signal1(s.as_str()), 13140);
}
#[test]
fn test_signal1_test() {
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(signal1(s.as_str()), 5245);
}
#[test]
fn test_signal2_unit() {
let s = fs::read_to_string("test.txt").unwrap();
signal2(s.as_str())
}
#[test]
fn test_signal2_test() {
let s = fs::read_to_string("input.txt").unwrap();
signal2(s.as_str())
}
}