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
158
159
160
161
162
163
164
165
166
167
168
|
use std::{str::FromStr, string::ParseError};
#[derive(Debug)]
enum Command {
Addx(i32),
NoOp,
}
struct Crt {
width: usize,
height: usize,
pixels: Vec<String>,
}
struct Sprite {
location: i32,
}
impl Crt {
fn new(width: usize, height: usize) -> Self {
let row = ".".repeat(width);
let pixels = vec![row; height];
Crt {
width,
height,
pixels,
}
}
fn get_overlap_with_sprite(&self, sprite: &Sprite, cycle: i32) -> Option<i32> {
let x = cycle % 40;
if x == sprite.location || x == sprite.location - 1 || x == sprite.location + 1 {
Some(x)
} else {
None
}
}
}
impl Sprite {
fn new(intial_location: i32) -> Self {
Sprite {
location: intial_location,
}
}
}
impl FromStr for Command {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"noop" => Ok(Command::NoOp),
_ => {
let (_, value) = s.split_once(" ").unwrap();
Ok(Command::Addx(
value.parse::<i32>().expect("Should be parsable to usize"),
))
}
}
}
}
fn signal_strength(cycle: &usize, x_val: &i32) -> i32 {
*cycle as i32 * x_val
}
fn solve_part_one(data: &str) -> i32 {
let important_cycles: [usize; 6] = [20, 60, 100, 140, 180, 220];
let mut cur_cycle = 0_usize;
let mut x_val = 1_i32;
let mut total_signal_strength = 0_i32;
let commands: Vec<_> = data
.lines()
.map(|line| Command::from_str(line).unwrap())
.collect();
for command in commands {
use Command::*;
match command {
Addx(val) => {
for _ in 0..2 {
cur_cycle += 1;
if important_cycles.contains(&cur_cycle) {
total_signal_strength += signal_strength(&cur_cycle, &x_val);
}
}
x_val += val;
}
NoOp => {
cur_cycle += 1;
if important_cycles.contains(&cur_cycle) {
total_signal_strength += signal_strength(&cur_cycle, &x_val);
}
}
}
}
total_signal_strength
}
fn render_grid(grid: &Vec<String>) {
for row in grid {
for ch in row.chars() {
match ch {
'#' => print!("🥳"),
// '#' => print!("😉"),
'.' => print!("🌑"),
_ => unreachable!(),
}
}
println!();
}
}
fn solve_part_two(data: &str) {
let mut cur_cycle = 0_i32;
let mut y_val = 0_i32;
let mut crt = Crt::new(40, 6);
let mut sprite = Sprite::new(1);
let commands: Vec<_> = data
.lines()
.map(|line| Command::from_str(line).unwrap())
.collect();
for command in commands {
use Command::*;
match command {
Addx(val) => {
for _ in 0..2 {
if cur_cycle % 40 == 0 {
if cur_cycle != 0 {
y_val += 1;
}
}
match crt.get_overlap_with_sprite(&sprite, cur_cycle) {
Some(idx) => crt.pixels[y_val as usize]
.replace_range((idx as usize)..=idx as usize, "#"),
None => {}
}
cur_cycle += 1;
}
sprite.location += val;
}
NoOp => {
if cur_cycle % 40 == 0 {
if cur_cycle != 0 {
y_val += 1;
}
}
match crt.get_overlap_with_sprite(&sprite, cur_cycle) {
Some(idx) => {
crt.pixels[y_val as usize].replace_range((idx as usize)..=idx as usize, "#")
}
None => {}
}
cur_cycle += 1;
}
}
}
render_grid(&crt.pixels);
println!();
}
fn main() {
let test = include_str!("../input/day10.test");
let prod = include_str!("../input/day10.prod");
println!("part1: test {:?}", solve_part_one(test));
println!("part1: prod {:?}", solve_part_one(prod));
solve_part_two(test);
solve_part_two(prod);
}
|