2023 day 3.
This commit is contained in:
parent
53f05058f3
commit
d3eacd48aa
49
2023/day3.py
Normal file
49
2023/day3.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import string
|
||||||
|
import sys
|
||||||
|
from collections import defaultdict
|
||||||
|
|
||||||
|
lines = list(filter(bool, sys.stdin.read().split("\n")))
|
||||||
|
|
||||||
|
values: list[int] = []
|
||||||
|
gears: dict[tuple[int, int], list[int]] = defaultdict(list)
|
||||||
|
|
||||||
|
for i, line in enumerate(lines):
|
||||||
|
j = 0
|
||||||
|
while j < len(line):
|
||||||
|
# skip everything until a digit is found (start of a number)
|
||||||
|
if line[j] not in string.digits:
|
||||||
|
j += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# extract the range of the number and its value
|
||||||
|
k = j + 1
|
||||||
|
while k < len(line) and line[k] in string.digits:
|
||||||
|
k += 1
|
||||||
|
|
||||||
|
value = int(line[j:k])
|
||||||
|
|
||||||
|
# lookup around the number if there is a symbol - we go through the number
|
||||||
|
# itself but that should not matter since it only contains digits
|
||||||
|
found = False
|
||||||
|
for i2 in range(max(0, i - 1), min(i + 1, len(lines) - 1) + 1):
|
||||||
|
for j2 in range(max(0, j - 1), min(k, len(line) - 1) + 1):
|
||||||
|
assert i2 >= 0 and i2 < len(lines)
|
||||||
|
assert j2 >= 0 and j2 < len(line)
|
||||||
|
|
||||||
|
if lines[i2][j2] != "." and lines[i2][j2] not in string.digits:
|
||||||
|
found = True
|
||||||
|
|
||||||
|
if lines[i2][j2] == "*":
|
||||||
|
gears[i2, j2].append(value)
|
||||||
|
|
||||||
|
if found:
|
||||||
|
values.append(value)
|
||||||
|
|
||||||
|
# continue starting from the end of the number
|
||||||
|
j = k
|
||||||
|
|
||||||
|
answer_1 = sum(values)
|
||||||
|
answer_2 = sum(v1 * v2 for v1, v2 in filter(lambda vs: len(vs) == 2, gears.values()))
|
||||||
|
|
||||||
|
print(f"answer 1 is {answer_1}")
|
||||||
|
print(f"answer 2 is {answer_2}")
|
140
2023/inputs/day3.txt
Normal file
140
2023/inputs/day3.txt
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
......124..................418.......587......770...........672.................564............................438..........512......653....
|
||||||
|
665/...*......................*599.....*.983......794*..140..*...........@..963*....................445........*......*.........709.....*...
|
||||||
|
.......246.....581......701..........108....%.532........../.73..699...927............................*....579.354.464..............298..86.
|
||||||
|
........................*.....@...............%........$............+.........167..................408............................$..*......
|
||||||
|
....914......335.......513..245....106=...............974................749.....*.702.......589........803........*176..386.....631..340...
|
||||||
|
....*.......*...........................48...203@.............767......*..../.362...$......................*159.381.........................
|
||||||
|
..620.....430.....612.507.........365.....................335../........938.................154.........@....................682............
|
||||||
|
.....................*...........*..................470.........................889...........*.....@.489.....453$.329..334............86...
|
||||||
|
..........324...............431..58..533-../..-...../......*405.................$.............47..474..............*......*.......930.*.....
|
||||||
|
............/.....*350....................400.502...............$...........168.......855.635....................258.......794...+.....846..
|
||||||
|
........................560...72.945..............866..........783..328....*....116......*...........179..904........682$..........333......
|
||||||
|
.....674...........152....*....*....*815.........*........$609.............737../................583*........*.84..............767*.........
|
||||||
|
..../......55@....+........645.914................987..................*..........972.........#.......80..750........588................=260
|
||||||
|
.....................349&...................../.................754.407..203*720./.......207...14...............=88...+...767...............
|
||||||
|
.........*824.............890.......269....893..271*139..645....*...................233...%................428...........*.........79.......
|
||||||
|
..........................#............*.................@.../...316...844.............*........@439...287*......*974.....182...............
|
||||||
|
....*.....50.......671+.................267........634*....417............-.598.....531....891................331................358.....341
|
||||||
|
.883.561..*....428.........../14...742...........@.....654.....809../716.......*456.....=....*........$..............................607....
|
||||||
|
...........835..*..796*............*..............321......612*.......................299..203....962..431..........277.......40......$.....
|
||||||
|
......+591.....916.....294.........446..111......................237*77.....&........................-................*...150*....*......873
|
||||||
|
....%..................................*.....................819............522.................922................738.........214.595..&...
|
||||||
|
...552.........*...............+545.....627...........601......*..801..............867.....954....*.984.....752..........*830...............
|
||||||
|
............276...939.............................979....*.........*........866*.....*.......*..963.-.................172...................
|
||||||
|
278................*......@..........52........51*....851..766@..515.949=.......736...456...107............796.@668..................#......
|
||||||
|
...*...431.................616..............................................79..................651.806.....%.............554.........740...
|
||||||
|
.969...-............................-..........721.......555.657....+.........*....#....704........*.................556...*....196.........
|
||||||
|
................*228..........312.201.....490...%.........*..#...815........896..417.....=....890.....274....884.683*.......327...*.........
|
||||||
|
......@......742.............*........393*....=........715..............535...................*.........................529.....741.....#...
|
||||||
|
.......386........244......196...............815...........869....+580...*...................71........654...454..346=.$.............909....
|
||||||
|
....................................................265...$.............738..401...984...........265.....*......*...............-...........
|
||||||
|
.......=.....184....148....14.........685...990..................80..=....../........$.......511....%.....424..400......184..551............
|
||||||
|
.....71......*.......@............812.........&..$.....573*613.....*.677.......#.......299..&.......................933.....................
|
||||||
|
...=.......142...917...-989....76*....230...*....105.............920.....+......371.......*............660..692.........553*........%.......
|
||||||
|
341................&.....................=.29.............643.82......*...714............222....934......*....@.............178..581..+.....
|
||||||
|
...............874...129.......................739*971.......*.......176.............3.@...........*..219..40..........#.............168....
|
||||||
|
.....179..............*...741.......524...................................757.=633..*...136......63........*..........399...................
|
||||||
|
.......*....315....307....*.....%..*.............718......371....=....654*..........89...................194........................+.23....
|
||||||
|
.....737.........&........540.253...80...273......*.........&...773............492.................722........113....970..=195....702.......
|
||||||
|
..............869..........................*.539...434...............393*933......*........679.874..%......=....*...........................
|
||||||
|
............................$....148......43...*............@....................67.876........=......./..388..920......423.........-.......
|
||||||
|
..118*773.142....%565.......397....*.........352..#..........217......................*....865......257..................*........421.415...
|
||||||
|
............&.........898.............607..........897...631.................787...840.......*..............684.........34.359.........*....
|
||||||
|
........995....235.....@.........#.......*.932*.............*73...940..997..#..........&.&...942...$.727..........115.........*122....380...
|
||||||
|
................-................131...........265...827...........*....*............797.490.....845..*..........#....+823..................
|
||||||
|
............96.......................383.......................53.292....19...536......................42..668..................579......666
|
||||||
|
...-..487............680..&...*45...&........801.............*.*.................-........374.....................128..109.......%....*.....
|
||||||
|
.611.+..............*.....151................=......739....622..572................103...........683.....245..748*....*............298.67...
|
||||||
|
........*735......911...................562........@.........................458.....*...753........................275.362*................
|
||||||
|
.....683......702.....736.230.....457.........................13................*..126...........458........................890.........992.
|
||||||
|
...............*......*..........*.......499........10.........+...227.227...542..........................167....661....................*...
|
||||||
|
.............691.7....135.62..157..570..*....304......*...........*......*................$589....#946....*.........*............132....190.
|
||||||
|
........687.......*.........*............768..-...453..643.........844....706...%......................509........767......*................
|
||||||
|
...........*....485......859...........*.........................................92...268.........193.....................385.....991*722...
|
||||||
|
...-.....18..................217......853................................28..............*........@.......+......302........................
|
||||||
|
...103......60=..*352...........*916........351.....347..=..452.810....................304..........539.346.......*....................*....
|
||||||
|
......................610..........................=....990.....=...819....*496..797.................*........946..44..................261..
|
||||||
|
.......630.............*........882.........................173....*...............*....903....44.....318.212.@.......-.....................
|
||||||
|
.......-......-...........@..=.....*841.....812.......515......*....713....+.....566....*...................*......344..297....356.430..%...
|
||||||
|
.........*482..453.......279.554./............#..320..*.....................671......873........&...637$.....413........@.........*.....906.
|
||||||
|
....263...........................861......*....*.....908......365....123.......494.............134.................808.......*.....*.......
|
||||||
|
..........465.....520%.....................432.76..........160........*......................26.......218....14.......*......598.874.844....
|
||||||
|
...417......%.............138.....................$..............84..............272...573.....*.296.*................585...................
|
||||||
|
...*................596...&......................783.....992..........*....982.....*.........857..*...314...797..265........*....*..547.#...
|
||||||
|
....260........75....*...........389....616=.........5.....*.....695..427..*....780....-425......872..........*...*.......49....599.....19..
|
||||||
|
..............+.......389...........*33.........596.......600......*......67......................................567.......................
|
||||||
|
...802............................$.....302....*.....-93........434............$........554../339..............................277..........
|
||||||
|
.....*....................$.....822.....*.....89...........233..........602.....911.....#..........958............475......773*.........%989
|
||||||
|
..849.............228..868..217......679.......................99*...../............................*.......................................
|
||||||
|
.........253.........*......*....................643..............796......-200...355..469.........174......=........174....279.638.........
|
||||||
|
295*22..*............664...462.-238...................&765.........................*........................241..............$.......%......
|
||||||
|
.......937.....25....................422.264.................244...........628...340..................106.........................551.....82
|
||||||
|
897*.........../..60.......361.......*......#.....164.........*.......804*....%..........670*194........#.......#..........83...............
|
||||||
|
....754............*.................51..47.......*.........487...585.....202........838..................28...734...*457..............427..
|
||||||
|
............155...705......................*....912.....887..........................*....*875...........*........................&.........
|
||||||
|
..952.621......*.............+983...........832.....783....@.......432#...+........530.223.............456......462...257*100......763......
|
||||||
|
..........=..905........................892...............................262..85..........#..................-../.....................671..
|
||||||
|
.........994...............476.............%.665...524...53*........939*........*..........703..497.........186............=.567........*...
|
||||||
|
154...............$412......&..........-.....&.....-........41.109......282......676...........%....&80.@...............439...*.....@...997.
|
||||||
|
........+130..................296.....308................@.....*...902..................................77....833.....%.....932.102.48......
|
||||||
|
................407%.%685......*..........927.=222........426.450...*........12.....82...../..570................*...798..........*.........
|
||||||
|
..825.....................923.429......#..*........#630...........409..314*..........*...569.....*....273..648...961.............279........
|
||||||
|
...=..827=..........293.....#.........875.401.............457.433..........690.....600............929......*..............*245.......93..94.
|
||||||
|
.............557.......*.......................52........@..........284.......................450......................986..........*.......
|
||||||
|
........................463.....583..708...........................+........101....834.445.......*.....336...................694...333......
|
||||||
|
....556....923................./......*..433.....182....181.........................*..*......492..598.*............260.....*...............
|
||||||
|
......*.....*....430...............960....*...+..&.......*..=96.....628.282...@355.833.883...........*..468...420...........288.............
|
||||||
|
...140...682......*..945...150............7.654....+83.941...........*......@................#....7.780.......*......922.........334........
|
||||||
|
.............365..49.......$...590............................608.........503......./......36.....*.......74...340.....*....................
|
||||||
|
..........@..*.......870.-.....*...284*556................288..*...@.............$.526............587....*...........387....................
|
||||||
|
.......188....626.$......372.733.............................*..97.466.....776.541......................413.950..........696.162............
|
||||||
|
...../.............755...........62......99...............224...............-.........333......................*..337.....*....#......%233..
|
||||||
|
442..7.......=...................#...........875*705.548..............963...............*......180.....581...350.....*....513...............
|
||||||
|
..............377....................................*.........#......*.........291......981..*...........*..........74..............&......
|
||||||
|
......895.........644...................613.......540...........756.............%............390.263....754........#.....713-.....450.......
|
||||||
|
...85*....$.............297......%.........*..........156............974./870....................*...........12....426.................483..
|
||||||
|
.......774............#..*.....872.............361......-...196..................849...........419............*.........221....667..........
|
||||||
|
...................961....604...........644....*...............*........../927......*......503................124........-......../...238...
|
||||||
|
....370.............................531*......174.693.........349..................495.......*....925.......................................
|
||||||
|
.....+...104....582...602*604...123..................*..896..........71@.....................767...............573@....656......%...........
|
||||||
|
140........*...@...............*.........-........592..*....................806..692.....511...............755..........*......917...636....
|
||||||
|
..........905.......462.....+...655......860..150.....800..903.......8*920..........*867.@...................=..........269.................
|
||||||
|
....582..................217........842.........*...........*..................912...............551...615.......-..........343*129....+....
|
||||||
|
....*........................./.....................752..347...275...127.@........*.....804.....#....+.#.........252...845............671...
|
||||||
|
..862..........38..........293....429...@............*.......*...#.....*..245....330.......*18.....182.....$538..........&..725.............
|
||||||
|
.................=.............46*....696.............581.664.........608...........................................%.......=...............
|
||||||
|
...*724.....977.............................../................565...............#15...............................782.........359....$.....
|
||||||
|
529.....373.*............198.983.....980..559..592...100.579......*889..145..839..............790.....496....193......................545...
|
||||||
|
.........*..560..769.......*.....775...*.............*....@..104................*...%..820.....*......*.....*.............245...160.........
|
||||||
|
.....338............@......28.......*...725.687....761......%...............%..423.773..*.....315....765..69.................*...*..........
|
||||||
|
.851*.....619...333..............907..........*.........512....536..........98.........434....................48.613.973..941...554..751....
|
||||||
|
....................*564....122.............608........$.........-.........................48.........435.......*.......*............&......
|
||||||
|
...........165*967...........-..799.186.938......@.615...317........................630...........551*................345...................
|
||||||
|
....968............281.546.......*.....*.......225.......*...162....372.........&85...+...177@...........719..55................360*........
|
||||||
|
.......*.......%.......#.......226....................341.....*......*.............................341.........*...........989&.....321..618
|
||||||
|
....884..554.163...%...............*423.233.................653...557....$....910...................*...........109....772..................
|
||||||
|
..................854......167..560......@.....311...958..............492.....*.....*877.........@...911..797%...........*..................
|
||||||
|
....852.815.....9.................................*....#...../..941.........960..494........56.375.....................50..+......=...106...
|
||||||
|
....*......*461.&.......739....$..........=.$562.276.......408.....=.....................=...%.................769.........893..463..*......
|
||||||
|
.133..................-........797.....313...........................................20.214.....357..776.471......*687.@.............527....
|
||||||
|
.......14........899...845..........*..........+.....46........634........914.....84../...............*.....*780.......878..%36.435.........
|
||||||
|
542.....*...........$........*833...257..329-.147...........+........150..*......*.......907...........429.........................*....#...
|
||||||
|
.....517..799.44@.........230..........................477.579...........836......839.....-.....................964.704.............194..310
|
||||||
|
298.......*....................307..800......346.65.....*..........414........@.......619........&442.............*..*......................
|
||||||
|
...........510...+.....837.237*.........../.....*.....270.....818$..+..........27....*.................163.....140....647....764.163........
|
||||||
|
................181...*..........536#....335......................................610..170...............*.-....................*.......&...
|
||||||
|
.......................832........................#........&........611.........................&55...428..472....586......111........768...
|
||||||
|
.......763.....................................461.........381......-............566..814.....*.....................*........./............8
|
||||||
|
.............+.......................318............695............................=..%....323.756............711..663............827.......
|
||||||
|
...........526....=....675...353&.....@..982#......*..................+.62...880+..................631.......$......................-.118...
|
||||||
|
..................655....*....................795..30...922*.......978...+.&.........539...........-....719.................599.............
|
||||||
|
......2...574%..................#.698...475.....*...........652./...........464.163$...*..338*966.........................../.....534..386..
|
||||||
|
......*.......................404..#............747...703........231...-...............................................................*....
|
||||||
|
.......906.................................&575.........&....457.......633...395..761...355.#780....3+......799+...............496...264....
|
||||||
|
311............967.682............%.838...........253..........@..551.......@....*.....*.............................487.........*..........
|
||||||
|
..........@.......*......925....376....&...419......=.............*..............20..952.111/....648.&........748................834..706...
|
||||||
|
..443....940.............*....................*..........................820.684...................@.755........*........106.283............
|
||||||
|
..............397.........803...84............627..........704.983..........*................522............................*....541........
|
||||||
|
.....32....$.....#...643*..............116........./905......*..../...........311......811$.*........*890..........924..670........=....882.
|
||||||
|
......*.....81.....*.....636.......317...*...................899.............*....*698............626....................-..+..@.......*....
|
||||||
|
.......877......256.714...................825.........458....................869..............................54............28.823..110.....
|
10
2023/tests/day3.txt
Normal file
10
2023/tests/day3.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
Loading…
Reference in New Issue
Block a user