Compare commits
2 Commits
71984d10d6
...
b7005f244d
| Author | SHA1 | Date | |
|---|---|---|---|
| b7005f244d | |||
| 05f3d9411a |
16
src/bin/d10p1.rs
Normal file
16
src/bin/d10p1.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
use aoc22::{day10, util};
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let instructions = day10::parse_instructions(&util::parse_input());
|
||||||
|
let mut cpu = day10::CPU::new(instructions);
|
||||||
|
|
||||||
|
let mut sum = 0;
|
||||||
|
for _ in 0..221 {
|
||||||
|
if (cpu.cycle - (20 - 1)) % 40 == 0 {
|
||||||
|
sum += cpu.signal_strength();
|
||||||
|
}
|
||||||
|
cpu.do_cycle().expect("No more instructions?");
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Sum of 20 + 40n cycles: {}", sum);
|
||||||
|
}
|
||||||
14
src/bin/d10p2.rs
Normal file
14
src/bin/d10p2.rs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
use aoc22::{day10, util};
|
||||||
|
|
||||||
|
pub fn main() {
|
||||||
|
let instructions = day10::parse_instructions(&util::parse_input());
|
||||||
|
let mut cpu = day10::CPU::new(instructions);
|
||||||
|
let mut crt = day10::CRT::new();
|
||||||
|
|
||||||
|
for _ in 0..240 {
|
||||||
|
crt.render(&cpu);
|
||||||
|
cpu.do_cycle().expect("No more instructions?");
|
||||||
|
}
|
||||||
|
|
||||||
|
crt.draw();
|
||||||
|
}
|
||||||
104
src/day10.rs
Normal file
104
src/day10.rs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Instruction {
|
||||||
|
Noop,
|
||||||
|
Addx(isize),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_instructions(input: &String) -> Vec<Instruction> {
|
||||||
|
let mut result = Vec::new();
|
||||||
|
|
||||||
|
for line in input.lines() {
|
||||||
|
if line == "noop" {
|
||||||
|
result.push(Instruction::Noop);
|
||||||
|
} else {
|
||||||
|
let (inst, v) = line.split_once(' ').unwrap();
|
||||||
|
assert_eq!(inst, "addx");
|
||||||
|
let v = v.parse().unwrap();
|
||||||
|
result.push(Instruction::Addx(v));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct CPU {
|
||||||
|
pub x: isize,
|
||||||
|
pub cycle: isize,
|
||||||
|
instructions: Vec<Instruction>,
|
||||||
|
pc: usize,
|
||||||
|
inst_cycle: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CPU {
|
||||||
|
pub fn new(instructions: Vec<Instruction>) -> CPU {
|
||||||
|
CPU {
|
||||||
|
x: 1,
|
||||||
|
cycle: 0,
|
||||||
|
instructions,
|
||||||
|
pc: 0,
|
||||||
|
inst_cycle: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn do_cycle(&mut self) -> Option<()> {
|
||||||
|
if let Some(inst) = self.instructions.get(self.pc) {
|
||||||
|
match inst {
|
||||||
|
Instruction::Noop => {
|
||||||
|
self.pc += 1;
|
||||||
|
}
|
||||||
|
Instruction::Addx(v) => {
|
||||||
|
self.inst_cycle += 1;
|
||||||
|
if self.inst_cycle == 2 {
|
||||||
|
self.pc += 1;
|
||||||
|
self.inst_cycle = 0;
|
||||||
|
self.x += v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.cycle += 1;
|
||||||
|
Some(())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn signal_strength(&self) -> isize {
|
||||||
|
return self.x * (self.cycle + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CRT {
|
||||||
|
pub pixels: [[char; 40]; 6],
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CRT {
|
||||||
|
pub fn new() -> CRT {
|
||||||
|
CRT {
|
||||||
|
pixels: [['.'; 40]; 6],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render(&mut self, cpu: &CPU) {
|
||||||
|
let c = cpu.cycle;
|
||||||
|
assert!(c >= 0 && c < 240);
|
||||||
|
let x = c % 40;
|
||||||
|
let y = c / 40;
|
||||||
|
let sprite_pos = cpu.x - 1;
|
||||||
|
let px = &mut self.pixels[y as usize][x as usize];
|
||||||
|
if x >= sprite_pos && x < sprite_pos + 3 {
|
||||||
|
*px = '#';
|
||||||
|
} else {
|
||||||
|
*px = '.';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw(&self) {
|
||||||
|
for y in 0..6 {
|
||||||
|
for x in 0..40 {
|
||||||
|
print!("{}", self.pixels[y][x]);
|
||||||
|
}
|
||||||
|
print!("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
pub mod day1;
|
pub mod day1;
|
||||||
|
pub mod day10;
|
||||||
pub mod day2;
|
pub mod day2;
|
||||||
pub mod day3;
|
pub mod day3;
|
||||||
pub mod day4;
|
pub mod day4;
|
||||||
|
|||||||
Reference in New Issue
Block a user