Advertisement
Guest User

aoc-day3-2.py

a guest
Dec 3rd, 2023
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 25.57 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. input = """.479........155..............944.....622..............31.........264.......................532..........................254.........528.....
  4. ..............-...............%.....+...................=....111*.................495.......+.......558..................../..........*.....
  5. ....................791*..62.....$.............847........&........-..........618.*...........818....&..642.........................789.....
  6. ....520.58......405......#....542.../587.............*....198.......846.........*..............*.......*....................647.............
  7. .........*........./.964..........................474.302.....................786...43..............505..436...................*.....#51....
  8. ......832....@..........*.951*....984*111..801................../.....................-.......@............%.198......322.186...262.........
  9. ..........490........690......346.........................702&.566.%....................192...190.87............*.....-....=..%.........344.
  10. ....*.........................................816*588..............152..535................*.......*...........425...........53.............
  11. ..36.290.831....374................579.536.....................408.......*..733....998....169...146.....%179..........658...............260.
  12. ...........+..../...........795/.....*.*.....................%........776......*..............................790.871./.............281*....
  13. ....................78.............716..400....319........167.................399..@.............................*.........$...599..........
  14. ............719.......*........640................%..376...............800........211......#478......326*93........889..684....*.....285....
  15. .....852.......-.......462..................374/....%..................*........$..............................603..*..........369..&.......
  16. [email protected]..................................*...........966.321...925............926...................*.947..&.............574..
  17. ..............%.....$.........$......*.......479....909.339..........*..............803........*17......284$...657.......587......*.........
  18. ...........772............&....345..93...465*................419......676...............-.@521.....-...........................399.662......
  19. .................17+..2..531.......................79........*...589......198*734....534.........614..................109...................
  20. .......301............=............................&..321..895..*..........................344.................694............717...511*....
  21. ..........%...707*370.....................473.428........*.....509......=889.....353%.........*................*.......299.......*..........
  22. ........................973.....................*.......877............................&[email protected]..
  23. ................314....*....504*352........602...468..............688.....10-....................#.......*...........306*.............*.....
  24. .....................987.......................5.............811..*...../.......515..217...........705....462..880.......374......16...42...
  25. ....#......402.$............804..295...406.....&..150........*....22..429...268............324........-.....................................
  26. .....270..*.....982...644.../.....+....*.........%.............................-..........#.............-...87.......................505....
  27. 938.................=...*.....=98.......370................19@[email protected]...*......760.......627.#...*......
  28. ..........593.....793....503.........34...............................406.....456...............+.........303........*...........142.432....
  29. ...........*..........&........707..*................563.....837.........+......*.....230..169.......................138....420.............
  30. ..572....689..........503.......#...................*...........*449..........39.........*./......77.......%....404......#..$............739
  31. ...............137...................#.....624.....883..../[email protected].*.....404...................
  32. ...287.......*..%............*[email protected]$................$.......531.72.......424............766..........
  33. ...*......476......316....722............780........613........../533...............96......553.91*......*.835*........*690...*.............
  34. .350...........%...............470.950...*.............*......................%.....*...728............359.....141..326.......658..832...330
  35. ..........772...127..................-...335......./...539........362......101.....959.............221..................512.........*.......
  36. ..........*..........798.......138..............207.....................................999...574.....*..........484....&............364....
  37. [email protected].........*......*.....202.971...............488.................@349........*......*..........404...=........&..448..$........
  38. .....246........211...426......206...*.....*557..........*[email protected]...*..........583.....*..280.....
  39. ...........724.....*......324*.....788..........685....788......@........*....532.................85.......*.139....75.........196..........
  40. [email protected]................./[email protected]...*.....................*810.214........*.....-........490......
  41. ......................................&....679.........776....447...457......25..............................467..173....241................
  42. ................43........898.412...742......%....*540.*.............*..............825..259...997.514.........*............................
  43. .......%775..52*...........*..*.........809....871.....384.....295..470............$.......-.....*....*..&.....114.....................=....
  44. ...147...........69=...........914..144*....=..............%.....*......$875.....+............=......278.441.........859.346.281........40..
  45. ......*....................-...............89...........578....519.............676..........473..361..................*....*..+.............
  46. ..78+..42......$...750..465.....218...833.......137...=.............538.783...........*962.........*............*...421.502....../..42......
  47. ..............457.....*.........+........*.....*....825.....26*....-.........238...205.....539.109.348........837..............842...*......
  48. ........#..............175..............925....399.............560.......88.*..............*.....*.................................636......
  49. ......693.......................447.................137............-679......479........619...283...............$458.544.-802.848@..........
  50. ...%.........................&...$....&.+39............*.........................%..618..................*141........+......................
  51. ....471...502....252....663..986...633..............530..117............598.....220...................542......568.......#219..532..15......
  52. .........*.....-........-...........................................840.$.............717..$...................../............#......*......
  53. .......351......993......................................573...865...*....$......848.......239.....134....826........409..338...$64..231....
  54. ...........................................=...809.925..*.........*[email protected].=........................................
  55. ..........+698..355.....-...594....%...#.55.......*......847..409...............@...............*.....*78..........................#363.497.
  56. ...................#..261.......591..695....-....................=.....678.......714.......364.804.156...................676...605..........
  57. 587.881.....356............192.............957...963.447.................#...63.............*................344.....373........*...........
  58. ..................524..568....&...691...........*.......*...169*218..10....................10.........399.46*............../488.491.16......
  59. [email protected]..$.....&..........*.265...............964............#...............................*........359...556............=.897..
  60. .............*................293.345.*...161*.....................671.............%414.726.347.....564....................420..............
  61. .............155.......483....................546........-.....794....*......968.........*...............591.......................$........
  62. .....806.../.......120.*.......813....................481...........593.........*....667.815.....682........*.%579.......#298....668.188....
  63. .......*.718.......*....469...*.........251...52*919.......846..................887....*......../........637...........................*....
  64. .....81.......132.51..........236........-................*.......$.167....338......963..258......844.........884.......*980........#...816.
  65. ..........475.........150..........316........389......590......291..-.....*....662......*...........................143..........284.......
  66. .......................&.......*..*....390.......+.559..................116.............926........779..................................233.
  67. 500...%......................821..594....*.........*................220...........830*...............*..........89...915.......230.363......
  68. ...*...623....337.......................40..........827..............*................828....$294....392....*....*.....%..............*.....
  69. .993............*....565........................638...............307.............95.......#..............535.105.........632..938.166..$939
  70. [email protected]...*.......4...283...971@.......*...................689..937...*[email protected]..@....*...........
  71. ....639....../.....886...........*..............668...88........472...+...*....742...493.........674....*......#........*.....*.78..-350....
  72. ...........290.........*.....%...110........447.......#....-.......*....838..........................167..911.487.....880..493..............
  73. 199...745......189=.389.....676......+........*..=........442....810........................................*.......................795.....
  74. ...................................337........76..728..=......61.....769.............................386.....627..604............$.....*623.
  75. ....=925................................=...............282.............*...597.....851..841...............2......*..........991..139.......
  76. .......................443....=..........829....................524...816......*......*.*.....752....=468..*...805.....388@[email protected]...
  77. ....14...........*........*...325.................676..............-.........192...859..712....+..........585...............................
  78. ....*.............892...513..........286.984...................301....859.....................................900.................788%......
  79. [email protected]/....155..*...........124.....*......844.......693.......*.............................
  80. ......469..................706.786.............67........*.............115*.....631.164..#............*..852..277.960.................11....
  81. [email protected]..........*..................354...471.......................................152.........................870...*...824
  82. ................*....*....832..822..............................=114.....881....-.........357*.....................................606..*...
  83. ..574............956.124........................943....278..................&.163...................=199................................761.
  84. ........329.623............308.....210..........*.........*....#...3........................792...........11...676..........................
  85. .795.68....*........................@.......280..........965.943...............814.....182.*.....454..202*..............971.47.704.....444..
  86. ....*.........407..............558.............#.........................913.....%....%.....801.................652......*....*.....-..*....
  87. ..........552*.................*.........487......321.......852.217.......+..........................=.943..628.......303..........350.288..
  88. [email protected]..................*.....$......*....*..............806*.......&....190...&.................806.235...........
  89. .......264.538......729.................997....688..435....510......82.285............316.639....................246.......%...*.......#....
  90. ....@.........+.964...............795..+.......................862...............................945.726-....../..../...........210.....700.
  91. ..452.............*...201.....891*.............49.............*........182......483.399.847.......*.............868....204.220.......-......
  92. .................466..*.............819...=.......184....$...114........+..........*........619....974....603................*.....854......
  93. .......................624.........*.....42...499*......743.........586.................950....-.............................927............
  94. ........$........379&.......855..252..#.....................120%......*..$...618.......*..................619..........636..................
  95. ....408.462.............528*...........869....145.15.606............581..297...*.....35..-...........218...*.......4*....%.......175........
  96. .....*........................$20................*..............................901......388...........$.............25...........*......157
  97. ..904........659............$.....762........808...%..........*18.............................#.878..........44..693............611..566*...
  98. ......977.......*.=.......597..-.*..........*.....538.209..............+.....$703......#....347....*652........*..........504...............
  99. .............668...462........59.2...370...587........*.............819..............152......................919.......+..*................
  100. .....817................688&.......................848..................639...347...........*671.......240........900..122.951..............
  101. ....*..........268/..........*821...........449..........817........86...*...............431......404..&...660....*..................691....
  102. ....611.............661....59........256.........$.......*...........+.548.........746.............+...........975...669.577...353....*.....
  103. 580.........307......&................*...........634.100....339......................*.................................*..........387......
  104. .....594.......*........&445...........675....+.............*......857......*........192.251..........504...................../.........534.
  105. .923..........954.............329.150......727....80.......474........$..385.............=...515*97..#......876.573...199.....921...........
  106. ...*.....236...............74........*..............*..............................919.........................*.......*..803....../460.....
  107. .94......*[email protected]............&.........%........349..752/.%.....735...........838.....*..............
  108. ...............425.*............985*........%......&........77....653....622...659.964..*.........147.-....................-.671............
  109. ......252...........898.....640.....841.........988..........*.............*.......*...805....................=...&.....410..........$743...
  110. .........*.....................*985.........407........$756..506.........-.332....934..................253..972...903........887.199........
  111. ......#...548........608*471.........142.....*...685...............729.172......-............................................&....#.....-558
  112. .....855..........%...........82.......*..729.......*.509.....850..@......./.910.......202........447..#........481......908........*.......
  113. .............$.641...758..846.*.....225...............*...374...........158........231.......775....*..837...............$...........549....
  114. .......587.615.......*.........844.......59......135..88.....*................437...*...........*.374................186....%829.167........
  115. ...691*............573..............293.*...........*.....940....105..38..........98........%.64.........432*737........*...........*.......
  116. [email protected]........=....50......324................*.......446...........51.....360....................175....227..426.....
  117. 566..186./......*277..18....*779..*.........+...........226..........184.....=......696..........+............344...550...........*.....65..
  118. .......*..................&........728....521.....277.........&.123........+........*....+......................*[email protected]....*....
  119. .....85........37....=....438.442.............522...*...757.248....*577.27..466..862...503.406*767........*736...164.......*...........233..
  120. .................#....640.....*........834...........23...*...............*............................404...........-......785.............
  121. .........*..................89..877*....#..572..........22............891..........295.354*864...875............=..&..706.........-.........
  122. .......307............59............510...*....187.*247............+........#..741*..............$.......$608.316.355......*.....916....858.
  123. ..................745......705*[email protected]...=..742........843.*44.......718.................309.............*...
  124. ....................*............................*888.......%[email protected]*.........797..$366..................95..
  125. ......890....*96.894......765......170/.......260.......149.......................759........................*.................940..........
  126. .......*....................*..#...................57*....-.........................&..858................312......881.........$.......627..
  127. ....149......392..633.....581.947.#...955...151*......44.................942..............+..........14........210*.....................*...
  128. ............/......*..889.........792...........201............#.....450*............................*...*................127.........921...
  129. ....567#........253...*......................................817...........85*106......239*.......920..16.969......%......*...............76
  130. .........*537..........583.512*...............*393.43...............513#...........906..............................562.681....*713.........
  131. ......260.....48..560..........376.........140............................151......*........823*.....921..........&.........224.............
  132. ...............*...%......=.................................716.........*[email protected]......
  133. .............61..........836..&29......357............106......*199...996.......*[email protected]*.........
  134. ....#...........#487.433...............#....81........*.......................488........382.....&.............#............................
  135. ..33............................908$........*........133....470&......-...........894...%......70..............626........253.*915..........
  136. ..................139*134.185...........49..142..../...............685..312...95..+..................985...#.......831.....@................
  137. ........*..................&.....................487..........822*........@....*.....................*....919......*...................*....
  138. 98......931......*[email protected]$...................522...915.583.72......592..........169.......353....365.678.
  139. ..............559......570...........................485..............#....582.......*.......*............*..................*....=.........
  140. .......+...38..........*...506.........811.....+188......766...623..363....*......*.914............#[email protected].........../...694..312..156..
  141. ........59...*.......405...*..........*......%..........&.........*[email protected]...........*....
  142. ....737.....608..........362...336....642....606..................262......................................209.........................617.."""
  143.  
  144. t_input = """................
  145. ................
  146. ................
  147. ......67*.......
  148. .........55.....
  149. """
  150.  
  151. t2_input = """................
  152. ................
  153. ................
  154. ......67*.......
  155. ......55.55.....
  156. """
  157.  
  158. t3_input = """................
  159. ................
  160. ..........67....
  161. ........*55.....
  162. ........4.......
  163. """
  164.  
  165.  
  166. import re
  167.  
  168. # debug output
  169. DEBUG = False
  170.  
  171. # real parts array
  172. parts = []
  173.  
  174. # first find all the numbers and all unique characters in input
  175. all_characters = set()
  176. all_numbers = {}
  177. line_number = 0
  178. all_lines = input.split('\n')
  179. for l in all_lines:
  180.     numbers = []
  181.     p = re.compile('\d+')
  182.     for m in p.finditer(l):
  183.         number = {'n': m.group(), 's': m.start(), 'e': m.start() + len(str(m.group()))}
  184.         #print('in line number %s found number %s' % (line_number, number))
  185.         numbers.append(number)
  186.     all_numbers[line_number] = numbers
  187.     for c in l:
  188.         all_characters.add(c)
  189.     line_number += 1
  190. #print("Found these numbers on these lines: %s" % all_numbers)
  191. print("Found all these unique characters in the input: %s" % all_characters)
  192.  
  193. # build unique symbols set
  194. symbols = all_characters
  195. for i in range(0, 10):
  196.     if str(i) in symbols:
  197.         symbols.remove(str(i))
  198. symbols.remove('.')
  199. print("Using symbols %s for validation in search" % symbols)
  200.  
  201. # now search for an adjacent 'symbol' for each potential part number
  202. all_parts = {}
  203. for line_number in all_numbers:
  204.     numbers = all_numbers[line_number]
  205.     for number in numbers:
  206.         number_is_part = False
  207.        
  208.         p_line_number = line_number - 1
  209.         n_line_number = line_number + 1
  210.  
  211.         # search previous line
  212.         if p_line_number > 0:
  213.             for i in range(number['s'] - 1, number['e'] + 1):
  214.                 if i >= 0:
  215.                     try:
  216.                         if all_lines[p_line_number][i] in symbols:
  217.                             if DEBUG:
  218.                                 print('found symbol %s at position %s on line %s to validate number %s in previous-line search' % (all_lines[p_line_number][i], i, p_line_number, number))
  219.                             if line_number in all_parts:
  220.                                 all_parts[line_number].append(number)
  221.                             else:
  222.                                 all_parts[line_number] = [number]
  223.                             parts.append(int(number['n']))
  224.                             number_is_part = True
  225.                             continue
  226.                     except Exception as e:
  227.                         pass
  228.         # search immediately before and after number
  229.         if not number_is_part:
  230.             if number['s'] > 0:
  231.                 if all_lines[line_number][number['s'] - 1] in symbols:
  232.                     if DEBUG:
  233.                         print('found symbol %s at position %s on line %s to validate number %s in previous-character search' % (all_lines[line_number][number['s'] - 1], number['s'] - 1, line_number, number))
  234.                     if line_number in all_parts:
  235.                         all_parts[line_number].append(number)
  236.                     else:
  237.                         all_parts[line_number] = [number]
  238.                     parts.append(int(number['n']))
  239.                     number_is_part = True
  240.                     continue
  241.             try:
  242.                 if all_lines[line_number][number['e']] in symbols:
  243.                     if DEBUG:
  244.                         print('found symbol %s at position %s on line %s to validate number %s in next-character search' % (all_lines[line_number][number['e']], number['e'], line_number, number))
  245.                     if line_number in all_parts:
  246.                         all_parts[line_number].append(number)
  247.                     else:
  248.                         all_parts[line_number] = [number]
  249.                     parts.append(int(number['n']))
  250.                     number_is_part = True
  251.                     continue
  252.             except Exception as e:
  253.                 pass
  254.         # search next line
  255.         if not number_is_part and n_line_number < len(all_lines):
  256.             for i in range(number['s'] - 1, number['e'] + 1):
  257.                 if i >= 0:
  258.                     try:
  259.                         if all_lines[n_line_number][i] in symbols:
  260.                             if DEBUG:
  261.                                 print('found symbol %s at position %s on line %s to validate number %s in next-line search' % (all_lines[n_line_number][i], i, n_line_number, number))
  262.                             if line_number in all_parts:
  263.                                 all_parts[line_number].append(number)
  264.                             else:
  265.                                 all_parts[line_number] = [number]
  266.                             parts.append(int(number['n']))
  267.                             number_is_part = True
  268.                             continue
  269.                     except Exception as e:
  270.                         pass
  271.  
  272. # now search the gears for 'two number' gears
  273. gear_ratios_sum = 0
  274. line_number = 0
  275. for l in all_lines:
  276.     for i, c in enumerate(l):
  277.         if c == '*':
  278.             adjacent_number_count = 0
  279.             adjacent_numbers = []
  280.             for li in range(line_number - 1, line_number + 2):
  281.                 for number in all_numbers[li]:
  282.                     if abs(number['s'] - i) < 2 or abs(number['e'] - i) < 1 or (number['s'] < i and i < number['e']):
  283.                         print('gear at i %s on line %s is adjacent to number %s on line %s' % (i, line_number, number, li))
  284.                         adjacent_number_count += 1
  285.                         adjacent_numbers.append(number)
  286.             if adjacent_number_count == 2:
  287.                 print("gear at i %s on line %s is adjacent to two numbers: %s" % (i, line_number, adjacent_numbers))
  288.                 gear_ratios_sum += (int(adjacent_numbers[0]['n']) * int(adjacent_numbers[1]['n']))
  289.                 print("running gear ratio sum is %s" % gear_ratios_sum)
  290.     line_number += 1
  291.  
  292. if DEBUG:
  293.     print("Found these validated parts on these lines: %s" % all_parts)
  294. print("Final sum of all validated parts is %s" % sum(parts))
  295.  
  296. print("Final sum of all gear ratios is %s" % gear_ratios_sum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement