main
mat ess 2022-12-10 12:14:00 -05:00
parent 5c6facfa66
commit b6eaa973c4
3 changed files with 392 additions and 0 deletions

146
inputs/10.small.txt Normal file
View File

@ -0,0 +1,146 @@
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop

145
inputs/10.txt Normal file
View File

@ -0,0 +1,145 @@
noop
noop
noop
addx 3
addx 7
noop
noop
noop
noop
addx 6
noop
addx -1
noop
addx 5
addx 1
noop
addx 4
noop
noop
noop
noop
addx 6
addx -1
noop
addx 3
addx -13
addx -22
noop
noop
addx 3
addx 2
addx 11
addx -4
addx 11
addx -10
addx 2
addx 5
addx 2
addx -2
noop
addx 7
addx 3
addx -2
addx 2
addx 5
addx 2
addx -2
addx -8
addx -27
addx 5
addx 2
addx 21
addx -21
addx 3
addx 5
addx 2
addx -3
addx 4
addx 3
addx 1
addx 5
noop
noop
noop
noop
addx 3
addx 1
addx 6
addx -31
noop
addx -4
noop
noop
noop
noop
addx 3
addx 7
noop
addx -1
addx 1
addx 5
noop
addx 1
noop
addx 2
addx -8
addx 15
addx 3
noop
addx 2
addx 5
noop
noop
noop
addx -28
addx 11
addx -20
noop
addx 7
addx -2
addx 7
noop
addx -2
noop
addx -6
addx 11
noop
addx 3
addx 2
noop
noop
addx 7
addx 3
addx -2
addx 2
addx 5
addx 2
addx -16
addx -10
addx -11
addx 27
addx -20
noop
addx 2
addx 3
addx 5
noop
noop
noop
addx 3
addx -2
addx 2
noop
addx -14
addx 21
noop
addx -6
addx 12
noop
addx -21
addx 24
addx 2
noop
noop
noop

View File

@ -250,9 +250,110 @@ solutions! {
let visits = motion_visits(motions, 10); let visits = motion_visits(motions, 10);
(visits.len() as u64).into() (visits.len() as u64).into()
}, },
],
[
// day 10 part 1
|input| {
let cmds = input.lines().map(Cmd::from_str).collect();
Screen::new().get_signal_strength(cmds).into()
},
// day 10 part 2
|input| {
let cmds = input.lines().map(Cmd::from_str).collect();
Screen::new().draw(cmds).to_string().into()
}
] ]
} }
struct Screen {
x: isize,
cycle: isize,
strength: isize,
output: String,
}
impl Screen {
fn new() -> Self {
Screen {
x: 1,
cycle: 1,
strength: 0,
output: String::new(),
}
}
fn step_cycle(&mut self) {
println!("cycle {}, x={}", self.cycle, self.x);
self.cycle += 1;
if (self.cycle - 20) % 40 == 0 {
println!(
"cycle {}, x={}, signal={}",
self.cycle,
self.x,
self.x * self.cycle
);
self.strength += self.x * self.cycle;
}
}
fn get_signal_strength(&mut self, cmds: Vec<Cmd>) -> u64 {
for cmd in cmds {
match cmd {
Cmd::Noop => self.step_cycle(),
Cmd::Addx(n) => {
self.step_cycle();
self.x += n;
self.step_cycle();
}
}
}
self.strength as u64
}
fn draw_cycle(&mut self) {
let coord = (self.cycle - 1) % 40;
let sprite = self.x - 1..=self.x + 1;
if sprite.contains(&coord) {
self.output.push('#');
} else {
self.output.push('.');
}
if self.cycle % 40 == 0 {
self.output.push('\n')
}
self.cycle += 1
}
fn draw(&mut self, cmds: Vec<Cmd>) -> &str {
for cmd in cmds {
match cmd {
Cmd::Noop => self.draw_cycle(),
Cmd::Addx(n) => {
self.draw_cycle();
self.draw_cycle();
self.x += n;
}
}
}
&self.output
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum Cmd {
Noop,
Addx(isize),
}
impl Cmd {
fn from_str(s: &str) -> Self {
match s {
"noop" => Cmd::Noop,
_ => Cmd::Addx(s[5..].parse::<isize>().unwrap()),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct Pos { struct Pos {
x: isize, x: isize,