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