Created
December 10, 2022 21:01
-
-
Save thomasweitzel/630d099d7ab848b714ae770fed714462 to your computer and use it in GitHub Desktop.
Advent of Code 2022, my solution for day 10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See: https://adventofcode.com/2022/day/10 | |
#[derive(Debug, Copy, Clone)] | |
enum Cmd { | |
Noop, | |
Addx(isize), | |
} | |
#[derive(Debug, Copy, Clone)] | |
struct State { | |
after_cycle: usize, | |
x: isize, | |
} | |
fn parse(input: &str) -> Vec<Cmd> { | |
input | |
.lines() | |
.map(|line| line.split_whitespace().collect::<Vec<&str>>()) | |
.map(|v| { | |
match v[0] { | |
"noop" => Cmd::Noop, | |
"addx" => Cmd::Addx(v[1].parse::<isize>().unwrap()), | |
_ => panic!("Invalid command {}", v[0]), | |
} | |
}) | |
.collect() | |
} | |
fn process(cmds: &Vec<Cmd>) -> Vec<State> { | |
let mut states: Vec<State> = vec![]; | |
let initial_state = State { | |
after_cycle: 0, | |
x: 1, | |
}; | |
states.push(initial_state); | |
for cmd in cmds { | |
let last_state = states[states.len() - 1]; | |
let cycles = match cmd { | |
Cmd::Noop => 1, | |
Cmd::Addx(_) => 2, | |
}; | |
let v = match cmd { | |
Cmd::Addx(i) => i, | |
_ => &0 | |
}; | |
let state = State { | |
after_cycle: last_state.after_cycle + cycles, | |
x: last_state.x + v, | |
}; | |
states.push(state); | |
} | |
states | |
} | |
fn get_state(states: &[State], during_cycle: usize) -> State { | |
*states | |
.iter() | |
.filter(|state| state.after_cycle < during_cycle) | |
.last() | |
.unwrap() | |
} | |
fn render(pixels: Vec<bool>, line_length: usize) { | |
pixels.iter().enumerate().for_each(|(i, &pixel)| { | |
match pixel { | |
true => print!("#"), | |
false => print!(" "), | |
} | |
if (i + 1) % line_length == 0 { | |
println!(); | |
} | |
}); | |
} | |
pub fn part_one(input: &str) -> isize { | |
let cmds = parse(input); | |
let states = process(&cmds); | |
let cycles: Vec<usize> = vec![20, 60, 100, 140, 180, 220]; | |
let sum_signal_strength: isize = cycles | |
.iter() | |
.map(|&cycle| { | |
let state = get_state(&states, cycle); | |
state.x * (cycle as isize) | |
}) | |
.sum(); | |
sum_signal_strength | |
} | |
pub fn part_two(input: &str) -> Vec<bool> { | |
let cmds = parse(input); | |
let states = process(&cmds); | |
(1..240 + 1) | |
.map(|cycle| { | |
let state = get_state(&states, cycle); | |
let sprite_middle = state.x; | |
let pixel_to_draw = (cycle - 1) % 40; | |
// Sprite is three pixels wide | |
if (sprite_middle - (pixel_to_draw as isize)).abs() <= 1 { | |
return true; | |
} | |
false | |
}) | |
.collect() | |
} | |
fn main() { | |
let input = include_str!("../inputs/10.txt"); | |
println!("Part 1 = {:?}", part_one(input)); | |
println!("Part 2 = "); | |
render(part_two(input), 40); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment