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(2023): day 3

Signed-off-by: Kaushik Chakraborty <git@kaushikc.org>

+382 -1
+140
data/2023/day3.txt
··· 1 + ................458...689.556..3............197......582........720.........................515..352..286.........670.741.....895.626....... 2 + ...910.743..........................13..........................*.............775...956........@.........*................971.-............. 3 + ....*......406.507.97..846..............968+.........253........730...574............#....308......*.....798..............*.......894....... 4 + ....555...............*......%...............980.+43..=..239..........*......495................638.111.........*490...124...*........576... 5 + .............807......176.....820...=.........*.........@................144*...........324..............82..745............775............. 6 + ...509.../..../...85/......9.........574....744....586....../..796................763...............................759............#........ 7 + .....*...541...................930%................*.....831............494...44........799.....................870*.......834.215.675...... 8 + .....23..................391.........17.......500..329.................+.....*......................431.....................-...........19.. 9 + ........................#.............*.704+./..................................152............=61....................668..............*.... 10 + ........894....334.............766.817.........302.-367.663......670.....+..........628..............592..................652......130..885. 11 + ....228...........*...183..................169*...........*...............836..........*...230............45.....$....200....#.......&...... 12 + .......*........920..*....&........................865........60...=..........901-.....766.*...815..........*....410...*...............227.. 13 + .....#.623..........183...28.....460....861......./............*....151....................759...*......341..84......928....906..470........ 14 + ..148........636................*........&....376...387=......187............741.308.............413.......................*.......*........ 15 + ...............-.158........362..220.......+.........................$..553..*.....%...........*.........359=......373....706.518$..274..... 16 + .....688..279........441......+........320..596.............../.#...989...*....%.....108....139.216...........-...*......................... 17 + ......*......*399.......*.506.........*....................282..344......610...462..%...............876.....287...230......$.......#633..... 18 + ...221...111.........235....*...%377.319.............503.......................................402...+...=...............224.............980 19 + ............*.............992...........................*..........945.891.................312*........747...........-...................... 20 + .........350.........................147....957.314.183.483...........*.....................................613#.....880.........$177....... 21 + .....................146*409..........*.....................-................392......892.....981...246.437......790........................ 22 + ......440..@860...............205*421..791.................652...456..$........................-....*.......565...%......140....511......... 23 + .......*.............762.........................513.....#........*...596..656*.....959............292.476..............*........*.......... 24 + .....276.............&...167....108.819...............845.........950..........577.*..........533.........+...........323.......12.......... 25 + .........................*.......*...*.......348...........570.........998..........308..........*.765.......................76......558.897 26 + ............530....992....512...560...755......*..569.........#...................%.....461...896.....&...813......763..............*....... 27 + 424..........+....*.....*.....@...............922.....................445.99......88.......................=...803....*..14.440....139..349. 28 + ...*............155..417.911.383.........434.......+...345.763..279...#.....*6...................................&.814....*..#.............. 29 + ...147.............................762...+........856..*....*....#..#............82..%.........=.......*................619................. 30 + .......#668..........88.....................-..........448.710......25.427........&.740....*...44.608.809........%..................119..... 31 + ..................*........&....../........86.....724.....................*...924..........240....$.......935..945.....545.641.......@...... 32 + .....578.......565.749...342....338..................*786...305.....315..627.+........924.............+..................................... 33 + ........*135.................@......786.........911........*......*..............193.@...........862..337.......879.......992..681.598...207 34 + ..........................646..........*105.......*......682...683.53.849..750...*.......+......*...............*...787*..........*......... 35 + ............899......230.....................792...779....................@......918..955...731..233...........496..................109..... 36 + ...905.........*.......*....../...550*675...*..........652..........$581.......................*..........................$.....204...*..... 37 + ....*...146.171........162.944.............613...34.........563.321......648.................278.....69............519.....558.+.....573.... 38 + ..134....*.........260............815*20..............286....*.....*....*.....100...261.....................=850....*....................... 39 + ......872.........+.....................................*..971..669....733...$.........*..........610............983..801=..........153..... 40 + 601.........484@.........109..........317..............424............................298.........*...976.....*............................. 41 + ...*....................*............*....316.201...........935*................................22...&.......137...336.....557........4..... 42 + ..745.........286.....989..........582.......*..................277...795..........335.................................520*............*.... 43 + .............=....&.............85..................=...19...................281+..@.............940.+970....318.................281..216... 44 + ................361................178../.........725.......*.........548..................*190...*..........$.....%.....&..........*....... 45 + ..57*......131*.........436*375.....#...168..............305.435........*...........674..........245....$.......$...404.270......812..707... 46 + .....440.......318.525.........................*.........................540....799...*................344...432.......................*.... 47 + ............$......*....526...730..142......927.785............................&.....509.......45................849*.......276......993.... 48 + ..362*541...187....393........../...........................365...........442.................*....105....*..........256.................... 49 + ........................568.......&.98.........+.=427./718..*......500#......#............692.547.&....753........=......................... 50 + .........652@...........*.......836.&........333...........144.................*...33....#..................593.540.....968..822.....450.... 51 + ..............518....442............................521.............884.303.85.483................594.......*..........@......*.......*..... 52 + .......278......*........674..............844...435*.................*..-...&.............837......*.....333..#849..........607.......177... 53 + .........&.......302........*........*519....*...............270....174....................*......579................66#..........425....... 54 + ..............*......*......205...833.........672...............*........269.440...771.....446.................=.*...............*.......... 55 + .134.527.....14...910.845.....................................113..-....*......*..*..........................579.171.........287..828....... 56 + ....*.....................726.942..83..........264.752..............188.765..359...182................380..............-.....*...........734 57 + ..........................*........#.............$.........322..............................*...........$....@830.....989.246...&........... 58 + .....*260..452.325.................................................246...................466.915..............................-..417....&558 59 + ..961........%...........#....203*690..659..@.....424*..............*.............*.................797.363...$813...........340............ 60 + ........................260................608........565.......@....856.......576.159.....691..23.....*................*562.........373.... 61 + ......235.962..311-................*....#......218...........129............@..............@......*354................33................*... 62 + 649..*....&..........140......910..613.544..........420.............903*....825...............................139.819............595........ 63 + .....349......318.....$..746...*.............264......$..&...607........879................................+.....*...............&.......... 64 + ...........$......259....*......609.............*.......619.....*.............#...68.45*...560*723..........171....12=....277.........563... 65 + ...39...428.......*.......551...................962...=..........620........776....*.......................................*..626.......*... 66 + ...&..............370.#.......*807..103...820.........842...562*.................646..48....%..412...........684*754....143..*.............. 67 + ............866.......77...225.............&......152...........241.........628=........*.462....%.926........................990..439...... 68 + .352.............................&615........633+...*............................996..388..........*...516..............107%..........*743.. 69 + ....*...............970.......%...................564........................99..../........*790..239.*......94....688...................... 70 + .211.....342+.........*....521...............788...............+........839............+.265...........486................637......+...@.... 71 + .....................82..............187...#....*731..230.......50...................480.......8.................................507....660. 72 + ...103..762*436.523*.......................250.........*...#...................................*...518.916...........................$...... 73 + ...*................439....553....................479.466.553............699#..3.$...........695.......*....................=.......923.714. 74 + ..867.+971....45...........*..........669.....885................605.272......#...175...836...........15...................257..144......... 75 + ............................579.......#.............................*.....877..........*.......330........141=.............................. 76 + .................................881......98..400.517....................*....453......509........*..................................879.... 77 + ....99.........300.882.....670....*............*...................712.119...*................473..290...630..........510......@.....*...... 78 + .../.....979...../....*959..+.....189........123...........402......&......394....329...........%.........*.......208*.......583...785...113 79 + ...................&............*........402......369..521........................*........314.............7.............564................ 80 + ...............555.430.......630.854........%..........+......676...........980+.........................................*...............849 81 + ...........531..=.......................*......139............-..........................5..969=....338.........464.......508.......843..... 82 + .............*.......118.............999.915.....*.&.....................755.........501*.............-....2.........766........3......*792. 83 + ...............@706..&...258.#501.............442...469..........332.327.*....@..............................*..........*...661..*.......... 84 + ...955.....900......................#.....986..............705%.*.....-..884..290...177*531........632......802...936..39.....$...952....... 85 + ..........@...........483........553..544..#......920............340.............................../.................*.................396.. 86 + ...149................*..............#................446....405.......555...........................98....313........834.#.....%...../..... 87 + ....*...409...266...29.......283..............=................*........*............%830....748....*......*..............457...592......... 88 + ...310.*......./....................*......138...197..........169...308.387..923............*........598....50...980........................ 89 + ........178.............236......865.134...........*.436..772...................%..688.&.....850.66.........................796*897....679.. 90 + ................170.......=.784..........$463...871..*.....*...338.........16........*.876.......*....................368*..........#...*... 91 + ...........270.....*682.......+.....................129..802....*...505......*822.834.........575.....309....@.577........6......926..632... 92 + .....448.....@...*................105..960.480..................293.*....................598......$...*....686....*..759....13.............. 93 + .........535...507.........@.........@....*............390..........42.........919....-........928..584.............%........*.............. 94 + ....12#.................661.....535.............397........................*.....*.....788...................936....................314..... 95 + ................................./.........417......528..492.......202..654.143.476.73.....796........282....*........317..............-.... 96 + .....511....................396.............*.............#.........................%.............&........861........*....919..954......... 97 + ......*............704*853........396.772...407.....499......$.....393.....-317.648..........262.72..............482.418.....-.......661*466 98 + ...387...300...................+....=../........688*........895.2...*................/........*.......25.......................268.......... 99 + ............+................253.........16.......................665.......936..451.222..299.498.....*......*669.....652........*.218...... 100 + .................49....43..........567...*............%...226.........188.....*...$.........*......510....717.....$53..*......474....*...... 101 + ...........#..........................#...906...206..479...#.............*.599.............598.........................904..&.....808....... 102 + .........876...............295..................-.................998...34.........=348........499.81...........@..........681.............. 103 + ....452........407.350.......*..@971................721............*....................129..-...#.............508....744................... 104 + .......*17.....*.......739.136........................%.........635.........310.487....*.....746.....964.....+...........*.....639.......... 105 + ...........314..471.............................768.................640.......=.../..387.............*....420.............975...-..230...533 106 + ..............*.......389.........&.372........%.......537.....799.....*.................678....661...531......460..................-....... 107 + ............97.........=....180.943...*...=.......................*....424..................#.....*...........*..............$.............. 108 + ...803..............................529....264...............$...810..........810..................183......429...730*52.709..896..696*481.. 109 + ......*82.570-............$.......%...................754...528..........+399....*893.....176*............................%................. 110 + ........................731.......679...........110*.....*........749.........................325.803*.....130..................+.820*541... 111 + ......739*.....707..........@577......4....210......21.396..........*..........822.930................689......250.749.774...539............ 112 + ..........113.........................*......-..477.........112.....881......$....*.....$....$...........................*.................. 113 + .....+...............................809..........*........................459.......496...354....979...231....31.....-..541...168...708.... 114 + .....954..........642...267........*.....453......812...993............................../.......*........*..........267..........-....@.... 115 + .............9.........$.........307......*...364.........*..266.757/...800......805*197.453...758.617.....390................329........... 116 + ..............*954...................*...358..*..........859...*........*....848...................*.....-...............+....*...43.671.... 117 + .........107........596...*.......941........555.....378......824.348...207.....*....469/..352*..........474...........961.848...*......*... 118 + ............*......*....659..............+86........*.............%...........898..............307...............824.............735...584.. 119 + ....540....734...463...........374..............204.........633*......................................627.......%.......%...%............... 120 + ....*................727..606............752.......+............522........@.............187&...$..89.+................311.384.............. 121 + .........368*901...................120........308......783..................361................901..........770.163.............348...278... 122 + .........................10..874....*...................*................................826.......*440.=.....#.*...631...........*......... 123 + ..........145...........*.......*..665..567....&.........486.........%869......679........*.....369......195....890.*...4........100........ 124 + ......664....@...314.....49....859.....%.......8.............971..........905.....&......595........................742...............340... 125 + ........*................................852........120.......*....80....*...................823.....%.........476.........893.............. 126 + ........748.178.50*479........863....=.$..............*...........*....808...495...305......*.....684...47............./......*634.......... 127 + .....#........*.........&.....$.....83..674.571....575...........756...............*....*....546........-.....*124...199.758................ 128 + ...346..937..70.........569..................@.........12....=...............-...742....694................405...........*.......$.......... 129 + ...................................617...........958....*.778........540+.293.............................................502.834.....537... 130 + ...#.......47................908....*..............*.622.......................980.390#......785.104....................-................... 131 + ....261....*.......756..............555...%......893...............810..........-........804...*...$.942........739..773.................... 132 + .........390...765.......655......&......497.........885.....2.....$.................89../....636......*.862.......%................717..... 133 + .......2........*...897....@....354..+...........959...*....@......................@........*.......576..@.............698.................. 134 + ...............307..../.$.............100..........*...324....*.821....377..........939..134.625.......................*.................... 135 + .........................556.....................63........307...$.....*......=.....................462.....427.........822............681.. 136 + ....................+........................................................864..........8.....550*............665...........#...956..*.... 137 + .780.......864.......435......811....760.................452.........................................&..886......*...........792..@.....245. 138 + ....*.........../............*..............661%.213........=.........310/.726.....739...791...387.123............924...............281..... 139 + ..139..........45......#...316....=................*..........678*..........*..@10..*......*..*.........*...61-......................*...... 140 + .....................815.........786................186...........640......388.......415....4..36.....894.........................303.......
+1 -1
src/main.rs
··· 3 3 pub mod solutions; 4 4 5 5 fn main() { 6 - let sol = solve!(2023; 2; 2); 6 + let sol = solve!(2023; 3; 2); 7 7 println!("{:?}", sol); 8 8 }
+239
src/solutions/year_2023/day3.rs
··· 1 + use std::{collections::HashSet, str::Chars}; 2 + 3 + use aoc_lib::{create_solution, Input, Solver}; 4 + 5 + #[derive(Debug, Clone)] 6 + pub struct Day3 { 7 + engine_schematic: EngineSchematic, 8 + part_numbers: Vec<u64>, 9 + } 10 + 11 + #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Hash)] 12 + struct Point { 13 + x: usize, 14 + y: usize, 15 + } 16 + 17 + impl Point { 18 + fn neighbors(&self) -> HashSet<Self> { 19 + HashSet::from([ 20 + Point { 21 + x: self.x.saturating_sub(1), 22 + y: self.y.saturating_sub(1), 23 + }, 24 + Point { 25 + x: self.x.saturating_sub(1), 26 + y: self.y, 27 + }, 28 + Point { 29 + x: self.x.saturating_sub(1), 30 + y: self.y + 1, 31 + }, 32 + Point { 33 + x: self.x, 34 + y: self.y.saturating_sub(1), 35 + }, 36 + Point { 37 + x: self.x, 38 + y: self.y + 1, 39 + }, 40 + Point { 41 + x: self.x + 1, 42 + y: self.y.saturating_sub(1), 43 + }, 44 + Point { 45 + x: self.x + 1, 46 + y: self.y, 47 + }, 48 + Point { 49 + x: self.x + 1, 50 + y: self.y + 1, 51 + }, 52 + ]) 53 + } 54 + } 55 + 56 + #[derive(Debug, Clone)] 57 + struct Digit { 58 + digit: char, 59 + point: Point, 60 + } 61 + 62 + #[derive(Debug, Clone)] 63 + struct Number { 64 + number: u32, 65 + points: HashSet<Point>, 66 + } 67 + 68 + impl Number { 69 + fn is_adjacent_to_special_char( 70 + &self, 71 + special_char_indices: HashSet<Point>, 72 + ) -> bool { 73 + self.points.iter().any(|point| { 74 + point 75 + .neighbors() 76 + .intersection(&special_char_indices) 77 + .count() 78 + > 0 79 + }) 80 + } 81 + } 82 + 83 + #[derive(Debug, Clone)] 84 + struct SpecialChar { 85 + char: char, 86 + point: Point, 87 + } 88 + 89 + impl SpecialChar { 90 + fn gear_parts(&self, numbers: Vec<Number>) -> Option<[Number; 2]> { 91 + if self.char == '*' { 92 + let neighbors = self.point.neighbors(); 93 + // check if neighbors contains two numbers 94 + let xs = numbers 95 + .iter() 96 + .filter(|n| n.points.intersection(&neighbors).count() > 0) 97 + .enumerate() 98 + .take_while(|(i, _)| i <= &2) 99 + .map(|x| x.1) 100 + .collect::<Vec<_>>(); 101 + if xs.len() == 2 { 102 + Some([xs[0].clone(), xs[1].clone()]) 103 + } else { 104 + None 105 + } 106 + } else { 107 + None 108 + } 109 + } 110 + } 111 + 112 + #[derive(Debug, Clone)] 113 + struct EngineSchematic { 114 + special_chars: Vec<SpecialChar>, 115 + digits: Vec<Digit>, 116 + numbers: Vec<Number>, 117 + 118 + special_char_indices: HashSet<Point>, 119 + } 120 + 121 + impl FromIterator<Vec<char>> for EngineSchematic { 122 + fn from_iter<T: IntoIterator<Item = Vec<char>>>(iter: T) -> Self { 123 + let mut special_chars = Vec::new(); 124 + let mut digits = Vec::new(); 125 + let mut numbers = Vec::new(); 126 + let mut current_number_string = String::new(); 127 + let mut current_number = Number { 128 + number: 0, 129 + points: HashSet::new(), 130 + }; 131 + 132 + for (x, row) in iter.into_iter().enumerate() { 133 + for (y, c) in row.into_iter().enumerate() { 134 + if c.is_digit(10) { 135 + digits.push(Digit { 136 + digit: c, 137 + point: Point { x, y }, 138 + }); 139 + current_number.points.insert(Point { x, y }); 140 + current_number_string.push(c); 141 + } else { 142 + if !current_number_string.is_empty() { 143 + numbers.push(Number { 144 + number: current_number_string.parse::<u32>().unwrap(), 145 + points: current_number.points, 146 + }); 147 + current_number = Number { 148 + number: 0, 149 + points: HashSet::new(), 150 + }; 151 + current_number_string.clear(); 152 + } 153 + 154 + if c != '.' { 155 + special_chars.push(SpecialChar { 156 + char: c, 157 + point: Point { x, y }, 158 + }); 159 + } 160 + } 161 + } 162 + if current_number_string.len() > 0 { 163 + numbers.push(Number { 164 + number: current_number_string.parse::<u32>().unwrap(), 165 + points: current_number.points, 166 + }); 167 + current_number = Number { 168 + number: 0, 169 + points: HashSet::new(), 170 + }; 171 + current_number_string.clear(); 172 + } 173 + } 174 + Self { 175 + special_chars: special_chars.clone(), 176 + digits, 177 + numbers, 178 + special_char_indices: special_chars 179 + .iter() 180 + .map(|sc| sc.point.clone()) 181 + .collect::<HashSet<Point>>(), 182 + } 183 + } 184 + } 185 + 186 + impl Day3 { 187 + fn part_numbers(&self) -> Vec<u32> { 188 + self 189 + .engine_schematic 190 + .numbers 191 + .iter() 192 + .filter(|n| { 193 + n.is_adjacent_to_special_char( 194 + self.engine_schematic.special_char_indices.clone(), 195 + ) 196 + }) 197 + .map(|n| n.number) 198 + .collect::<Vec<u32>>() 199 + } 200 + 201 + fn gear_ratios(&self) -> Vec<u64> { 202 + self 203 + .engine_schematic 204 + .special_chars 205 + .iter() 206 + .map(|sc| { 207 + sc.gear_parts(self.engine_schematic.numbers.clone()) 208 + .map(|x| x[0].number as u64 * x[1].number as u64) 209 + .unwrap_or_default() 210 + }) 211 + .collect::<Vec<_>>() 212 + } 213 + 214 + fn load_engine_schematic(input: Input) -> Self { 215 + Day3 { 216 + engine_schematic: input 217 + .lines 218 + .into_iter() 219 + .map(|line| line.chars().collect::<Vec<_>>()) 220 + .collect::<EngineSchematic>(), 221 + part_numbers: Vec::new(), 222 + } 223 + } 224 + } 225 + 226 + create_solution!( 227 + out_1 => u64; 228 + out_2 => u64; 229 + year => 2023; 230 + day => 3; 231 + sol_1 => |input: Input| { 232 + let day3 = Day3::load_engine_schematic(input); 233 + Some(day3.part_numbers().iter().map(|x| *x as u64).sum()) 234 + }; 235 + sol_2 => |input: Input| { 236 + let day3 = Day3::load_engine_schematic(input); 237 + Some(day3.gear_ratios().iter().sum()) 238 + }; 239 + );
+2
src/solutions/year_2023/mod.rs
··· 1 1 mod day0; 2 2 pub mod day1; 3 3 pub mod day2; 4 + pub mod day3; 4 5 5 6 pub use day1::*; 6 7 pub use day2::*; 8 + pub use day3::*;