From 2ca0c6efc3cfc772fcd84bce4265da6fb1c9c94d Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Sat, 10 Dec 2022 10:57:14 +0000 Subject: day10 --- input/day10.txt | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++ input/day10_ex.txt | 3 ++ input/day10_ex2.txt | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/day10.rs | 85 ++++++++++++++++++++++++++++++ src/main.rs | 2 + 5 files changed, 378 insertions(+) create mode 100644 input/day10.txt create mode 100644 input/day10_ex.txt create mode 100644 input/day10_ex2.txt create mode 100644 src/day10.rs diff --git a/input/day10.txt b/input/day10.txt new file mode 100644 index 0000000..d94ff8c --- /dev/null +++ b/input/day10.txt @@ -0,0 +1,142 @@ +addx 2 +addx 4 +noop +noop +addx 17 +noop +addx -11 +addx -1 +addx 4 +noop +noop +addx 6 +noop +noop +addx -14 +addx 19 +noop +addx 4 +noop +noop +addx 1 +addx 4 +addx -20 +addx 21 +addx -38 +noop +addx 7 +noop +addx 3 +noop +addx 22 +noop +addx -17 +addx 2 +addx 3 +noop +addx 2 +addx 3 +noop +addx 2 +addx -8 +addx 9 +addx 2 +noop +noop +addx 7 +addx 2 +addx -27 +addx -10 +noop +addx 37 +addx -34 +addx 30 +addx -29 +addx 9 +noop +addx 2 +noop +noop +noop +addx 5 +addx -4 +addx 9 +addx -2 +addx 7 +noop +noop +addx 1 +addx 4 +addx -1 +noop +addx -19 +addx -17 +noop +addx 1 +addx 4 +addx 3 +addx 11 +addx 17 +addx -23 +addx 2 +noop +addx 3 +addx 2 +addx 3 +addx 4 +addx -22 +noop +addx 27 +addx -32 +addx 14 +addx 21 +addx 2 +noop +addx -37 +noop +addx 31 +addx -26 +addx 5 +addx 2 +addx 3 +addx -2 +addx 2 +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 9 +addx -8 +addx 2 +addx 11 +addx -4 +addx 2 +addx -15 +addx -22 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx 4 +addx 19 +addx -15 +addx 1 +noop +noop +addx 6 +noop +noop +addx 5 +addx -1 +addx 5 +addx -14 +addx -13 +addx 30 +noop +addx 3 +noop +noop \ No newline at end of file diff --git a/input/day10_ex.txt b/input/day10_ex.txt new file mode 100644 index 0000000..fd7eba8 --- /dev/null +++ b/input/day10_ex.txt @@ -0,0 +1,3 @@ +noop +addx 3 +addx -5 \ No newline at end of file diff --git a/input/day10_ex2.txt b/input/day10_ex2.txt new file mode 100644 index 0000000..94cd0a8 --- /dev/null +++ b/input/day10_ex2.txt @@ -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 \ No newline at end of file diff --git a/src/day10.rs b/src/day10.rs new file mode 100644 index 0000000..9e63802 --- /dev/null +++ b/src/day10.rs @@ -0,0 +1,85 @@ +use std::ops::Add; + +fn cycles(op: &str) -> usize { + match op { + "noop" => 1, + "addx" => 2, + unk => panic!("unknown op {}", unk) + } +} + +fn op_and_args(instruction: &str) -> (&str, Option) { + let mut toks = instruction.split_whitespace(); + let op = toks.next().expect("expected an operator!"); + let arg = match op { + "noop" => None, + "addx" => { + let arg1: i32 = toks.next().expect("expected one arg for addx").parse().expect("arg for addx must be i32"); + Some(arg1) + }, + unk => panic!("unknown op {}", unk) + }; + (op,arg) +} + +fn apply(op: &str, arg: Option, x: i32) -> i32 { + match op { + "noop" => x, + "addx" => x + arg.expect("addx had no argument!"), + unk => panic!("unknown op {}", unk) + } +} + +pub fn run(input: String) { + let mut lines = input.lines().into_iter(); + // our (single) register + let mut x = 1; + let mut tick = 1; + let first_instruction: &str = lines.next().expect("expected a first instruction!"); + let (mut op, mut arg) = op_and_args(first_instruction); + let mut circ = cycles(op); + //let mut sss = 0; + let mut screen = "".to_owned(); + loop { + // Start of cycle, Load next instruction + if circ == 0 { + if let Some(next_inst) = lines.next() { + (op, arg)= op_and_args(next_inst); + circ = cycles(op); + } else { + break; + } + } + // Time passes.... + circ -= 1; + + // Pt1, signal strength + // if (tick + 20) % 40 == 0 { + // let ss = tick*x; + // //println!("tick {} x {} ss {}", tick, x, ss); + // sss += ss; + // } + + // Pt2, Draw pixels! note crt_col is one less(!) than tick + let crt_col = (tick-1) % 40; + //println!("tick {} x {} crt_col {}", tick, x, crt_col); + screen = screen.add(if crt_col >= (x-1) && crt_col <= (x+1) { + "#" + } else { + "." + }); + if crt_col == 39 { + screen = screen.add("\n"); + } + + // End of cycle, apply instruction + if circ == 0 { + x = apply(op, arg, x); + } + // increase cycle counter + tick += 1; + } + //println!("Day 10: {}",sss); + println!("Day 10:"); + print!("{}", screen); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e9ea587..31bb9ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ mod day6; mod day7; mod day8; mod day9; +mod day10; fn main() { day1::run(fs::read_to_string("input/day1.txt").expect("Failed to read input file!")); @@ -20,4 +21,5 @@ fn main() { day7::run(fs::read_to_string("input/day7.txt").expect("Failed to read input file!")); day8::run(fs::read_to_string("input/day8.txt").expect("Failed to read input file!")); day9::run(fs::read_to_string("input/day9.txt").expect("Failed to read input file!")); + day10::run(fs::read_to_string("input/day10.txt").expect("Failed to read input file!")); } -- cgit v1.2.3-ZIG