1
0
Fork 0

day10 solution

main
Andrew Coleman 2022-12-10 02:32:57 -05:00
parent 34ab94f5f0
commit 920945061f
5 changed files with 350 additions and 0 deletions

143
2022/day10.txt Normal file
View File

@ -0,0 +1,143 @@
noop
noop
addx 5
noop
noop
addx 6
addx 4
addx -4
addx 4
addx -6
addx 11
addx -1
addx 2
addx 4
addx 3
noop
addx 2
addx -30
addx 2
addx 33
noop
addx -37
noop
noop
noop
addx 3
addx 2
addx 5
addx 20
addx 7
addx -24
addx 2
noop
addx 7
addx -2
addx -6
addx 13
addx 3
addx -2
addx 2
noop
addx -5
addx 10
addx 5
addx -39
addx 1
addx 5
noop
addx 3
noop
addx -5
addx 10
addx -2
addx 2
noop
noop
addx 7
noop
noop
noop
noop
addx 3
noop
addx 3
addx 2
addx 8
addx -1
addx -20
addx 21
addx -38
addx 5
addx 2
noop
noop
noop
addx 8
noop
noop
addx -2
addx 2
addx -7
addx 14
addx 5
noop
noop
noop
addx -16
addx 17
addx 2
addx -12
addx 19
noop
noop
addx -37
noop
noop
noop
addx 3
addx 2
addx 2
addx 5
addx 20
addx -19
addx 2
noop
noop
noop
addx 5
addx 19
addx -12
addx 3
addx -2
addx 2
addx -18
addx 25
addx -14
addx -22
addx 1
noop
noop
noop
addx 3
addx 5
addx -4
addx 7
addx 4
noop
addx 1
noop
noop
addx 2
addx -6
addx 15
addx -1
addx 4
noop
noop
addx 1
addx 4
addx -33
noop
addx 21
noop

57
2022/src/days/day10.rs Normal file
View File

@ -0,0 +1,57 @@
use anyhow::{anyhow, Result};
use std::fs;
pub fn run() -> Result<()> {
#[cfg(not(feature = "test_input"))]
let file_contents = fs::read_to_string("day10.txt")?;
#[cfg(feature = "test_input")]
let file_contents = fs::read_to_string("tests/day10.txt")?;
let mut regx: Vec<i32> = Vec::with_capacity(250);
regx.push(1);
for line in file_contents.lines() {
regx.push(0);
match &line[0..4] {
"noop" => {}
"addx" => {
let count: i32 = line[5..].parse().expect("invalid numeric value");
regx.push(count);
}
_ => {
return Err(anyhow!("unknown op code"));
}
}
}
let sum_20: i32 = regx.iter().take(20).sum();
let sum_60: i32 = regx.iter().take(60).sum();
let sum_100: i32 = regx.iter().take(100).sum();
let sum_140: i32 = regx.iter().take(140).sum();
let sum_180: i32 = regx.iter().take(180).sum();
let sum_220: i32 = regx.iter().take(220).sum();
let part1 =
sum_20 * 20 + sum_60 * 60 + sum_100 * 100 + sum_140 * 140 + sum_180 * 180 + sum_220 * 220;
println!("part1 {}", part1);
let mut cycle = 0;
let mut sprite = 1;
for reg_val in regx.iter().skip(1) {
if cycle > 0 && cycle % 40 == 0 {
println!();
}
if sprite >= (cycle % 40) - 1 && sprite <= (cycle % 40) + 1 {
print!("#");
} else {
print!(".");
}
sprite += reg_val;
cycle += 1;
}
println!();
Ok(())
}

View File

@ -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;

View File

@ -39,6 +39,9 @@ fn run_day(number: i32) -> Result<()> {
9 => { 9 => {
days::day9::run()?; days::day9::run()?;
} }
10 => {
days::day10::run()?;
}
_ => return Err(anyhow!("Invalid day provided")), _ => return Err(anyhow!("Invalid day provided")),
} }
Ok(()) Ok(())

146
2022/tests/day10.txt Normal file
View File

@ -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