瀏覽代碼

Solve day 3

Taddeus Kroes 2 年之前
父節點
當前提交
bc8178cc57
共有 2 個文件被更改,包括 179 次插入0 次删除
  1. 39 0
      2023/03_gondola.py
  2. 140 0
      2023/input/3

+ 39 - 0
2023/03_gondola.py

@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+import sys
+
+def parse(inp):
+    numbers = []
+    nmap = {}
+    symbols = {}
+    for y, line in enumerate(inp):
+        num = ''
+        for x, char in enumerate(line):
+            if char.isdigit():
+                num += char
+                nmap[(x, y)] = len(numbers)
+            else:
+                if num:
+                    numbers.append(int(num))
+                    num = ''
+                if char not in '.\n':
+                    symbols[(x, y)] = char
+    return numbers, nmap, symbols
+
+def neighbors(x, y):
+    for nx in (x - 1, x, x + 1):
+        for ny in (y - 1, y, y + 1):
+            if nx != x or ny != y:
+                yield nx, ny
+
+def gear_ratios(numbers, nmap, symbols):
+    for (x, y), symbol in symbols.items():
+        if symbol == '*':
+            nb_nrs = set(nmap[nxy] for nxy in neighbors(x, y) if nxy in nmap)
+            if len(nb_nrs) == 2:
+                a, b = nb_nrs
+                yield numbers[a] * numbers[b]
+
+numbers, nmap, symbols = parse(sys.stdin)
+partnrs = set(nmap.get(nxy, -1) for x, y in symbols for nxy in neighbors(x, y))
+print(sum(numbers[i] for i in partnrs if i >= 0))
+print(sum(gear_ratios(numbers, nmap, symbols)))

+ 140 - 0
2023/input/3

@@ -0,0 +1,140 @@
+...........................................751........501...................................890.231...............829..168......143.........
+......................................*.........841....*....../................+..311.......................441..........*...........202....
+.........332...60....537..697.......901.................609....678....261.....90................870....519...........272..449.......%.......
+840.........*...........*....*..968......273...440.415..................*..................&......*...*......447................883...&.....
+.....34....651.786...646...804.*..........*.../.....-..........=...............94......96..760.222.....637.%...&.290...55..368.....*..565...
+.518*..........+................717......80.............231.....610...810*942..........*...................640...*................499.......
+...................189......................772.........*........................550..626...676-......213......432...........790.......834..
+......995..=..........&..955...........391......387...825..362........481...334..*................579.*...84%.......506./...................
+........*.700..............*..........*.......+..*............*...170.%..........733........658..*....832............-..777..616.915........
+......322...........977.526............852...410.14.........28.....$.........831.....%.....@.....586.........69.....................+.......
+...............................30.........................*............./.....*.......505....274...........=.#....344...52.308..............
+.........123*504.................-...-..................15.968.....427.439...456..............*..935../..441......*.....*..*.....195.794....
+..729.............186...............786..476*168.653*.............*........................148...+...962...................271......*.......
+............23.......................................435.491*4..40...-...26.......11..133..................*758.-....215....................
+466...656..=............957.........463.............................372..*..............................894.....661.......903.......%283....
+..................421...*.................32..946...%....668.886..........30.682......108...........................292..................189
+....759......379.*.....380.......................-..942..=....*...891......../.......*....193.......703..946/......./...................*...
+.39*.........*...847............80-..........................886.*.....+.............418.@......763............432.....................873..
+...........916.......................335..369.....................651..624.445*809.........*695...*..267.......+....666.641.888.480.........
+...@38...............685..149..........&.*..........312..................................27......544....*.420.........*....*..........*750..
+................&.....*..*...............208...........*...361.....&.....569................*986.....295...*..751..499.............549......
+.....@.......626...390..761....*.............$.........319....+.289..250*................189.....947......804....*......742*................
+.....660....................342.582.......24..611..................................354.*............*.............997.......595....874......
+...................................................109.........*...108.........252.*...168.@88...973................................#...$...
+214.....161..............499-.......................%........497...*....419.....*...52.........................83.........*5.............739
+.......*...............................156..........................88........798..............204...............*.....444.......974........
+.108..39..................*651.395#.....*.............963.......911...............886.................532.....417...................-.......
+....*..........921*82..321...........597...@321..409.....*254....../........237...*.......................&......./...................=308..
+.954..110........................................../.................478....*.....423.........990....187.429..#...506..........614*.........
+........#...211.............................575.....................*....893...13.........811*................673.....85...........465......
+..................393...........782.............-166.........=.831...401........*.348....................477...........*.819..#261..........
+.....761..........*...536.....+....&.................549..427.....*...........924.@......283.50............*........795...=.................
+....*....-.51....617./......954...................................248...................*........776....407.................................
+.756..986..............326........516............387..342....336............142=.823..354....233.............$...........523........336.....
+..........+495...856.....*....363....*188.177.4..+..............*....=512....................*...835......458..348..........*.......=....445
+................*.........638.$..............*.......214.........190......293...........&.128......*............*..167......711.........*...
+...565.......842.................#.....................@...............=..#...........195.......984.....201#...530...................640....
+...*...........................289.401.......251...626..........198*..277....$..............%.......................719..........964........
+...397..910+............................$....*.....&.....936..............893../...........249.....#334.299.../503..........*....*..........
+..............................918-...420...405..............$...226.729.........910........................*.........892....76....511.......
+...674=...282*418......#................................................962..........=693.....760..........499.908/..../.......$........*926
+.......................906..../......485*..........*......9....+....564*.........132.............*..........................137.............
+...........................300...........960....503.572......490............608*.......*....504.979..25......230.................925...@....
+..673............787..............................................433*164.......961.956.596.........*...610...............340.......#...765.
+....*....................8*............217*805..................*..................................427..*........680*.......................
+....247..............699...848....................651........137.870.673............696..................660.........432...593=.........906.
+..........209.........*....................327............................%..........%................3.....................................
+.....%.../.....482...708...782............*..........520..................947....390...420.....%..............456.....667*544............732
+..305.......%.....*......%....%....220...419....608..*.............282............&....#........269......117-....*834.........546#..........
+............137...145.128...........%.............*.555.............*.......................=...............................................
+....988.299.....................940.......978..971........437.......79..........*.........238.497...490.912.539.........85......157-........
+..........*.660....335......../...%........................*..224........@...556.368.............*...*...*..*...551......................474
+291..217..1..................321......-...................17.....*.......810............411......573.688...273.....*....$.............../...
+....*.........=.......701............781...*....................761...........315........*........................895.445...................
+...............467.......*...............444................................/../..........925..530+....50.....-.............................
+........+..........545....970........./........767......543..............953........389.................$.....393......#....17....83........
+.668.....577...&..@............803.....931......*............829..226*.............*.......-....114................=..9.....&............298
+..../........239.......101...&...*..................419.....@.........922..17......598....704..*..................722...........768.........
+.................776..*......156.163............419...@....................*...................465..+....................206=......-.*......
+....506.891-.......=.499....................@......#..........992..664....270......*395..............519.......666..............37...566....
+....&........251.................671.=.......474......429....*..../.............776.........#.............260.&....319......410*............
+............*.....46...772.........#.441.........340=..-.....393.....452..../..............219......487..*........*.....697.................
+......993.507.............*.........................................&......515.231..#988........./..$...519.=...435.....*...........753.....
+..177..=.......*17.........341..62.....361...820.....647+.........................*...........939...........980.......7.145....310....*.....
+....*.....113.....................*17.....*..................583....585...........160..571............................*..............75.....
+..937.163....*857...........322.......233..436..973................*............................678..................304.....#140...........
+.........*............@................@.......@......412..521......917......+.............................................@..............32
+.........233.........5.......649.230..................*...%..................513....304.........745...85..31....799........958..............
+.....983........@...............*.....#......%.....340..........=....745...............*565........%..*..........*.....835.........../503...
+.....*.......558....71.............518......546..........148..439....*.......567......................870..846$.519.......#..../17..........
+....945..381.......$........671@.....................390...........870..........+..%.........*..198..................347-...............164.
+....................................................@.....-...............318......555....234.9................@..................134.......
+..............489$....=608.............................&...40.*.................................................291.274...489.690*..........
+.....=485...........................633.462....+......490......370...............-......740..........416*..............*....................
+....................................*.....*.....125./.....#656.....679.....227....455.-....*.......*.................414................552.
+.................227..............733.$....787.......532..............*879...-........681...148.188.426./.......547.........=.......645.....
+....+..21.....75...*.....797...........73...................491...12............27......................727...@...*........912..............
+...258...*572.....955.......*..&...585.........*.......800.*...............@...-............................357.........................+...
+...........................415.389.*........973.738.%..-...759.968....477..192.......553...#.......398..170......900..../..986..834$..758...
+.........10......816..676..........356..............78............*..%..............*.....372......./......*..........322...................
+.....122*..........*......%............590.............870..920..911............246..452................740...............199...............
+...........444.....738....505...@.498...*...=..........*...................................946...................+.*......*.................
+.....675.....*................180...%.957..335.295...&.222....-..........281..625....@....=.....695...........173..652......141...-.........
+.......*....860........842.........................453.......809.....167....*..*..308..........*......902.101..................+...894..130.
+.....834...............*....=138.........................224.....301...$..663.9.......58.....899.........*..................................
+........................948......................908............+...............#................74........384.454......69.....*724.........
+...............4............84........990.949...*...................480...961..230......18........#..367......*.............................
+......496.......*289..........*11............*..........139*129....@.........*........................*...255................532.....607.443
+......*................888....................812....................876..364......965....105.....446.612...*..12*............*......*......
+..374...833.........19*.......904.........432.......153.....78/...............267..........*..............800......285......890...618.......
+...*......*...479..........*.*.................648.....+.........71...........*........53..216.686............3/....@..498...............346
+.739......28.....*760....112.725..145......133.../.#71.....737....$..*.....407..228......=........%....................=.............407*...
+.....619............................=.........&.............../.....932........&.............................296.........348.+166...........
+.......*.............22...-917......................................................-317........................=.........#........=.....111
+.......263...............................$........829......=...418-...........$..............599..304.............@..........849$..442..&...
+..792%...........*880....499...........719....415*.......853................928...47...*59...@...*....679.......744.........................
+.........590..589..........*...90...................*...............#282.........*..............568..$..........................736..383.321
+.........../.........481....87.*.................831.674....430.............270..393....................%65......................*......*...
+....864.........809....*........922.979......................./....461.545.....@.....738........-.....................355.....646...........
+.......%......@....#....539.........$.................*122............*....992...898*.....801.497.......#...&...@.................214..862..
+.............393...............................964.953..........503@......*........................764..143.17..385...........525...*.......
+.......87.49......444#....................&............................108.....801..............*.....&................866.......$.334...626
+............*120.............736.971....595........715...462...=.....-........=.................754.............@.........*629..............
+.....331............620........*...*.........252.............987.=...846..........163..941.............-219...719...231*..........=.........
+....../.........468*..........734...939.........+..220..934......170........745..-.................*....................635......650........
+.550.......&..........................................%.*.....................*.......&.........261.346.237..................#..............
+...*.528..303.546..........357.....878.....571..........755...619...661.....803.358.335....67.......................496...259.........25....
+.938.+....................*...........+.......*649.............*..../.....................*..........................*..........961....*....
+...........975........-....745...........312................134...........253.98...........362.......716.............94.+...620...+.-...62..
+............*.......123..............429....*26.......590............836&.@...........$..............=..................485.........821.....
+....555......924...........287........../.............*............................837..........408.........................................
+........265............924*.........784......382......354.............255..280.896...............+..........537....936....524...601.........
+.............495........................177.....+...........148...628*......*............691.........776=..*......*......*.......&..........
+.....617....*....................832.....#........278*651...*...............767.......16.......66..........857.956.....145..............817.
+.......*...560..........906..........635..........................526*.............44*...%....@......@981......................=...624.*....
+....446............-.../................*....................%........798...............796.....................448..........839...*....941.
+.................523.......%.990..828-.537......22........771....251...........938..884........782....866..................@........776.....
+.808...255..............527..$..............&......709*.............*....=....*........+.......*.........*..........908..659.675............
+..........=.-694.................225........603........523.......617...201.....85..............430.......982...62..=..........*...+...118...
+.......=..........672............*.................713...................................120..........&.....................237..733........
+........723....77*.......765........453....296.......*...........................392.554*.......@......770.878.569..../..............757....
+........................./............*.......@.....518.................360*.................272...602........*....456....614/..............
+..............*677............593.460.51..571.................136..758.................422........+.........................................
+...........265.......974....../...........=..............142..................82......+.........$......591*...........$......&..............
+....542........44.......*297.......................=.......*.......516.......$.................113.........198.......792..244......391......
+......&...$........737*.............743......741.64.....688........*...........647.986..+588..................................=......&..129.
+...........4...........14..............$.397*................896..832...810*.........*.............656...........996..........618.......*...
+.......765..................887....8..................226.......%...........149......884.678..........*...358....*................915....956
+........*...................*...../....514.............*................995......429.......@../969...434...*......955....../..210*..........
+.....672....@......@.......322.........*........404#...784.............../..#884....*......................506..........984..........708....
+..........757..712.195...............188.....................895...................326...501.480..&524..........998./..........996.....*....
+..972.160...............857..293..............274....-...-...........807...%............*....................+...*...128..313.........478...
+...*..*...804............*...........334..110*.......974.459...........*....801........727.595..778.........699..800.........*..............
+.934..798.%...............37.....191.............*............@.......75...........486......*.................................323...........
+................................*......387....477.200......360.......................*....962...47................................179.......
+.....................884........985...................................67..145*.......292................191..323...213..............*.......
+.....................$....860.............700.147..................$..&.......334........282..267...........*.........-.93..867........525..
+..............492.......&...*....91.......%...*....779.......-691.93.......@.........710.*....*...=...310..........97.....*./..........*....
+..........568*.......708...216..............780......*....................312........*....438.....346....$..$327...=......5........756..855.
+.......................................261......807.261.............................42......................................................