From 637b097245f1d67f5391e0cd10de6fad6623dbbf Mon Sep 17 00:00:00 2001 From: Nettika Date: Sat, 9 Dec 2023 19:33:23 -0800 Subject: [PATCH] Solve day 3 --- 03/__main__.py | 14 +++++ 03/puzzle.py | 35 +++++++++++ 03/puzzle_test.py | 10 ++++ 03/schematic.py | 88 +++++++++++++++++++++++++++ 03/schematic.txt | 140 +++++++++++++++++++++++++++++++++++++++++++ 03/schematic_test.py | 87 +++++++++++++++++++++++++++ 6 files changed, 374 insertions(+) create mode 100644 03/__main__.py create mode 100644 03/puzzle.py create mode 100644 03/puzzle_test.py create mode 100644 03/schematic.py create mode 100644 03/schematic.txt create mode 100644 03/schematic_test.py diff --git a/03/__main__.py b/03/__main__.py new file mode 100644 index 0000000..7644b01 --- /dev/null +++ b/03/__main__.py @@ -0,0 +1,14 @@ +from pathlib import Path + +from puzzle import solve_pt_1, solve_pt_2 + + +def main(): + input = Path(__file__).parent.joinpath("schematic.txt").read_text().strip() + print("Sum of Part Numbers:") + print("Part 1 -", solve_pt_1(input)) + print("Part 2 -", solve_pt_2(input)) + + +if __name__ == "__main__": + main() diff --git a/03/puzzle.py b/03/puzzle.py new file mode 100644 index 0000000..1db2e69 --- /dev/null +++ b/03/puzzle.py @@ -0,0 +1,35 @@ +from schematic import Schematic, SchematicNumber, SchematicSymbol + + +def solve_pt_1(input: str) -> int: + schematic = Schematic.parse(input) + + total = 0 + for part_number, _ in schematic.part_numbers(): + total += int(part_number.number) + + return total + + +def solve_pt_2(input: str) -> int: + schematic = Schematic.parse(input) + + part_groups: dict[SchematicSymbol, list[SchematicNumber]] = {} + + for part_number, part_symbol in schematic.part_numbers(): + if part_symbol.symbol != "*": + continue + if part_symbol not in part_groups: + part_groups[part_symbol] = [] + part_groups[part_symbol].append(part_number) + + gears = [ + part_numbers for part_numbers in part_groups.values() if len(part_numbers) == 2 + ] + + total = 0 + for gear_1, gear_2 in gears: + gear_ratio = int(gear_1.number) * int(gear_2.number) + total += gear_ratio + + return total diff --git a/03/puzzle_test.py b/03/puzzle_test.py new file mode 100644 index 0000000..241259a --- /dev/null +++ b/03/puzzle_test.py @@ -0,0 +1,10 @@ +from puzzle import solve_pt_1, solve_pt_2 +from schematic_test import mock_input + + +def test_solve_pt_1(): + assert solve_pt_1(mock_input) == 4361 + + +def test_solve_pt_2(): + assert solve_pt_2(mock_input) == 467835 diff --git a/03/schematic.py b/03/schematic.py new file mode 100644 index 0000000..c694397 --- /dev/null +++ b/03/schematic.py @@ -0,0 +1,88 @@ +from __future__ import annotations + +from dataclasses import dataclass +from typing import NamedTuple + + +class SchematicNumber(NamedTuple): + number: str + row: int + col: int + + def extend_digit(self, digit: str) -> SchematicNumber: + return SchematicNumber(self.number + digit, self.row, self.col) + + +class SchematicSymbol(NamedTuple): + symbol: str + row: int + col: int + + +@dataclass +class Schematic: + numbers: list[SchematicNumber] + symbols: list[SchematicSymbol] + + @classmethod + def parse(cls, input: str) -> Schematic: + row = 0 + col = 0 + current_number: SchematicNumber | None = None + numbers: list[SchematicNumber] = [] + symbols: list[SchematicSymbol] = [] + + for char in input: + match char: + # Digit + case n if n in "0123456789": + if not current_number: + current_number = SchematicNumber("", row, col) + current_number = current_number.extend_digit(char) + col += 1 + + # Blank + case ".": + if current_number: + numbers.append(current_number) + current_number = None + col += 1 + + # Newline + case "\n": + if current_number: + numbers.append(current_number) + current_number = None + row += 1 + col = 0 + + # Schematic smybol + case _: + if current_number: + numbers.append(current_number) + current_number = None + symbols.append(SchematicSymbol(char, row, col)) + col += 1 + + # Finalize a number at the end of the schematic + if current_number: + numbers.append(current_number) + + return cls(numbers, symbols) + + def part_numbers(self) -> list[tuple[SchematicNumber, SchematicSymbol]]: + results = [] + + for number in self.numbers: + for symbol in self.symbols: + if ( + # Symbol within 1 row + (number.row - 1 <= symbol.row <= number.row + 1) + and + # Symbol within 1 column + (number.col - 1 <= symbol.col <= number.col + len(number.number)) + ): + results.append((number, symbol)) + break + + return results diff --git a/03/schematic.txt b/03/schematic.txt new file mode 100644 index 0000000..86d4798 --- /dev/null +++ b/03/schematic.txt @@ -0,0 +1,140 @@ +...766.......821.547.....577......................................387.....................56..........446.793..........292.................. +...........................%...../.....981..........627..../..........-.....623......610..-..............*..................16......891..... +...$...........716..&336.......470.325.................*.84........$..34....*.....+.....#.....*76....#.........303.433........-........&.... +.117../359.#...............595............129..963#..722..........128........192.313........31........887...............234.......-......... +............298.....922...*.......482.......*..................*......./.........................395................264..../.......166...... +.732..................*..815..920*......113.827.........453.571.356..902......693...147............*.....128................................ +...*..........451-.442..................*...................................+....*....*.......918...680...........................529+...... +....844.587.....................347...425.....974......348.........$615....174.330..............*..................556.......972*........... +..........&...676.........947..%.................*976.*.................45..........192........272.131..............-..977*......85......... +.588..........*........$.$.......515...493.............73.....%...........*.....428.*....................*968..............964.......153*274 +....=..860...157....347............*..*......954.930.......472...618....899......%..726.330..44.......687........$.......................... +..........@..................+....465..47.......*......*............*........554............@...................485..320.................... +.....................%831.267.......................305.844.........413........*........741....-...692.948..........*..........650.......510 +....................................*212....419..............848............710.............670.....*......932...281......-.........398..... +.......%....782.......187#..-....890...........@................*../...*531.....745................645......*........#..&..835......*....... +.......180..=....153*......487........@322.........693.........805.687..............350.......259........626...849.221.833.......573....%674 +.....................821...........................*...988.............90..273...................*............*............................. +.....65*845..346%..................*412...@.....901.............79.....*.....*..........169.764.14...3.....839...........559..=............. +.....................553..798.............318.%..............29......772...................*......................511.........749...697..... +.........@............*....*.........414.......13.517.......*...............597.....................28*366..........*......%.........*...... +......412...........724....973...&....*.............*..#......................%...910...........702......../......473.651...375.*347........ +.............*559................906..111........449....510......+342............*....797.262.............650............................175 +...........40.........793...................156......./.....643................577..........+.....................*304...711....437*368..... +.........................*.............................843..*...........47..........406............=..../......761.......*...............851 +......309@......283...296......#.....322............./.....72.....729.....+...515......*..78......735..970.130..........16...../.&...426*... +................................720.@......952....958.........672*..................239....*...............+................816..1.......... +............=..............259........494.+...........................+...447...........310............*...............%553................. +....650....999..837*.......+.........*......%359..604.....577.........591.+.......................832.383........@252.........292........... +....*...............677..........581.528...........*..=.....%.....................583.........................&........*354................. +.................=.........470...*...........=...467...117........$736..3.........*...........................127...945.......808...&....785 +.747...658.....223...../........563.......466..........................#...652.841......417...........253.................906.*...984....... +..........*106.......173....396.....................*106....645...426........*.......*......310.........%.....238*546.....*...442......$145. +...618.........&203............*........988......833.................*......594.....837.....*...........................460.......671....... +........................*...548.....704*......................274..985.575*...............122.....574......=391.............433%.-.......... +......871...747.......468................466*367..482....455....*..........53...................&.*.................507@.................... +........*..*..............597......................*...........855............................502.243.$484......446............-............ +.......51.............841...+...........7.565.....281.873*305........603................$..................#781...*.819.....591..104....675. +............+..........*.......391-......*...........................-....658....515...861.......195/....................................... +.............130.903...235..........757................&...765*237.........&....*...........215.......389%............*...269*812........... +.526.919...........+.......-.......*........508*202...109............53.......899...........*....@..................55.94..............26... +...*....................329........73...........................60=..+.....................10..152........674*993..........74..343.......... +......305......................902........349...542/.......................783..352...38.....................................=..*....507.... +.797.....+...............@.......=.......$.............303.348..408............*......-...729......=867.683..+.425*137.........698....&..... +....*...........988.964.145.785.....916............*.....#.*...*.....535%.806.532.........*....@.........*..11.............................. +.684..834...473*.....*.......-.....*........892.372.448.....77.220........*............326..723.........836...........#503...994....*786.... +.........*..........863..483....773...........*.........101................121..535..................46......706..............*..334........ +.938*..304..../854......*...........267...462..367........#.......................*..%..............*........*...&...*......631.......384... +....................................@.....*......................485..836...197..594..393....433..169.209..13....812..849..........=..../... +.....................#.....132........172....419..380..245*860...........*...$.................=......@............................264...... +....52.555.........602...*........780*..........*...*............................398*245................................61.196.+............ +......*........@........624...823...............823..770.@....518......663.........................@972.....396*975.......*.....986.....*968 +.............694.308........................373...........578...#.........................959........................=250............461.... +..601...............#....719.-.......&.......................................757.............*.........344................660............699 +.....-.358..480*989...........261.....749......@......-689...209-.......826$...-..194........457..........*...473..........*.........893*... +.339......%...........221..................643..564................................*.............443.....798.=.........668..69....@......... +....*702..........&........145.734...........@..........$951...............325......439...........&.......................*....151.......436 +.........#......554.661....%......-....243.....................735.......-.....457.........769*........474*769............547........331.... +136.361..907.........*........../..........600.....-..739.769..#.........306.....*.............245..............*218..296......683...*...... +...*..................682...746.765..............681.....*...........511......704.........231................892..................*.715..728 +.......*415...................*........295..468.......................*.............35...*......./................665.985..&148.........&... +....831.......510....444.......565.................3.....334......558..........626........342..532..426......709...#....*.......815......... +..............*......*.............................*....*....*...*...............@.....................................418......*......%527. +.280#...515..706..305........................344.623...957.803..2..............=...871.............243..989....+..............218........... +.........*...............773...../...........@.......$................519/.88..249.+...........449*........#....86....454...2........$437... +..........822...197.........*..717......./.......8....562.58*102.936@......*............187...........................@.........212......... +..349.719.............728..623...........58.....*........................610....873........@...................................*............ +...*..*..................%.......423*214......259....426...101.....346............*.+.........359.....813.=...686.....$...639............140 +..839.768....464.........................../........*.....&...........@....367.951...741..........*....=..283...+..195.....%........632.*... +...........#.......................821.893..539..287....................../.....................942...........................149.....*.925. +......%....155..........*48.............*............143....../901.............844*368...=...................48....777.........*....246..... +....79...............282.............437...............+...........561.....736............133.%.........+......*....*...257*....71.......... +.............655.131................................................*..........%..............154.......897..791.....29.....358............. +...$...966.....*....*....864......892...........661..857=........=..259........645.................................$....630...........134... +.806...=.....=....949...............*....#.......*..............531.................410.............................428..*.................. +..............225............941....570...705....705..36.86...........................%...972/...735....448/............528................. +.....253................................................*...506.918.......390.478............................862............................ +.....*......137......................300..........607.....#....*......663................190................*.....869&..117$................ +.831.252.......+...%483..110.........*......299.........606..........&..................=................148...24..............147*748...851 +...........................-.......431.....%....820.........713.219....366.......704...........................*....%...&...............*... +.......669.295.......*.................................155..*......#......*......=.....727........494...27...110...588..676..904...345...382 +......@...........987.829......732/......=.517*519.405.......897........115........716*...................*....................*......*..... +..........................740..........713.........*.............978........*................+387.......=..885....-...+968......951....669.. +....105..............907.-.......105.......807..741.........*....*........627...882...=...........695..779.......248.......421.............. +...$................$.............*..907*.....*.....*....111.215.89...723........&....546.....849*....................119.....*......644*... +.......62.....723......155.923...26......678.341.998.107...................=.........................=.........934.9....*................... +....20*.................*...........251..............................470.46../........................31............*...406...441........... +........$61......#...628......942......&......255/..926.......................520......47&...............240.......791.......*....875...424. +.851.27.......700............*.....234..............-.......91.......=..729......................706...........458.....752...722.*......*... +....*...............997....635...........920...*...........*.......266............2................*.............*......=................656 +.........=...................................57.115.773.944..107.........368...../..205.......971..829.728........566..........75....269.... +.......538...........646.......706#..762..............*........*.814=...................291..*..............30................*......&...... +..605*..............*...............*.......226.....709.8...671............159..974#...$.....427.......................645..587............. +..........973.866..799......913...741...600*....705...............+........*.............................565..606.........*........654.778.. +......872....*.....................................#.873.....-225.499..546.847.......763..743@......113.....*.....=....380............*..... +.........+......891.53.......206..........760..................................822.....-........136..%.....610.....20......85............... +............200..+....*......*....236*511......849+.....672-.&................@..........591*.....*....292......%......176*.......571....... +....#15......*......783....512..+....................*........914.86...-216........-574......381.658......*..201....17......758..*......$... +..............560................725...628...+...646.432..867.....*.....................................734........*..........*.570....948.. +....417.....................................992....*............638......696..362...=.......40......................843.....722............. +....*......*.................855.169...623......816..816...97.........*....*.....-..998..............................................3...... +...646..646.389..........................-..............*../.....990...972.92......................333....=..........185..261.400...*...#... +.................966.&.............$.........#.....413.700........*...............................&....465...........*......=...+.621....452 +..873..247........*...212....290....429...551.....=............305....#...719.776....573#....657...............972..477..................... +.....*...........725............*.........................405.........952..*.....$..........*.....856.......................295............. +.....661.............919*92.....373.917*..794....#............12/..&.......468.......$....841.680.../..303..$..............*....653.197.=... +..........730...........................9.#...#..166...206..........794.............891........*.............192.....300...329..+...*....884 +............/...........*189.....*..........677........+...%.............961.................819....294*............................242..... +.....................139......607.41.923....................610..528......*...&455.....70.-.............215.......827&...256....124......... +...............342..........................454.................*.......885.&...........*..396....69&....................*........%......... +................&...........842...581..335.&.......672.......144.............852.......801...............&60..*............&..........802... +..73.133.40....................+...*....%...........+...............248..........769.......511......707*......710..........228..........*... +.....*...*.........984.....532....415..................................=.........*...151....*...........467........170..........#....913.... +..206.....147.....%....338....@..........394.........143%./......958..........749...*....569...................848...#.........268.......... +..............493......*.............*....................844....*................824...........=...553.......%...........894............... +......................38..........796.852...=....955............674.974...................964..551.=............*259..-..*.................. +...596............857......941...............497...&.564.890.........*..311.905......382...../..........25...329.....241..174......873...... +...........907.......#........*741.....................*......180.808..*......*........*..........764..........................269*......... +...308.......&.569......898........524...452...426..430....34.*.......937...231........752..........&.......111.......................93.... +....*..........&..............754...*.....*......................409............619................................610...............*...... +...20...886............788.......&..261.943.............#...#.....................*.../........@......+..137........*..460.205......760.71.. +........*.........+........837........................184..892...................811..161....648...643.....*........10.......*.............. +........309.....290..........*...../227.......331...........................659............................876..440.......891.........893... +............*..........198...................*.............711*629.............*....837....538..-................*....335.....+.........=... +..........923.............*281.330..423...726....530..*280....................990..*.............651..661......121......=.....236...344..... +.....419...........$.............*...*..........*.............866..904............96..422.............@......%.....744.....$.......*........ +.......*..........770......../..843.794..42......201...........-..*.....................*........%.........905........&.....335..865...255.. +....164....95.814...........300.............*332...................180.....739...=..464.619....617......-.............................#..... +.............*.........*.............304..81................../...........*....662...*................658...............368..%...........996 +.........755........738.329.&866...../.....................614...$.......851.......254......917.152............297..860*.....466...774...... +..........*................................467.....173.........$.684...........................*.......535.......$....................*..... +..960..569.....888..=......................+........*........744.................................882........................975........841.. +...*..............*.700..........................458....*817......668..........882.710.............#.413%......@.............*.............. +..648............63...............803.237...341......229.............*..632....*......*910...405................625...........805...&...$... +....................../...=............*.........984..........417...78.....*..141.+............*....46..839............786*88......454.289.. +.....145..=........502..63............111...826....*.../.........*......740.......153.432.....74......*....&.430..............594........... +.......*...739..............599.&.............#...454.611........291........196......................172.......*.%434............*.......... +.........*..............671..&...266............/.......................928.-................................434......387/......16.699...... +......538.581........&....*............%......10.....168....537&....296..*......177...192................-.......470........................ +..................661......496.346*.....870............*................958....-......*......-....@......101.....+.......................... +..808..............................365..................195.........................90......482.837............................404.214...... diff --git a/03/schematic_test.py b/03/schematic_test.py new file mode 100644 index 0000000..dd069a3 --- /dev/null +++ b/03/schematic_test.py @@ -0,0 +1,87 @@ +from schematic import Schematic, SchematicNumber, SchematicSymbol + +mock_input = """ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. +""".strip() + + +def test_schematic_number_extend_digit(): + sn = SchematicNumber("", 1, 2) + assert sn.number == "" + sn = sn.extend_digit("3") + assert sn.number == "3" + sn = sn.extend_digit("5") + assert sn.number == "35" + sn = sn.extend_digit("1") + assert sn.number == "351" + + +def test_parse_schematic(): + assert Schematic.parse(mock_input) == Schematic( + [ + SchematicNumber("467", 0, 0), + SchematicNumber("114", 0, 5), + SchematicNumber("35", 2, 2), + SchematicNumber("633", 2, 6), + SchematicNumber("617", 4, 0), + SchematicNumber("58", 5, 7), + SchematicNumber("592", 6, 2), + SchematicNumber("755", 7, 6), + SchematicNumber("664", 9, 1), + SchematicNumber("598", 9, 5), + ], + [ + SchematicSymbol("*", 1, 3), + SchematicSymbol("#", 3, 6), + SchematicSymbol("*", 4, 3), + SchematicSymbol("+", 5, 5), + SchematicSymbol("$", 8, 3), + SchematicSymbol("*", 8, 5), + ], + ) + + +def test_schematic_part_numbers(): + assert Schematic.parse(mock_input).part_numbers() == [ + ( + SchematicNumber("467", 0, 0), + SchematicSymbol("*", 1, 3), + ), + ( + SchematicNumber("35", 2, 2), + SchematicSymbol("*", 1, 3), + ), + ( + SchematicNumber("633", 2, 6), + SchematicSymbol("#", 3, 6), + ), + ( + SchematicNumber("617", 4, 0), + SchematicSymbol("*", 4, 3), + ), + ( + SchematicNumber("592", 6, 2), + SchematicSymbol("+", 5, 5), + ), + ( + SchematicNumber("755", 7, 6), + SchematicSymbol("*", 8, 5), + ), + ( + SchematicNumber("664", 9, 1), + SchematicSymbol("$", 8, 3), + ), + ( + SchematicNumber("598", 9, 5), + SchematicSymbol("*", 8, 5), + ), + ]