My attempts to solve puzzles of Advent of Code
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(2022): day10

+321 -2
+148
data/day10.txt
··· 1 + addx 1 2 + noop 3 + addx 2 4 + addx 11 5 + addx -4 6 + noop 7 + noop 8 + noop 9 + noop 10 + addx 3 11 + addx -3 12 + addx 10 13 + addx 1 14 + noop 15 + addx 12 16 + addx -8 17 + addx 5 18 + noop 19 + noop 20 + addx 1 21 + addx 4 22 + addx -12 23 + noop 24 + addx -25 25 + addx 14 26 + addx -7 27 + noop 28 + addx 11 29 + noop 30 + addx -6 31 + addx 3 32 + noop 33 + addx 2 34 + addx 22 35 + addx -12 36 + addx -17 37 + addx 15 38 + addx 2 39 + addx 10 40 + addx -9 41 + noop 42 + noop 43 + noop 44 + addx 5 45 + addx 2 46 + addx -33 47 + noop 48 + noop 49 + noop 50 + noop 51 + addx 12 52 + addx -9 53 + addx 7 54 + noop 55 + noop 56 + addx 3 57 + addx -2 58 + addx 2 59 + addx 26 60 + addx -31 61 + addx 14 62 + addx 3 63 + noop 64 + addx 13 65 + addx -1 66 + noop 67 + addx -5 68 + addx -13 69 + addx 14 70 + noop 71 + addx -20 72 + addx -15 73 + noop 74 + addx 7 75 + noop 76 + addx 31 77 + noop 78 + addx -26 79 + noop 80 + noop 81 + noop 82 + addx 5 83 + addx 20 84 + addx -11 85 + addx -3 86 + addx 9 87 + addx -5 88 + addx 2 89 + noop 90 + addx 4 91 + noop 92 + addx 4 93 + noop 94 + noop 95 + addx -7 96 + addx -30 97 + noop 98 + addx 7 99 + noop 100 + noop 101 + addx -2 102 + addx -4 103 + addx 11 104 + addx 14 105 + addx -9 106 + addx -2 107 + noop 108 + addx 7 109 + noop 110 + addx -11 111 + addx -5 112 + addx 19 113 + addx 5 114 + addx 2 115 + addx 5 116 + noop 117 + noop 118 + addx -2 119 + addx -27 120 + addx -6 121 + addx 1 122 + noop 123 + noop 124 + addx 4 125 + addx 1 126 + addx 4 127 + addx 5 128 + noop 129 + noop 130 + noop 131 + addx 1 132 + noop 133 + addx 4 134 + addx 1 135 + noop 136 + noop 137 + addx 5 138 + noop 139 + noop 140 + addx 4 141 + addx 1 142 + noop 143 + addx 4 144 + addx 1 145 + noop 146 + noop 147 + noop 148 + noop
+2 -2
src/main.rs
··· 1 - use crate::solutions::year_2022::{Day2, Day3, Day4, Day5, Day6, Day7, Day8, Day9}; 1 + use crate::solutions::year_2022::{Day2, Day3, Day4, Day5, Day6, Day7, Day8, Day9, Day10}; 2 2 use aoc_lib::Solver; 3 3 pub mod solutions; 4 4 5 5 fn main() { 6 - let sol = Day9::solve_part2(); 6 + let sol = Day10::solve_part2(); 7 7 println!("{:?}", sol); 8 8 }
+169
src/solutions/year_2022/day10.rs
··· 1 + use std::fmt::Debug; 2 + 3 + use aoc_lib::Solver; 4 + 5 + #[derive(Debug, Default)] 6 + pub struct Day10 { 7 + cycles: Vec<(i32, i32, i32)>, 8 + } 9 + 10 + #[derive(Clone, Default)] 11 + struct Crt { 12 + rows: Vec<PixelType>, 13 + current_sprite_pos: usize, 14 + } 15 + 16 + #[derive(Clone, Copy)] 17 + enum PixelType { 18 + Lit, 19 + Dark, 20 + } 21 + 22 + impl Solver for Day10 { 23 + fn day() -> u8 { 24 + 10 25 + } 26 + fn solution_part1(input: aoc_lib::Input) -> Option<Self::OutputPart1> { 27 + let mut d = Day10::default(); 28 + d.load_instructions(input.lines.iter().map(Instruction::from), |_, _| {}); 29 + 30 + let s = [20, 60, 100, 140, 180, 220] 31 + .map(|i| (i, d.cycles[i - 1])) 32 + .iter() 33 + .map(|(n, (_, during, _))| *n as i32 * during) 34 + .sum::<i32>(); 35 + Some(s) 36 + } 37 + 38 + fn solution_part2(input: aoc_lib::Input) -> Option<Self::OutputPart2> { 39 + let mut crt = Crt::default(); 40 + let mut d = Day10::default(); 41 + d.load_instructions(input.lines.iter().map(Instruction::from), |x, y| { 42 + crt.handle_cyle(x, y); 43 + }); 44 + 45 + println!("{:?}", crt.clone()); 46 + Some(()) 47 + } 48 + 49 + type OutputPart1 = i32; 50 + type OutputPart2 = (); 51 + } 52 + 53 + impl From<PixelType> for usize { 54 + fn from(val: PixelType) -> Self { 55 + match val { 56 + PixelType::Lit => 1, 57 + PixelType::Dark => 0, 58 + } 59 + } 60 + } 61 + 62 + impl From<PixelType> for String { 63 + fn from(p: PixelType) -> Self { 64 + match p { 65 + PixelType::Lit => "#".to_string(), 66 + PixelType::Dark => ".".to_string(), 67 + } 68 + } 69 + } 70 + 71 + impl Crt { 72 + fn handle_cyle(&mut self, register_val_after: i32, register_val_during: i32) { 73 + let crt_draw_pos = self.rows.iter().enumerate().last().map(|(index, _)| index); 74 + 75 + let sprite_pos = [ 76 + (register_val_during as usize % 40).saturating_sub(1), 77 + register_val_during as usize % 40, 78 + register_val_during as usize % 40 + 1, 79 + ]; 80 + let next_crt_pos = crt_draw_pos.map(|p| p % 40 + 1).unwrap_or(0); 81 + let should_lit = sprite_pos.contains(&next_crt_pos); 82 + 83 + if should_lit { 84 + self.rows.push(PixelType::Lit); 85 + } else { 86 + self.rows.push(PixelType::Dark) 87 + } 88 + self.current_sprite_pos = register_val_after as usize; 89 + } 90 + } 91 + 92 + impl Debug for Crt { 93 + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { 94 + self.rows 95 + .chunks(40) 96 + .map(|r| r.iter().map(|x| String::from(*x))) 97 + .for_each(|x| { 98 + f.write_str(&x.collect::<Vec<_>>().join(" ")).expect(""); 99 + f.write_str("\n").expect("") 100 + }); 101 + Ok(()) 102 + } 103 + } 104 + 105 + impl Day10 { 106 + fn load_instructions<F, G>(&mut self, instructions: F, mut cycle_callback: G) -> &mut Self 107 + where 108 + F: Iterator<Item = Instruction>, 109 + G: FnMut(i32, i32), 110 + { 111 + instructions.for_each(|instr| match instr { 112 + Instruction::Noop => { 113 + if self.cycles.is_empty() { 114 + self.cycles.push((0, 1, 1)); 115 + cycle_callback(1, 0); 116 + } else { 117 + let (_, _, after) = self 118 + .cycles 119 + .last() 120 + .expect("expected some value in the register"); 121 + let after = *after; 122 + self.cycles.push((after, after, after)); 123 + cycle_callback(after, after); 124 + } 125 + } 126 + Instruction::AddX(a) => { 127 + if self.cycles.is_empty() { 128 + self.cycles.push((0, 1, 1)); 129 + cycle_callback(1, 0); 130 + self.cycles.push((1, 1, 1 + a)); 131 + cycle_callback(1 + a, 1); 132 + } else { 133 + let cycles_1 = self.cycles.clone(); 134 + let (_, _, after) = cycles_1 135 + .last() 136 + .expect("expected some value in the register"); 137 + 138 + self.cycles.push((*after, *after, *after)); 139 + cycle_callback(*after, *after); 140 + self.cycles.push((*after, *after, after + a)); 141 + cycle_callback(*after + a, *after); 142 + } 143 + } 144 + }); 145 + self 146 + } 147 + } 148 + 149 + 150 + #[derive(Debug)] 151 + enum Instruction { 152 + AddX(i32), 153 + Noop, 154 + } 155 + 156 + impl From<&String> for Instruction { 157 + fn from(s: &String) -> Self { 158 + let xs = s.split(' ').collect::<Vec<_>>(); 159 + match *xs.first().expect("expecting a string") { 160 + "addx" => Self::AddX( 161 + xs.get(1) 162 + .map(|x| x.parse::<i32>().expect("expecting a number")) 163 + .expect("not possible"), 164 + ), 165 + "noop" => Self::Noop, 166 + _ => panic!("not possible"), 167 + } 168 + } 169 + }
+2
src/solutions/year_2022/mod.rs
··· 7 7 mod day7; 8 8 mod day8; 9 9 mod day9; 10 + mod day10; 10 11 pub(crate) use day1::*; 11 12 pub(crate) use day2::*; 12 13 pub(crate) use day3::*; ··· 16 17 pub(crate) use day7::*; 17 18 pub(crate) use day8::*; 18 19 pub(crate) use day9::*; 20 + pub(crate) use day10::*;