-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_10.rs
81 lines (71 loc) · 1.96 KB
/
day_10.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
pub fn run() {
let input_str = include_str!("../../inputs/input_10.txt");
let input: Vec<(&str, i32)> = input_str
.lines()
.map(|line| {
let line_split: Vec<&str> = line
.split_ascii_whitespace()
.collect();
(line_split[0], line_split.get(1).unwrap_or(&"0").parse().unwrap())
})
.collect();
part_one(&input);
part_two(&input);
}
fn part_one(input: &Vec<(&str, i32)>) {
let mut signal_strength_sum = 0;
let mut x_register = 1;
let mut cycle = 0;
for &(command, arg) in input {
cycle += 1;
if cycle % 40 == 20 {
signal_strength_sum += cycle * x_register;
}
match command {
"noop" => (),
"addx" => {
cycle += 1;
if cycle % 40 == 20 {
signal_strength_sum += cycle * x_register;
}
x_register += arg;
},
_ => println!("Invalid command '{command}'")
};
}
println!("Part one: {signal_strength_sum}");
}
fn part_two(input: &Vec<(&str, i32)>) {
let mut x_register: i32 = 1;
let mut cycle: i32 = 0;
println!("Part two:");
for &(command, arg) in input {
cycle += 1;
if ((cycle - 1) % 40).abs_diff(x_register) <= 1 {
print!("█");
}
else {
print!(" ");
}
if cycle % 40 == 0 {
println!();
}
match command {
"noop" => (),
"addx" => {
cycle += 1;
if ((cycle - 1) % 40).abs_diff(x_register) <= 1 {
print!("█");
}
else {
print!(" ");
}
if cycle % 40 == 0 {
println!();
}
x_register += arg;
},
_ => println!("Invalid command '{command}'")
};
}
}