[TASK] Solved Day 10
This commit is contained in:
139
input/day10.txt
Normal file
139
input/day10.txt
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 9
|
||||||
|
addx -5
|
||||||
|
addx 1
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 6
|
||||||
|
addx -1
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -2
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx -38
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 32
|
||||||
|
addx -22
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx -2
|
||||||
|
addx 7
|
||||||
|
addx -2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -5
|
||||||
|
addx 10
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx -38
|
||||||
|
addx 1
|
||||||
|
addx 27
|
||||||
|
noop
|
||||||
|
addx -20
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 27
|
||||||
|
noop
|
||||||
|
addx -22
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -11
|
||||||
|
addx 16
|
||||||
|
addx -2
|
||||||
|
addx -17
|
||||||
|
addx 24
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx -38
|
||||||
|
addx 15
|
||||||
|
addx 10
|
||||||
|
addx -15
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 26
|
||||||
|
noop
|
||||||
|
addx -21
|
||||||
|
addx 19
|
||||||
|
addx -33
|
||||||
|
addx 19
|
||||||
|
noop
|
||||||
|
addx -6
|
||||||
|
addx 9
|
||||||
|
addx 3
|
||||||
|
addx 4
|
||||||
|
addx -21
|
||||||
|
addx 4
|
||||||
|
addx 20
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -38
|
||||||
|
addx 28
|
||||||
|
addx -21
|
||||||
|
addx 9
|
||||||
|
addx -8
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -9
|
||||||
|
addx 14
|
||||||
|
addx -2
|
||||||
|
addx -5
|
||||||
|
addx 12
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -27
|
||||||
|
addx 28
|
||||||
|
addx -36
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
noop
|
||||||
|
addx 6
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -2
|
||||||
|
addx 20
|
||||||
|
addx -10
|
||||||
|
addx -3
|
||||||
|
addx 1
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
addx 4
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx -30
|
||||||
|
noop
|
||||||
146
input/day10_example.txt
Normal file
146
input/day10_example.txt
Normal 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
|
||||||
100
src/day10.rs
Normal file
100
src/day10.rs
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
|
||||||
|
use crate::day_solver::DaySolver;
|
||||||
|
|
||||||
|
use super::util;
|
||||||
|
|
||||||
|
pub struct Day10 {
|
||||||
|
instructions: Vec<Instruction>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Day10 {
|
||||||
|
|
||||||
|
pub fn create() -> Self {
|
||||||
|
// let lines = util::read_file("input/day10_example.txt");
|
||||||
|
let lines = util::read_file("input/day10.txt");
|
||||||
|
|
||||||
|
// Put the input into the day struct
|
||||||
|
return Day10 {
|
||||||
|
instructions: lines.iter()
|
||||||
|
.map(|s| {
|
||||||
|
if s == "noop" {
|
||||||
|
Instruction::Noop
|
||||||
|
} else if s.starts_with("addx") {
|
||||||
|
Instruction::Addx(s.split_whitespace().last().unwrap().parse::<i32>().unwrap())
|
||||||
|
} else {
|
||||||
|
panic!("Unknown instruction {}", s)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DaySolver for Day10 {
|
||||||
|
|
||||||
|
|
||||||
|
fn solve_part1(&mut self) -> String {
|
||||||
|
|
||||||
|
let mut x = 1;
|
||||||
|
let mut cycle = 1;
|
||||||
|
let mut interesting_cycle_sum = 0;
|
||||||
|
for instr in &self.instructions {
|
||||||
|
|
||||||
|
for _ in 0..instr.cycles() {
|
||||||
|
if (cycle + 20) % 40 == 0 {
|
||||||
|
// println!("{} * {} = {}", x, cycle, x * cycle);
|
||||||
|
interesting_cycle_sum += x * cycle;
|
||||||
|
}
|
||||||
|
cycle += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Instruction::Addx(addx) = instr {
|
||||||
|
x += addx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return interesting_cycle_sum.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_part2(&mut self) -> String {
|
||||||
|
|
||||||
|
let mut x: i32 = 1;
|
||||||
|
let mut cycle = 1;
|
||||||
|
let mut res: String = String::from("\n");
|
||||||
|
for instr in &self.instructions {
|
||||||
|
|
||||||
|
for _ in 0..instr.cycles() {
|
||||||
|
if x.abs_diff((cycle - 1) % 40) <= 1 {
|
||||||
|
res += "#";
|
||||||
|
} else {
|
||||||
|
res += "."
|
||||||
|
}
|
||||||
|
if cycle % 40 == 0 {
|
||||||
|
res += "\n";
|
||||||
|
}
|
||||||
|
cycle += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Instruction::Addx(addx) = instr {
|
||||||
|
x += addx;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
enum Instruction {
|
||||||
|
Noop,
|
||||||
|
Addx(i32)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Instruction {
|
||||||
|
|
||||||
|
fn cycles(&self) -> u8 {
|
||||||
|
match self {
|
||||||
|
Self::Addx(_) => 2,
|
||||||
|
_ => 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ use crate::day6::Day6;
|
|||||||
use crate::day7::Day7;
|
use crate::day7::Day7;
|
||||||
use crate::day8::Day8;
|
use crate::day8::Day8;
|
||||||
use crate::day9::Day9;
|
use crate::day9::Day9;
|
||||||
|
use crate::day10::Day10;
|
||||||
use crate::day_solver::DaySolver;
|
use crate::day_solver::DaySolver;
|
||||||
|
|
||||||
mod util;
|
mod util;
|
||||||
@@ -21,6 +22,7 @@ mod day6;
|
|||||||
mod day7;
|
mod day7;
|
||||||
mod day8;
|
mod day8;
|
||||||
mod day9;
|
mod day9;
|
||||||
|
mod day10;
|
||||||
|
|
||||||
const MAX_DAY: u8 = 9;
|
const MAX_DAY: u8 = 9;
|
||||||
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
||||||
@@ -95,6 +97,7 @@ fn build_day_solver(day: u8) -> Option<Box<dyn DaySolver>> {
|
|||||||
7 => Some(Box::new(Day7::create())),
|
7 => Some(Box::new(Day7::create())),
|
||||||
8 => Some(Box::new(Day8::create())),
|
8 => Some(Box::new(Day8::create())),
|
||||||
9 => Some(Box::new(Day9::create())),
|
9 => Some(Box::new(Day9::create())),
|
||||||
|
10 => Some(Box::new(Day10::create())),
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user