From ef57c0df338dc3897d0cfec969daf66f787d4540 Mon Sep 17 00:00:00 2001 From: Bart Riemens Date: Thu, 7 Dec 2023 21:43:51 +0100 Subject: [PATCH] 2023 - Day 3 --- 2023/03/PART1.md | 46 +++++++++++++++ 2023/03/PART2.md | 44 ++++++++++++++ 2023/03/input.txt | 140 ++++++++++++++++++++++++++++++++++++++++++++ 2023/03/sample.txt | 10 ++++ 2023/03/solution.ts | 98 +++++++++++++++++++++++++++++++ 5 files changed, 338 insertions(+) create mode 100644 2023/03/PART1.md create mode 100644 2023/03/PART2.md create mode 100644 2023/03/input.txt create mode 100644 2023/03/sample.txt create mode 100644 2023/03/solution.ts diff --git a/2023/03/PART1.md b/2023/03/PART1.md new file mode 100644 index 0000000..ba89a9c --- /dev/null +++ b/2023/03/PART1.md @@ -0,0 +1,46 @@ +--- Day 3: Gear Ratios --- + +You and the Elf eventually reach a gondola lift station; he says the gondola +lift will take you up to the water source, but this is as far as he can bring +you. You go inside. + +It doesn't take long to find the gondolas, but there seems to be a problem: +they're not moving. + +"Aaah!" + +You turn around to see a slightly-greasy Elf with a wrench and a look of +surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working +right now; it'll still be a while before I can fix it." You offer to help. + +The engineer explains that an engine part seems to be missing from the engine, +but nobody can figure out which one. If you can add up all the part numbers in +the engine schematic, it should be easy to work out which part is missing. + +The engine schematic (your puzzle input) consists of a visual representation of +the engine. There are lots of numbers and symbols you don't really understand, +but apparently any number adjacent to a symbol, even diagonally, is a "part +number" and should be included in your sum. (Periods (.) do not count as a +symbol.) + +Here is an example engine schematic: + +``` +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. +``` + +In this schematic, two numbers are not part numbers because they are not +adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number +is adjacent to a symbol and so is a part number; their sum is 4361. + +Of course, the actual engine schematic is much larger. What is the sum of all of +the part numbers in the engine schematic? diff --git a/2023/03/PART2.md b/2023/03/PART2.md new file mode 100644 index 0000000..cc3f31d --- /dev/null +++ b/2023/03/PART2.md @@ -0,0 +1,44 @@ +--- Part Two --- + +The engineer finds the missing part and installs it in the engine! As the engine +springs to life, you jump in the closest gondola, finally ready to ascend to the +water source. + +You don't seem to be going very fast, though. Maybe something is still wrong? +Fortunately, the gondola has a phone labeled "help", so you pick it up and the +engineer answers. + +Before you can explain the situation, she suggests that you look out the window. +There stands the engineer, holding a phone in one hand and waving with the +other. You're going so slowly that you haven't even left the station. You exit +the gondola. + +The missing part wasn't the only issue - one of the gears in the engine is +wrong. A gear is any * symbol that is adjacent to exactly two part numbers. Its +gear ratio is the result of multiplying those two numbers together. + +This time, you need to find the gear ratio of every gear and add them all up so +that the engineer can figure out which gear needs to be replaced. + +Consider the same engine schematic again: + +``` +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. +``` + +In this schematic, there are two gears. The first is in the top left; it has +part numbers 467 and 35, so its gear ratio is 16345. The second gear is in the +lower right; its gear ratio is 451490. (The * adjacent to 617 is not a gear +because it is only adjacent to one part number.) Adding up all of the gear +ratios produces 467835. + +What is the sum of all of the gear ratios in your engine schematic? diff --git a/2023/03/input.txt b/2023/03/input.txt new file mode 100644 index 0000000..14a7579 --- /dev/null +++ b/2023/03/input.txt @@ -0,0 +1,140 @@ +...................305.124................................432..............................................576..313.....514................. +.............113...-......&....................&...819...........654..../..........................&901................*....869.257......... +...377..&783../.................................9...........855*......940..463................-.........................844.*....@......679. +......*...........197.261.....817..336.759............&742......548.......&........748......844.............#.......&........254...169..*... +.......36....368.*...............*....*.........*..88......%866.......135.........*..................515.682.....114...%...........*.....768 +...233......*....909..698.........427..........127.*...................*...........450.........482..../.................312.....621......... +.../.......882...............776...................555......180.........971....217.......857.../........212....674.917......736.....441*760. +............................*.....@....907....%940.....%.......+.#..........45*...........$............#..........*...............=......... +..................370.225.425..211...................932..........381..267.............45.....=....549....238........367..&.....488......... +.468.............*.......................761...677.......&.................929..907.....%....79...*.......*....324..*....515................ +....-.#103.504...388...........$..........*.....@........766.%................*....*623.........532..881...573....*..840..............497... +...........*.........305....412..687.......971................676....@640....657........709......................807............98.....+.... +....279...502...62...*.............*..............................................838%.....$.........585..........................*......... +....=...=......*....209.825.482.993..........972............620*664..-759............................*.......184.........952....160.....156. +.......814.....931......*...*............551..*....711...65.................224*115...851.772..104..505.....=........796*................... +....................723.103..275........*......564..*.........830........................*...../..............................232........... +.............$......&....................951........703......=............160+.....138......*........257*809.............$298........284.... +.....684....464.333..................827.....*211..................................#.....855.86.....................$913............*....... +.......*...........*..*......917.163....*.409......294.......................#..39.......................511....358.......$671.......237.... +.......663.......311..163....*...%...158........*....-.442.........958....108.....#.....941*837.........*..........*........................ +............865=..........409....................128...*.......283....................................680.........730.935@........768....... +....../................=.............54.....857.........418.....*..911.......610..643............381...............................@........ +.......925............984...........-...800*.................194.../.........*.......*.........../.....................................@.... +.................................@...............................&......615...16....695.....-..........................*750.............855. +....611..136...................108.540.........362..711......*....420.....................190.......................213.............86...... +204*....@.........288..825..+........&...........*....*......733.........................................7..387.......................*..... +...........795..../.......+.505.834.....228....192...619..........886......467.....942.............................%838.........512...340... +.......957.........................*...*..................962........................*..............211...$..............624................ +.......*....................349....144......./786..350.......*.......................541..21.......$.....284..190*......*............518.... +....484...@....17..........*..../........309..........*....530...118..........208..............253................489.828...........*....... +..........309...........715.....531........+.516#...645.........$........369....*.......*185...@............=.#...........@............&.... +491*192..............................................................233.........711.948.........572.....869..289......932...........149.... +........274.....511.....425..........421.288*965...652=.919........./....*.786...........676.....*..................+.........=............. +.........*.........@......$...*.........................*........*....791...+..113......*....527..897..499+.904...166........580.........389 +.......306.....................353.497..929/.......=....553......495.................884.....&........................794........774........ +...................../...........................582....................................................489..........*...............&...... +.605......537.....247.....................479................74................227*995.761.....@...........+.......958...87.....547.795..... +...*.....*....................289+.....*.....%........967..............*...............*........611.....28....367.................*......... +..690....916.497....................894.440......547$.............596.492......23...............................*...............237.221..786 +.........................681.....=..........580+.................+...............+.......696............@..542..56.......................... +...936..639*947.............+...48.*709..................307.257...-248.697.553.............+.........146.....#.......*241.................. +...*................568....................=378....41.......*............*...%..11......*..........................982.............#........ +..323..704.+724..*....*..195*....812..879...........*.....*....508@.....852....$.....664.......317....*......332..............326.158....... +.......*........610..767.....936...#...&..744.290..839.382.543....................................&.827...../.............323*.........884.. +...........962...............................*...................46....502.265.150...................................639...............*.... +955*818.......*...@...124*512..256...................+....&..............*....*.......170....996...#........................786.......4..... +..............284.697...........*......172...........398.435.......-.....380..........+..........698....428...361................206........ +.822...#..............936$...745..........*..%59..................726...........+.........249......../...*.....*..................%......... +.....870................................978.......761=.......179.............692.........*..........477..550.....676..281.............507... +..............913.................215.........................*..745...#.............882..130..569....................&..........*472..*.... +...27..710.....*.....................*.205*403..............59....+..172................=.............................................697... +....*....*....731.......865.......155..........849*....#..............................................679...854....................&........ +.985..%.............599....#..........191..520.......473..#........./...........708....406..677+...=............................599.....133. +......501...........*........@.........*...*..............446....767...*444........@......&.........49......#...311*413..................... +..................157.......830..13.90.398.945.978............................950.................%.......88................$............... +.............297........618.......+.*............%............317.............../.........709..481............503.........570...305*845..... +......661....*....632.....@..+......524......../.....+...........*...590..............944....*...............*................+..........681 +.......*.....820...@..........829...........671.......206.......239.+......................339..#............106......415..213..........-... +........114........................903.669=............................764...319....373........642...................................-...... +.................*...........................*.......121.......898&.....$......*...*.....................................211#.......579..... +........*.......362......787.440..........842.825.....*......................590...357..........949......523....470..292.................... +.....594.459........98.....*....*.......................355&..*..................*..........554..*.......*.......*....*..222...243.......... +.........................535.....674.........804....#..........455.............847............=.32...23..951.....217.346....+.*....+272..... +..183...........443..........119.......833....#..693......818#........................@..699..............................*....678.......... +...*......455/...%..826.............#.....*.........................$...........868.739....=..753...181....773.........323.508.............. +...382................*...169..510..590.295....212.767....*970......711.........*........#.......*..*............651...........36..766*..... +............&..61..697.....*................46.*....*..691......970............786....953.....775...235..................*550....*.....417.. +..........45.....*........689...645.....549*...821.601.............*......................952..............#..........464.....609........... +.....657.........48...888.......*...................................305......+47..........................254.27......................123... +818......388............*....412....../491.........#336...506..422.................989....15...70..............*..354............@.....*.... +..............506........11......./...........643..........#..=..........703...963*........@.........613.....413....%...485.......265...33.. +....589.......*................207..............%...............................................+................*......*.....94............ +...*.........663...413+....................................819...........655.%...648..823.......770....385......457..150...#.../............ +515.......................475....825......33../350.....459...%..........*....681...*...*..............%..................437.......689...... +....$993..897@..............*.......*837...-...........*..............726........372....922......&198................................*...... +180............*..........679.................71.629=.796......109...........................527..................682.....558.....798....... +........437.742.46.................@.......&...................*......498.......@.............*............./......$........*............... +..761..$...........148.......856....701.377..536*510...604...540.....&........929........172.799.............310.....700...........440.%.... +.....*........808*....=.........@......................*.........577.....................*................&...........$...417.39.....&.229.. +........794.......446..............................214.543...%...*..........935......865..445...../....%.615................=..*............ +..572..+...............................419....932@.........924..72.902.......-..579.-...........997.203................873......945..567.... +.....*.................273.............*...........................*...142............688................755.850...........846.........=.... +...880................*..............883.698.....$.......=........197.*...........316*.........365.........$...*............*............904 +.........385..977..179....+...................652......875.............350...863............73......464.........214.852..478................ +......22....*..@.........93............*682..................*137..309.........*....557.....*........................=.......698.....+.218.. +......*...645......163...............89......*853..................*...386..341.......*...228............718*492...............*..153..*.... +.....576......................857.........513....................509......*.....322.710.........615.......................613.165.......292. +................................@.....370.......598......487...........503..*.....................*...738#..#..$901...$.....#.......648..... +..19*454.............78................/..876..#..........*......+..........937.......383........523........69......326.678.....212....*.... +.....................*.....436.@...646...*..........100....367..325.....361.......207*....50............621...............*..........237.... +757.........714......502...%...54......909.411.....*.....................*..........................=..$................415................. +...$.......&.................................*...471..985...508.472....891.842..$.................418.........&.....&....................... +.................$........906..........*215.338...............*..............*..606.#235......................813..259........229........... +........472...222.........*.....245.572.......................927...769....344.............291.239.....................94....*.............. +...818...$.............291..............288...........163.734......+...........699............*.......981&...514*.............224.....=..... +.....*..................................*.........................................+..189..........452............644...................669.. +....850................326$..............869..&985......784..../..............592.......*..153.....*....887..493.....211........%........... +.............................457....834.....................94..564..........&.........498....*...304....../...*.....*..........845.....&... +.......12*48.753...244..196....=......$..721....90*29.........*.................+.522.......487...............317.....531..311........20.... +.177...............*.....*.................*.............*969.611.......*565..338...*.............712..922$.......770......*....522......... +....*..459......149......165...=........879...........103............497..........258.931..75....*..........400*.....*......972............. +.269...*............#........715.............................*475..........................*....145.............414.....228........893...... +........209......508...................../331......469....894............347.890$..541.....56...............#........#..*................... +......................=...........37................*...........................................$...........197...119.....=...........756... +...........&.......850.....367.34...*............517........178*716..........=...................892......-............883...755............ +.....@496.125.................*......965.....428.....908..................405.....650...................=..493..89.......................... +..............581........396.............596.........*....964........672......159.%....463............768......./.............391........... +.............*...........*.....871.660..*......577.73......@.....932....#....*............*574...186........154......996........*......720.. +............914..132..137......=.......746......*..................*......376.......&661............*560.%.......%....*...264....511........ +...%...151................*851...................................*..284.........754..........190.19.......611...326.841...............89.... +644.....*...797........133.....476..............................810.............*.....866......+.*..............................#903..*..... +........839..*..478...........*........372.#.....744.....227*............477.3.253.%....&..746.....289.&860.....720*708..................... +............443...%.....68*....283.......%..900...%....%..........584......*.$......308....*......*......................592................ +...239................@....250........50........&...%...192.$111.*........74............831....914...#.....886*492..........=............... +...*......425.....181..680..............*......873.502............93.................................503.@..............116........372...... +492..........*.....$.............302...789.................861.......................290.......452........83....626....*.....76.......*..... +......178...528..........*739...@..........................*......804....320....#.........&878.*.....746........./..&...148...$......741.... +...&....*.............262..............325...........599..592.579.....%...+..808...............386..*...............761..................... +663..462...........*..........109..706*.................=.............553........712......*971......674.396...635*.......................... +........../...728.952...413......*......744.......%..........................300..*....782................*.......742..&424........41+...564 +........375...%.........*......450.456.$.........714........851.327..#...+......*...+.......179.630....854.................................. +..251................195...887.....*........*209.......430.*......-.83.596....956..966.274.%.....*........................83.359........411. +....%.......................*..........89.........194..+...491.........................&.....*.762..+741..........598.......*.....910..*.... +..........618+..............677........*..598........+..........-.....58....290...........795...........................*.....580.&....559.. +...................561..............910......-..321....834....602.752*....=..+........191..............463....@..........792..*............. +............216.......%...722*668.....................$..................109............*....222...195*.......689....380.......637.......... +.....846.......*..436..............=827...263@.764*.......*.464.................729.....934...*......................................289+... +.127*.......757.....&......./954...................965..664........./......716*.+...........266....781.........989..........844...$......... +................57.....................&118......=...........385.623...245..............345........*.....615-.....*..........*...768........ +...538...........#..860......................783.460..........*............401.............*796..354..............37......797........725.193 +....*..#620../.........*..........578..54...*..........780..251...634..946....*........................./....702........................*... +..202.......815........724....#..............822..972...................$..........*.....#....@........27.....*...........268.....347....... +.................+..........98.....703..............&....*52.....320.............670...318....734.............47..................+....239.. +.953*919...=......273................=...454.....@....601...........*...958.........................+.................758...=............... +..........905./50.........@...................971..................762.*..................169........915.682..........=......533.......502.. +.....884.................24...........262.........@.....531..698.......234.................................................%.......149..*... +....................834.....994.......*........266.....*......*............................................/.....941.434...812.......*..211. +.............133......*......*.....613...85..........871.......497..346...737....88.....176.....192$........128.*.......&......./163.26..... +....734..543....*.....656....461........*......531..................*........../...*..&................$971.....931......................... +................606....................506............................779.......30...211.....243..........................153...504......... diff --git a/2023/03/sample.txt b/2023/03/sample.txt new file mode 100644 index 0000000..b20187f --- /dev/null +++ b/2023/03/sample.txt @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. diff --git a/2023/03/solution.ts b/2023/03/solution.ts new file mode 100644 index 0000000..809bbda --- /dev/null +++ b/2023/03/solution.ts @@ -0,0 +1,98 @@ +const sample = await Deno.readTextFile("sample.txt"); +const input = await Deno.readTextFile("input.txt"); + +interface Result { + value: T; + line: number; + length: number; + index: number; +} + +const isSymbol = (char: string): boolean => + char !== undefined && isNaN(parseFloat(char)) && char !== "."; + +const isGear = (char: string): boolean => char === "*"; + +const hasSurrounding = ( + dataMap: string[], + { line, index, length }: Result, + predicate: (char: string) => boolean, +): { row: number; column: number }[] => { + const result: { row: number; column: number }[] = []; + for (let row = line - 1; row <= line + 1; row += 1) { + for (let column = index - 1; column < index + length + 1; column += 1) { + if (predicate(dataMap[row]?.[column])) result.push({ row, column }); + } + } + return result; +}; + +const solvePart1 = (data: string): number => { + const dataMap = data + .split("\n") + .filter(Boolean); + + return dataMap.map((line, index) => + Array.from( + line.matchAll(/\d+/g), + (x): Result => ({ + value: parseFloat(x[0]), + line: index, + length: x[0].length, + index: x.index ?? -1, + }), + ) + ).flat() + .reduce( + (sum, result) => + hasSurrounding(dataMap, result, isSymbol).length + ? sum + result.value + : sum, + 0, + ); +}; + +console.log("Sample:", solvePart1(sample)); // 4361 +console.log("Input", solvePart1(input)); // 550934 + +const solvePart2 = (data: string): number => { + const dataMap = data + .split("\n") + .filter(Boolean); + + const numbersPerGear = dataMap.map((line, index) => + Array.from( + line.matchAll(/\d+/g), + (x): Result => ({ + value: parseFloat(x[0]), + line: index, + length: x[0].length, + index: x.index ?? -1, + }), + ) + ).flat() + .map( + (result) => ({ + ...result, + gears: hasSurrounding(dataMap, result, isGear), + }), + ).reduce((group, result): Record => { + result.gears.forEach((gear) => { + group[`${gear.row}:${gear.column}`] = [ + ...(group[`${gear.row}:${gear.column}`] || []), + result.value, + ]; + }); + return group; + }, {} as Record); + + return Object.values(numbersPerGear).reduce( + (sum, result) => + result.length > 1 ? sum + result.reduce((s, v) => s * v) : sum, + 0, + ); + +}; + +console.log("Sample2:", solvePart2(sample)); // 467835 +console.log("Input", solvePart2(input)); // 81997870