day 10 done
This commit is contained in:
7
day10/Cargo.lock
generated
Normal file
7
day10/Cargo.lock
generated
Normal file
@@ -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"
|
||||||
8
day10/Cargo.toml
Normal file
8
day10/Cargo.toml
Normal file
@@ -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]
|
||||||
138
day10/input
Normal file
138
day10/input
Normal file
@@ -0,0 +1,138 @@
|
|||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 5
|
||||||
|
addx -38
|
||||||
|
addx 7
|
||||||
|
addx 10
|
||||||
|
addx -14
|
||||||
|
addx 5
|
||||||
|
addx 30
|
||||||
|
addx -25
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 2
|
||||||
|
addx -21
|
||||||
|
addx 22
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx -39
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 5
|
||||||
|
addx 4
|
||||||
|
addx -5
|
||||||
|
addx 4
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx -9
|
||||||
|
addx 12
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -1
|
||||||
|
addx 6
|
||||||
|
addx -2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
addx -37
|
||||||
|
addx 39
|
||||||
|
addx -33
|
||||||
|
addx -1
|
||||||
|
addx 1
|
||||||
|
addx 8
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 20
|
||||||
|
addx -19
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 4
|
||||||
|
addx -21
|
||||||
|
addx 22
|
||||||
|
addx -38
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx 32
|
||||||
|
addx -27
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 2
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx -39
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 4
|
||||||
|
addx 8
|
||||||
|
addx -8
|
||||||
|
addx 6
|
||||||
|
addx -1
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -11
|
||||||
|
addx 12
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx -6
|
||||||
|
noop
|
||||||
46
day10/src/main.rs
Normal file
46
day10/src/main.rs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::{prelude::*, BufReader};
|
||||||
|
use std::str;
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
//Read in file
|
||||||
|
let file = File::open("input")?;
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
|
||||||
|
let mut part1: Vec<i32> = Vec::new();
|
||||||
|
let mut part2 = [[b'.'; 40]; 6];
|
||||||
|
let mut x_reg = 1;
|
||||||
|
let mut pc = 0;
|
||||||
|
|
||||||
|
reader.lines().for_each(|line| {
|
||||||
|
let line = line.unwrap();
|
||||||
|
let op = match line.split(' ').collect::<Vec<_>>()[..] {
|
||||||
|
["addx", x] => Some(x.parse::<i32>().unwrap()),
|
||||||
|
["noop"] => None,
|
||||||
|
_ => panic!("invalid command: {}", line),
|
||||||
|
};
|
||||||
|
let steps = if op.is_some() { 2 } else { 1 };
|
||||||
|
for i in 0..steps {
|
||||||
|
let row = pc / 40;
|
||||||
|
let col = pc % 40;
|
||||||
|
if col - 1 == x_reg || col == x_reg || col + 1 == x_reg {
|
||||||
|
part2[row as usize][col as usize] = b'#';
|
||||||
|
}
|
||||||
|
pc += 1;
|
||||||
|
if pc < 221 && (pc - 20) % 40 == 0 {
|
||||||
|
part1.push(pc * x_reg);
|
||||||
|
}
|
||||||
|
if i == steps - 1 {
|
||||||
|
if let Some(x) = op {
|
||||||
|
x_reg += x;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
println!("Part 1: {}", part1.iter().sum::<i32>());
|
||||||
|
for row in part2 {
|
||||||
|
println!("Part 2: {}", str::from_utf8(&row).unwrap());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user