From f564d01084bdc0ffc246daf1cfe0ca18f52ea1e9 Mon Sep 17 00:00:00 2001 From: Colin Sullivan Date: Mon, 4 Dec 2023 23:29:39 -0500 Subject: [PATCH] Solutions for Day3, Part 1 and 2 (#5) * day3 part 1 and 2 * version bump --- data/day3.txt | 140 +++++++++++++++++++++++++++++++++++++++ docs/docs/CLI.md | 56 ++++++++++++++++ docs/docs/day3/part1.md | 1 + docs/docs/day3/part2.md | 1 + docs/mkdocs.yml | 3 + pyproject.toml | 2 +- src/aoc/cli.py | 36 ++++++++++ src/aoc/day3/__init__.py | 0 src/aoc/day3/part1.py | 122 ++++++++++++++++++++++++++++++++++ src/aoc/day3/part2.py | 89 +++++++++++++++++++++++++ tests/test_day3.py | 21 ++++++ 11 files changed, 470 insertions(+), 1 deletion(-) create mode 100644 data/day3.txt create mode 100644 docs/docs/day3/part1.md create mode 100644 docs/docs/day3/part2.md create mode 100644 src/aoc/day3/__init__.py create mode 100644 src/aoc/day3/part1.py create mode 100644 src/aoc/day3/part2.py create mode 100644 tests/test_day3.py diff --git a/data/day3.txt b/data/day3.txt new file mode 100644 index 0000000..3b50562 --- /dev/null +++ b/data/day3.txt @@ -0,0 +1,140 @@ +.........................3.......................................94...............806....................596.........793...........186...... +.../..........*574.587..*........161......904.......412.........*.................*.................................=.....637.%......*...... +..614..831..33.....*...........@....*398..&.....690*............183.........503..916..790................................*.....256....632... +.........*.................332..374......................%184...........467..........*....969.632.......26....457......440.........@........ +..........535...541=........*.......696..@..323..................93=.......*.......30......*....*......-........+.222$.......458.817....66.. +....*.......................885.....*...123.=...641...&188..577.......339..688.........287.684..219.........................+...........*... +...61........397...#386...=........313........-...&............*......*................@.............../.........621+....................169 +..............*.........34..................934..........17..168..320..263.........412.......$......966.....................303.....554..... +...............353.447.......@.....251............54....*........#.............149....*...871..964.................714.....*....403...*..... +...805......$......*.......632.....#...+514.944.....*....48..........*133.959.*....814...........-.521...........................*.....398.. +..../.......906.289...238.....................*................772........*...591......260.122......*........309...677...357.....790........ +........#................/.........450.....594.................*.......705............*....&.....676...@954.%....@....&.*................... +.....310..928...+.............488.*.............790.........286..982.........416*944..103.........................351...519...705........... +..............872.....+........*..820.....5....*.....529.........*...........................@..884......356*449................*..596...... +...................780......507.........$..*...936.............406...........*560..704....931...+....618..................%243.817...*...... +.386.....937.+904......*.............540...320............702.............118.........*............../.....445.....291.............899...... +........*...........930............*......................&......204...............243....*948.....*..........*...*.......512$.............. +......956....................$..788.487...........894.955..............................479.......709.........57....374................930... +............/534..260*33..680...............324...*....*......574......219................................................../934.......-.... +..261...........................*813....=...*..........378......&....-..%...198............................885.........666.................. +...*....../........905$......706......281...314...642......701....736.......*.................................*..........*...532............ +..235..400....670.........-........................../......................849...204...197..646...............548.......61.*.........538... +.............*.............927....340...........641..............................&......*...*.........137..144...............513............ +..400./285...16.......704............*896..836.....*..........738......102.&190..........89.137............*...$582....................&.... +...*.............763...#....541....................629...........*646.............295............368@....&..62........990...@428........955. +....218...650.....*.........%................269............669...........709.......+..810.............52........447...@.........784........ +.............=.....313.............*........+..........444....*.%.........................-.....225.......684...*.........../107...*........ +..66...643.......................87...770.........=.......@.122.495.......177.....226.974.........*.580..*......921....216*.........724..... +...*..*....................328.........../...460.325.....................+...........*.........198.....-.316...............912.............. +..882..421.......%766.........*..............................978*..........124.110.......$.....................467..............780......... +...........732.........531.....439...%...........................230..........*....396..126........................508...........*.......... +............/..........*.............401..69.........$.................630............*.....114.......959...81.............515..664......43. +......-............491..929.363*.............178..686.....546...................628...4.......%......+..............106...*.........*....... +...456....154..659...+..........111.*...........-.........*...............66.%...*......%...................583../1.*.....206.......439..... +...........&...$..........548.......560................431......752......@...574.534....303...843../.................844.................... +.........%.....................320%.......871*.....647..........*....................89.............981..................................... +.92....832.....194.804.270.992................388..................%..583*..........*......255..730.............................138..121.... +.../................%..../..*.........58.106......*..373.........107......964...52.910.465..*..*........15...@.....*14.=847...........#..... +......850...503......................*......*....411....*....674...............*...........532.124.......*.471..380......................... +......*........*...390..641......99...997..688..............-......403...6.....156.....................320................*721.............. +.......471....555..*.....*......*.../................749...........*......*..........92*756................336.....241.581.................. +...................904..762..681..695.&639..903&......*.........753........936...................683-..........921..*......654....7.470..... +.........16*494.....................................395..112...........=..................601..........+..........*.901.......*....*........ +................45...........77............$..636.........*...@593...670.........*796.270..*.......%..835.501...190......369.618.%.......... +......918..@....*....597...........758..550...$...895...688...................952.....*...761...176........%..............*......261........ +.....*......957.108.....+.....788................&..........221..........-308.....$..567..............562-.............963...833............ +....431.484...............35.+.....476.902.....................$.....495.......137...........880.199.......306+................*..725....... +..........*..............*.....312...=.....50-...687...274..........................-........%.....*.................*......930...*....349.. +.....197...748.........639........=................*..*.........601*.......19....995..............470.....81.954..684....82......872...*.... +....=................................996........457..208.403........379...=...............193..........61...*...........*....37.........248. +........155...................%........*.......................................689.211...+..........52*..................969..*....922...... +......*.........151......698.723.....175..10...........&............................*......852.486......442...900....934......60.....+...... +...103.896..142*.................430.....*..........-...743.........346&.................#..*...&........*.............*.........197........ +......................811........*..................813........802.............#333...167...916...856.....590..........658....-....-...563.. +..............838.275...-......529.............718.......502....*.................................*................284.....967.............. +...8....64.......*............................*....870......*.901......606.226...................534........773....*...............*43...... +....*.........57............338*142.......589.525....*...563...................745#......152..............%..*..298..206.................... +.....311.....=............................&.........309........630.....238................*.....345......701............*.....235....541.... +..................288...596...45...............953.............%.................@....379.746......-..%.........937....571...........=...... +161@................&......*.....47.=.........#......586.....&...281............764....*...............903.=810..-............837........... +............................944..&...650.............&....918.......*..74...............369....437&............................*........777. +......146...*..................................772*.................52...@......*895.@.......................371.......*76.....282.......... +......*.....278...696...........365....=334........786........879.=..........983.....877......@38........784..*.....255....&................ +.....132.............*.849........*........................%......783...553.................................&..663......297........419.440.. +.977...........839&.......=.393&.77....895*770............636..........................-803..353+......-.......................406*.....*... +...*.=....376......................................69..........&.....116*949.......................186..295............%.................833 +261...853...*...81........993.191.810...731..........*575......636..................306...........*............129....691....999*.+712...... +...........319...&..........*..*....*....-..903*187....................890....875......@........557............&............................ +...........................840..403.255.............*.......138.........*......../.841..................257........499.........*........%... +....430.....283.........................897......566.74..11...........705.641...................71*104.*...........*........197.782......290 +.....................605..........401..../....................690....................633...............957........739....................... +.280*135...706...........709.........*................388..............813............*.....906....%..................606................... +..............*.=965...............669.......944$..............................288.131.........-.183..594...............*..852..819......... +.......#513.676......667...............93..........#.....$.......984.............#.....511.....................172.283.323............+426.. +......................%....826........$...........709..91...........*....919..........*......................./....*..........%............. +....................$.......*....829........656......................400.*....................995.798.203..........107.994.....598.543...... +.........88.../..568.......194......*931.......*654..............*........533....................*.....*................+................... +............844................491.......642..................839.603.125.....777...96..24.312.........272................671.394.....532... +........85............/...@........*339..%......914...601.............+........*.....-....*......251.......29.....962.......+........@...... +...969...............916.265....630........791&....*.*..........$.........-166..307.................*........*543.&.............445......... +..........811.457...................942.........755..944.......108..254...................322.....695....250................86..*........290 +..........+....@............-871.......*....602......................*.......................*........15.=...........274.......637.......... +.....................&810........894-..130...*..........................................315...76.....+...............*.....55............... +..........................261.................894..582.....................................*............14....%...737.....*.........+....... +..375.987........803..69.*........&....80#................685.....386.....................602....10.....%......65........932./..742.24...... +.........@.608..*......*........563........630..947.................*..........234...728..........*..........................92..*.......... +.....89.......+.550....43........................*....471......286..517.510....../......*..........966...795............$.........813....... +.....*...651...............#.../573..569..........239.*..................*...........591.....935..........*...........536...536............. +.....344.*.............681..90........*........./......43.........../...473.....................=.....679..545.................*843......... +..........610.72.952..................850.......81.........733......895.....975..........132..........*........+898.......583.........656... +790=.@179...........*........$...............................&..............@....935.......#...270..621...................#.............*... +...................618....366...776..................143.......115.../..............*....*.......=...........306.........................369 +..............258................*...97........./967.-........*.....281...%............32.65........43.721.....*...........738...462*....... +..606........*................713.....*..................513............114..746...............................86.............*......507.... +......402.....200...983...............746...335......938........488.........*......$.649.............................214.......493.......... +........*..............*32.....................*381..*....46.......*..205..16...100....*.444#....&..522........896..*...............411..... +.......583........./................734..19..........795..*.....219...*.............804.......391......*............881................&.... +..............93....228............%.......*...985........936.......%..........=..+........................361.692................784....... +.....219.................808..........755..........233............348.......463....147..............162.....*.......464$.................... +.......+....&...876...+....*..461$...$.............*.....550...........456.............826.330.........*997..657.................763........ +..../......143....%...87...............225..160*8.66.......+.....#623..*..................*.......612............709...679*........$........ +.....271..................#.....610.....................................799......*....866.....965...=.456........*.........530......./...... +.........10...#495..508.521.......+......746.........165.........-978.........638.145...*.576*...........*......926.............468...267... +...........*........*.................&....$...........*...294..............$.........822.................535.........727.351............... +...671..233.......903....541.....421..760..............299.*..............570..............540.......947.............*....*...802........... +.....#........920.........*..455*...............942.........953..390..................365.....+.........*..........779...55.......293*...... +.................*........40......50..827........*..............*......................*..&.....-....374..............................80.... +......#...........636.274...............=.......72.............785...+596.............36..59...843............206..+625....%................ +......767.......*......*..........948..............*.345..................#..........................927..861....%........701...........64.. +.242.........476.363...936....347.*.........@.....41...............31...362.447...+902.....538........*.....*......+...........262&......... +......#.........................&.517..*911..791...................*......................=....187.....926.800...54......108................ +.593..829..............................................166.524=.....................+381........+..*.................468*.............&..... +..........766*......273...347.........347..........$...............836......436............843=....473..@.....=344...............573.675.... +..............279...%........*...786..........-..100.860..........*........*....421.....................414.................616...#......... +...322....496............334.648..*....75...520......*......881....87....120....$..............*.............79.............*..........*.... +.....*......*.....................367.............704.............................&..........59.542.........*.......-992..53...811..192.556. +...308....485...948.......................383*559........282.......15.........298.774.......................377...................*......... +...............+.......348........................&.809.-.....249.......699../............740..650...584...............801*563..721..470.... +.........343..........................970...324.782................324..*...........380...*...*......*....-.........................*....... +...627$....*...713#.....799...420........*....*......................*...24..........$..131.......148......245....94*781...%.......302...... +........698.........635.+...............644..593.%......784.....-....644......................409........&.....*...........952.............. +.999.................*............................627.........866..........418..........483*.....$....227...248.665...265.......222...&..... +...*...............320...396......%......................77........186........*991..................*.................*...232....*....255... +..363..35...=39............*.......831.......892.........@...=..+........268..................513..373.....875.......48....../....680....... +.........................259.............374*...............137..482.432...&...276....132........*.....798...&.................16........... +..669*246.......................491.............+...+..492...........................*.........=..603.@..........849........................ +...............429.220......-......*..........928..304...*......177................741...482.204.........*.........+....=........36......... +...218..449.......*..........214...620.................827.252..@...689...................*......598..460.473...........341.704../...434.... +......*....*548.....-679.149.................................%......*...371..........998..815...*...................................&....... +.......403................=.........166............................741.....&........%...........329.............62..............*........... +..706*......991*738..347.....571...*....983..........+..208...319...............406..................175#......*.................891........ +......392...........*...........*.409.....*........72......*.-.........233.......*...........................478.............256............ +...................559..................-.540..734........6....*.......-......771............398.729.............928#..+.................... +.....603.......472................%...892..............=....314.684.......712............206......+.....657.%..........844.584.456...=...... +......*..........*..#..........801...............738...524.................*........817.......+........*....57.............*..........749... +648..799........517.999...............#...........$..+.....................742..516*....939=..694...945..................863...480.......... +..........700........................994....314......214....105.............................................#....137............*...522..... +...153.....*.........685..283................*...151........*....#....232......$.......99.92...863....*.....567.....*.285.....69.....*...... +............205.........*..*..............275....*.........220...644...*....293.........$..%..*....337.91...............*.............963... +.......................844.32......449..........932....................869....................77......................288................... diff --git a/docs/docs/CLI.md b/docs/docs/CLI.md index 1b3d587..5fd84d8 100644 --- a/docs/docs/CLI.md +++ b/docs/docs/CLI.md @@ -19,6 +19,7 @@ $ aoc [OPTIONS] COMMAND [ARGS]... * `day1`: Command for running day1 stars * `day2`: Command for running day2 stars +* `day3`: Command for running day3 stars * `debug-version-info`: Print debug information to terminal. ## `aoc day1` @@ -93,6 +94,7 @@ $ aoc day2 [OPTIONS] COMMAND [ARGS]... **Commands**: * `part1`: Calculate the sum of all Game IDs +* `part2`: Calculate the sum of all Game Powers ### `aoc day2 part1` @@ -112,6 +114,60 @@ $ aoc day2 part1 [OPTIONS] [INPUT_PATH] * `--help`: Show this message and exit. +### `aoc day2 part2` + +Calculate the sum of all Game Powers + +**Usage**: + +```console +$ aoc day2 part2 [OPTIONS] [INPUT_PATH] +``` + +**Arguments**: + +* `[INPUT_PATH]`: Path to file containing the input [default: data/day2.txt] + +**Options**: + +* `--help`: Show this message and exit. + +## `aoc day3` + +Command for running day3 stars + +**Usage**: + +```console +$ aoc day3 [OPTIONS] COMMAND [ARGS]... +``` + +**Options**: + +* `--help`: Show this message and exit. + +**Commands**: + +* `part1`: Calculate the sum of engine parts. + +### `aoc day3 part1` + +Calculate the sum of engine parts. + +**Usage**: + +```console +$ aoc day3 part1 [OPTIONS] [INPUT_PATH] +``` + +**Arguments**: + +* `[INPUT_PATH]`: Path to file containing the input [default: data/day3.txt] + +**Options**: + +* `--help`: Show this message and exit. + ## `aoc debug-version-info` Print debug information to terminal. diff --git a/docs/docs/day3/part1.md b/docs/docs/day3/part1.md new file mode 100644 index 0000000..2584244 --- /dev/null +++ b/docs/docs/day3/part1.md @@ -0,0 +1 @@ +## ::: aoc.day3.part1 diff --git a/docs/docs/day3/part2.md b/docs/docs/day3/part2.md new file mode 100644 index 0000000..897bc9a --- /dev/null +++ b/docs/docs/day3/part2.md @@ -0,0 +1 @@ +## ::: aoc.day3.part2 diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index cd280e2..63460ca 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -13,6 +13,9 @@ nav: - Day 2: - Part 1: day2/part1.md - Part 2: day2/part2.md + - Day 3: + - Part 1: day3/part1.md + - Part 2: day3/part2.md theme: name: "material" diff --git a/pyproject.toml b/pyproject.toml index 542ad96..1b82170 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "aoc" -version = "0.2.0" +version = "0.3.0" description = "Record of current and past attempts at Advent-of-Code in Python" authors = ["Colin Sullivan "] readme = "README.md" diff --git a/src/aoc/cli.py b/src/aoc/cli.py index 71f6821..70a3f20 100644 --- a/src/aoc/cli.py +++ b/src/aoc/cli.py @@ -8,6 +8,8 @@ from aoc.day1.part2 import word_calibration_sum from aoc.day2.part1 import sum_valid_game_ids from aoc.day2.part2 import sum_game_powers +from aoc.day3.part1 import sum_part_numbers +from aoc.day3.part2 import sum_gear_ratios __version__ = version(__package__) @@ -143,3 +145,37 @@ def day2part2( cli.add_typer(day2app, name="day2") + +day3app = typer.Typer( + help="Command for running day3 stars", + rich_markup_mode="rich", +) + + +@day3app.command(name="part1") +def day3part1( + input_path: Path = typer.Argument( + Path("data/day3.txt"), help="Path to file containing the input" + ), +) -> None: + """Calculate the sum of engine parts.""" + with open(input_path) as infile: + input_text = infile.read() + total_sum = sum_part_numbers(input_text) + print(f"The sum of the engine parts is: [bold red]{total_sum}[/bold red]") + + +@day3app.command(name="part2") +def day3part2( + input_path: Path = typer.Argument( + Path("data/day3.txt"), help="Path to file containing the input" + ), +) -> None: + """Calculate the sum of all gears""" + with open(input_path) as infile: + input_text = infile.read() + total_sum = sum_gear_ratios(input_text) + print(f"The sum of the gear ratios is: [bold red]{total_sum}[/bold red]") + + +cli.add_typer(day3app, name="day3") diff --git a/src/aoc/day3/__init__.py b/src/aoc/day3/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/aoc/day3/part1.py b/src/aoc/day3/part1.py new file mode 100644 index 0000000..dc6eaf8 --- /dev/null +++ b/src/aoc/day3/part1.py @@ -0,0 +1,122 @@ +""" +# 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? + +""" +from collections.abc import Iterable +from dataclasses import dataclass + + +@dataclass +class Number: + """Dataclass for containing a number and it's position""" + + value: int + row: int + columns: list[int] + + def __hash__(self) -> int: + """The hash of a Number is its position. Used for making it possible to use as a key in a dictionary""" + return hash(f"{self.row}, {self.columns}") + + +@dataclass +class Part: + """Dataclass for storing the position of a engine number""" + + row: int + column: int + + def valid_coodinates(self) -> Iterable[tuple[int, int]]: + """Get an iterable of valid positions for engine numbers + + Returns: + A generator of position tuples for valid engine numbers + """ + for j in range(self.column - 1, self.column + 2): + for i in range(self.row - 1, self.row + 2): + yield (i, j) + + +def sum_part_numbers(input_text: str) -> int: # noqa C901 + """Get the sum of all the valid engine numbers that are adjacent or diagonal to a engine part. + Traverse the grid to accumulate engine numbers and parts and making a lookup from coordinates to engine numbers. + For each part search the lookup for using it's position for valid engine numbers and at them to the set. Return the sum. + Args: + input_text: Raw input string + + + Returns: + sum of all valid engine numbers + """ + + parts = [] + coords = {} + + lines = input_text.splitlines() + for i, line in enumerate(lines): + num = "" + cols = [] + for j, char in enumerate(line): + if char.isdigit(): + num += char + cols.append(j) + elif char != ".": + parts.append(Part(i, j)) + if num != "": + n = Number(int(num), i, cols) + for col in cols: + coords[(i, col)] = n + num = "" + cols = [] + else: + if num != "": + n = Number(int(num), i, cols) + for col in cols: + coords[(i, col)] = n + num = "" + cols = [] + if num != "": + n = Number(int(num), i, cols) + for col in cols: + coords[(i, col)] = n + num = "" + cols = [] + + valid_parts = set() + for part in parts: + for valid_coord in part.valid_coodinates(): + if valid_coord in coords: + valid_parts.add(coords[valid_coord]) + + return sum(part.value for part in valid_parts) diff --git a/src/aoc/day3/part2.py b/src/aoc/day3/part2.py new file mode 100644 index 0000000..c24e9c8 --- /dev/null +++ b/src/aoc/day3/part2.py @@ -0,0 +1,89 @@ +"""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? +""" +from aoc.day3.part1 import Number, Part + + +def sum_gear_ratios(input_text: str) -> int: # noqa C901 + """Get the sum of all the gear ratios. + Traverse the grid to accumulate engine numbers and gears and making a lookup from coordinates to engine numbers. + For each gear search the lookup for using it's position for valid engine numbers and if there are exactly 2 add them to a set. Return the sum. + Args: + input_text: Raw input string + + + Returns: + sum of all valid gear ratios""" + + parts = [] + coords = {} + + lines = input_text.splitlines() + for i, line in enumerate(lines): + num = "" + cols = [] + for j, char in enumerate(line): + if char.isdigit(): + num += char + cols.append(j) + elif char == "*": + parts.append(Part(i, j)) + if num != "": + n = Number(int(num), i, cols) + + for col in cols: + coords[(i, col)] = n + num = "" + cols = [] + else: + if num != "": + n = Number(int(num), i, cols) + + for col in cols: + coords[(i, col)] = n + num = "" + cols = [] + if num != "": + n = Number(int(num), i, cols) + for col in cols: + coords[(i, col)] = n + num = "" + cols = [] + + cum = 0 + for part in parts: + possible_nums = set() + for valid_coord in part.valid_coodinates(): + if valid_coord in coords: + possible_nums.add(coords[valid_coord]) + if len(possible_nums) == 2: + part1, part2 = tuple(possible_nums) + cum += part1.value * part2.value + + return cum diff --git a/tests/test_day3.py b/tests/test_day3.py new file mode 100644 index 0000000..0e502ae --- /dev/null +++ b/tests/test_day3.py @@ -0,0 +1,21 @@ +from aoc.day3.part1 import sum_part_numbers +from aoc.day3.part2 import sum_gear_ratios + +input_text = """467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598..""" + + +def test_sum_part_numbers() -> None: + assert sum_part_numbers(input_text) == 4361 + + +def test_sum_gear_ratios() -> None: + assert sum_gear_ratios(input_text) == 467835