Day 10. I'm a bit behind. CA trip got in the way

master
Dustin Swan 2 years ago
parent 7fc1ca6ed4
commit 4b3a9aaa7e
Signed by: dustinswan
GPG Key ID: AB49BD6B2B3A6377

7
day10/Cargo.lock generated

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day10"
version = "0.1.0"

@ -0,0 +1,8 @@
[package]
name = "day10"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,144 @@
noop
noop
noop
addx 6
addx -1
addx 5
noop
noop
noop
addx 5
addx -8
addx 9
addx 3
addx 2
addx 4
addx 3
noop
addx 2
noop
addx 1
addx 6
noop
noop
noop
addx -39
noop
addx 5
addx 2
addx -2
addx 3
addx 2
addx 5
addx 2
addx 2
addx 13
addx -12
noop
addx 7
noop
addx 2
addx 3
noop
addx -25
addx 30
addx -10
addx 13
addx -40
noop
addx 5
addx 2
addx 3
noop
addx 2
addx 3
addx -2
addx 3
addx -1
addx 7
noop
noop
addx 5
addx -1
addx 6
noop
noop
noop
noop
addx 9
noop
addx -1
noop
addx -39
addx 2
addx 33
addx -29
addx 1
noop
addx 4
noop
noop
noop
addx 3
addx 2
noop
addx 3
noop
noop
addx 7
addx 2
addx 3
addx -2
noop
addx -30
noop
addx 40
addx -2
addx -38
noop
noop
noop
addx 5
addx 5
addx 2
addx -9
addx 5
addx 7
addx 2
addx 5
addx -18
addx 28
addx -7
addx 2
addx 5
addx -28
addx 34
addx -3
noop
addx 3
addx -38
addx 10
addx -3
addx 29
addx -28
addx 2
noop
noop
noop
addx 5
noop
addx 3
addx 2
addx 7
noop
addx -2
addx 5
addx 2
noop
addx 1
addx 5
noop
noop
addx -25
noop
noop

@ -0,0 +1,67 @@
#[derive(Debug)]
enum Instruction {
Noop,
AddX(i32),
}
fn main() {
let contents = std::fs::read_to_string("data.txt").expect("Failed to read file");
let ops = contents.lines().map(parse).collect::<Vec<Instruction>>();
let strength = run(&ops);
println!("total signal strength {:?}", strength);
}
fn parse(line: &str) -> Instruction {
let parts = line.split(" ").collect::<Vec<&str>>();
match parts[0] {
"noop" => Instruction::Noop,
_ => Instruction::AddX(parts[1].parse().unwrap()),
}
}
fn run(ops: &Vec<Instruction>) -> i32 {
let mut x: i32 = 1;
let mut cycle = 1;
let mut to_add = None;
let mut op_idx = 0;
let mut strength = 0;
while op_idx < ops.len() {
let pixel = cycle % 40;
let diff = (x + 1 - pixel).abs();
if diff.abs() <= 1 {
print!("#");
} else {
print!(" ");
}
match to_add {
None => {
match ops[op_idx] {
Instruction::Noop => (),
Instruction::AddX(v) => {
to_add = Some(v);
}
}
op_idx += 1;
}
Some(v) => {
x += v;
to_add = None;
}
}
if (cycle + 20) % 40 == 0 {
strength += x * cycle;
}
if cycle % 40 == 0 {
println!();
}
cycle += 1;
}
strength
}

@ -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
Loading…
Cancel
Save