add day3 part 1
This commit is contained in:
105
day3/day3.py
Normal file
105
day3/day3.py
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
import sys
|
||||||
|
sys.path.append('../lib')
|
||||||
|
|
||||||
|
import tools, re
|
||||||
|
|
||||||
|
class Scheme(object):
|
||||||
|
def __init__(self, inp):
|
||||||
|
self.grid = []
|
||||||
|
self.grid.append(['.' for _ in range(len(inp[0])+2)])
|
||||||
|
for line in inp:
|
||||||
|
self.grid.append(['.'] + list(line) + ['.'])
|
||||||
|
self.grid.append(['.' for _ in range(len(inp[0])+2)])
|
||||||
|
self.part_numbers =[]
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
out = ''
|
||||||
|
for row in self.grid:
|
||||||
|
out += ''.join(row) + '\n'
|
||||||
|
return out
|
||||||
|
|
||||||
|
def _isdigit(self, y, x) -> bool:
|
||||||
|
return self.grid[y][x].isdigit()
|
||||||
|
|
||||||
|
def _issymbol(self, y, x) -> bool:
|
||||||
|
return (not self._isdigit(y, x)) and (self.grid[y][x] != '.')
|
||||||
|
|
||||||
|
def _find_all_symbols(self) -> list:
|
||||||
|
all_symbols = []
|
||||||
|
for y in range(len(self.grid)):
|
||||||
|
for x in range(len(self.grid[y])):
|
||||||
|
if self._issymbol(y, x):
|
||||||
|
all_symbols.append((y, x))
|
||||||
|
return all_symbols
|
||||||
|
|
||||||
|
def _find_adjacent_numerals(self, y, x) -> list:
|
||||||
|
tmp = dict()
|
||||||
|
for yadd in [-1, 0, 1]:
|
||||||
|
for xadd in [-1, 0, 1]:
|
||||||
|
x_, y_ = y + yadd, x + xadd
|
||||||
|
if self._isdigit(x_, y_):
|
||||||
|
num, coords = self._extend_digit_to_numeral(x_, y_)
|
||||||
|
# just put it in a map if we found a digit
|
||||||
|
# of the same numeral
|
||||||
|
tmp[coords] = num
|
||||||
|
return list(tmp.values())
|
||||||
|
|
||||||
|
def _extend_digit_to_numeral(self, y, x) -> int:
|
||||||
|
found_digit = self.grid[y][x]
|
||||||
|
x_array = [x]
|
||||||
|
|
||||||
|
x_ = x-1
|
||||||
|
proceed = True
|
||||||
|
while proceed:
|
||||||
|
if self._isdigit(y, x_):
|
||||||
|
x_array.append(x_)
|
||||||
|
found_digit = self.grid[y][x_] + found_digit
|
||||||
|
x_ -= 1
|
||||||
|
continue
|
||||||
|
proceed = False
|
||||||
|
|
||||||
|
x_ = x+1
|
||||||
|
proceed = True
|
||||||
|
while proceed:
|
||||||
|
if self._isdigit(y, x_):
|
||||||
|
x_array.append(x_)
|
||||||
|
found_digit += self.grid[y][x_]
|
||||||
|
x_ += 1
|
||||||
|
continue
|
||||||
|
proceed = False
|
||||||
|
|
||||||
|
x_array.sort()
|
||||||
|
return int(found_digit), (y,) + tuple(x_array)
|
||||||
|
|
||||||
|
|
||||||
|
def _find_part_numbers(self):
|
||||||
|
symbols = self._find_all_symbols()
|
||||||
|
for symbol in symbols:
|
||||||
|
found_numbers = self._find_adjacent_numerals(symbol[0], symbol[1])
|
||||||
|
self.part_numbers.extend(found_numbers)
|
||||||
|
|
||||||
|
def find_sum_part_numbers(self):
|
||||||
|
self._find_part_numbers()
|
||||||
|
return sum(self.part_numbers)
|
||||||
|
|
||||||
|
|
||||||
|
def solve1(lines):
|
||||||
|
scheme = Scheme(lines)
|
||||||
|
return scheme.find_sum_part_numbers()
|
||||||
|
|
||||||
|
def solve2(lines):
|
||||||
|
total = 0
|
||||||
|
return total
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
r = tools.Reader('input.txt')
|
||||||
|
lines = r.read()
|
||||||
|
|
||||||
|
x = solve1(lines)
|
||||||
|
print(x)
|
||||||
|
|
||||||
|
y = solve2(lines)
|
||||||
|
print(y)
|
||||||
|
|
||||||
|
|
140
day3/input.txt
Normal file
140
day3/input.txt
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
................458...689.556..3............197......582........720.........................515..352..286.........670.741.....895.626.......
|
||||||
|
...910.743..........................13..........................*.............775...956........@.........*................971.-.............
|
||||||
|
....*......406.507.97..846..............968+.........253........730...574............#....308......*.....798..............*.......894.......
|
||||||
|
....555...............*......%...............980.+43..=..239..........*......495................638.111.........*490...124...*........576...
|
||||||
|
.............807......176.....820...=.........*.........@................144*...........324..............82..745............775.............
|
||||||
|
...509.../..../...85/......9.........574....744....586....../..796................763...............................759............#........
|
||||||
|
.....*...541...................930%................*.....831............494...44........799.....................870*.......834.215.675......
|
||||||
|
.....23..................391.........17.......500..329.................+.....*......................431.....................-...........19..
|
||||||
|
........................#.............*.704+./..................................152............=61....................668..............*....
|
||||||
|
........894....334.............766.817.........302.-367.663......670.....+..........628..............592..................652......130..885.
|
||||||
|
....228...........*...183..................169*...........*...............836..........*...230............45.....$....200....#.......&......
|
||||||
|
.......*........920..*....&........................865........60...=..........901-.....766.*...815..........*....410...*...............227..
|
||||||
|
.....#.623..........183...28.....460....861......./............*....151....................759...*......341..84......928....906..470........
|
||||||
|
..148........636................*........&....376...387=......187............741.308.............413.......................*.......*........
|
||||||
|
...............-.158........362..220.......+.........................$..553..*.....%...........*.........359=......373....706.518$..274.....
|
||||||
|
.....688..279........441......+........320..596.............../.#...989...*....%.....108....139.216...........-...*.........................
|
||||||
|
......*......*399.......*.506.........*....................282..344......610...462..%...............876.....287...230......$.......#633.....
|
||||||
|
...221...111.........235....*...%377.319.............503.......................................402...+...=...............224.............980
|
||||||
|
............*.............992...........................*..........945.891.................312*........747...........-......................
|
||||||
|
.........350.........................147....957.314.183.483...........*.....................................613#.....880.........$177.......
|
||||||
|
.....................146*409..........*.....................-................392......892.....981...246.437......790........................
|
||||||
|
......440..@860...............205*421..791.................652...456..$........................-....*.......565...%......140....511.........
|
||||||
|
.......*.............762.........................513.....#........*...596..656*.....959............292.476..............*........*..........
|
||||||
|
.....276.............&...167....108.819...............845.........950..........577.*..........533.........+...........323.......12..........
|
||||||
|
.........................*.......*...*.......348...........570.........998..........308..........*.765.......................76......558.897
|
||||||
|
............530....992....512...560...755......*..569.........#...................%.....461...896.....&...813......763..............*.......
|
||||||
|
424..........+....*.....*.....@...............922.....................445.99......88.......................=...803....*..14.440....139..349.
|
||||||
|
...*............155..417.911.383.........434.......+...345.763..279...#.....*6...................................&.814....*..#..............
|
||||||
|
...147.............................762...+........856..*....*....#..#............82..%.........=.......*................619.................
|
||||||
|
.......#668..........88.....................-..........448.710......25.427........&.740....*...44.608.809........%..................119.....
|
||||||
|
..................*........&....../........86.....724.....................*...924..........240....$.......935..945.....545.641.......@......
|
||||||
|
.....578.......565.749...342....338..................*786...305.....315..627.+........924.............+.....................................
|
||||||
|
........*135.................@......786.........911........*......*..............193.@...........862..337.......879.......992..681.598...207
|
||||||
|
..........................646..........*105.......*......682...683.53.849..750...*.......+......*...............*...787*..........*.........
|
||||||
|
............899......230.....................792...779....................@......918..955...731..233...........496..................109.....
|
||||||
|
...905.........*.......*....../...550*675...*..........652..........$581.......................*..........................$.....204...*.....
|
||||||
|
....*...146.171........162.944.............613...34.........563.321......648.................278.....69............519.....558.+.....573....
|
||||||
|
..134....*.........260............815*20..............286....*.....*....*.....100...261.....................=850....*.......................
|
||||||
|
......872.........+.....................................*..971..669....733...$.........*..........610............983..801=..........153.....
|
||||||
|
601.........484@.........109..........317..............424............................298.........*...976.....*.............................
|
||||||
|
...*....................*............*....316.201...........935*................................22...&.......137...336.....557........4.....
|
||||||
|
..745.........286.....989..........582.......*..................277...795..........335.................................520*............*....
|
||||||
|
.............=....&.............85..................=...19...................281+..@.............940.+970....318.................281..216...
|
||||||
|
................361................178../.........725.......*.........548..................*190...*..........$.....%.....&..........*.......
|
||||||
|
..57*......131*.........436*375.....#...168..............305.435........*...........674..........245....$.......$...404.270......812..707...
|
||||||
|
.....440.......318.525.........................*.........................540....799...*................344...432.......................*....
|
||||||
|
............$......*....526...730..142......927.785............................&.....509.......45................849*.......276......993....
|
||||||
|
..362*541...187....393........../...........................365...........442.................*....105....*..........256....................
|
||||||
|
........................568.......&.98.........+.=427./718..*......500#......#............692.547.&....753........=.........................
|
||||||
|
.........652@...........*.......836.&........333...........144.................*...33....#..................593.540.....968..822.....450....
|
||||||
|
..............518....442............................521.............884.303.85.483................594.......*..........@......*.......*.....
|
||||||
|
.......278......*........674..............844...435*.................*..-...&.............837......*.....333..#849..........607.......177...
|
||||||
|
.........&.......302........*........*519....*...............270....174....................*......579................66#..........425.......
|
||||||
|
..............*......*......205...833.........672...............*........269.440...771.....446.................=.*...............*..........
|
||||||
|
.134.527.....14...910.845.....................................113..-....*......*..*..........................579.171.........287..828.......
|
||||||
|
....*.....................726.942..83..........264.752..............188.765..359...182................380..............-.....*...........734
|
||||||
|
..........................*........#.............$.........322..............................*...........$....@830.....989.246...&...........
|
||||||
|
.....*260..452.325.................................................246...................466.915..............................-..417....&558
|
||||||
|
..961........%...........#....203*690..659..@.....424*..............*.............*.................797.363...$813...........340............
|
||||||
|
........................260................608........565.......@....856.......576.159.....691..23.....*................*562.........373....
|
||||||
|
......235.962..311-................*....#......218...........129............@..............@......*354................33................*...
|
||||||
|
649..*....&..........140......910..613.544..........420.............903*....825...............................139.819............595........
|
||||||
|
.....349......318.....$..746...*.............264......$..&...607........879................................+.....*...............&..........
|
||||||
|
...........$......259....*......609.............*.......619.....*.............#...68.45*...560*723..........171....12=....277.........563...
|
||||||
|
...39...428.......*.......551...................962...=..........620........776....*.......................................*..626.......*...
|
||||||
|
...&..............370.#.......*807..103...820.........842...562*.................646..48....%..412...........684*754....143..*..............
|
||||||
|
............866.......77...225.............&......152...........241.........628=........*.462....%.926........................990..439......
|
||||||
|
.352.............................&615........633+...*............................996..388..........*...516..............107%..........*743..
|
||||||
|
....*...............970.......%...................564........................99..../........*790..239.*......94....688......................
|
||||||
|
.211.....342+.........*....521...............788...............+........839............+.265...........486................637......+...@....
|
||||||
|
.....................82..............187...#....*731..230.......50...................480.......8.................................507....660.
|
||||||
|
...103..762*436.523*.......................250.........*...#...................................*...518.916...........................$......
|
||||||
|
...*................439....553....................479.466.553............699#..3.$...........695.......*....................=.......923.714.
|
||||||
|
..867.+971....45...........*..........669.....885................605.272......#...175...836...........15...................257..144.........
|
||||||
|
............................579.......#.............................*.....877..........*.......330........141=..............................
|
||||||
|
.................................881......98..400.517....................*....453......509........*..................................879....
|
||||||
|
....99.........300.882.....670....*............*...................712.119...*................473..290...630..........510......@.....*......
|
||||||
|
.../.....979...../....*959..+.....189........123...........402......&......394....329...........%.........*.......208*.......583...785...113
|
||||||
|
...................&............*........402......369..521........................*........314.............7.............564................
|
||||||
|
...............555.430.......630.854........%..........+......676...........980+.........................................*...............849
|
||||||
|
...........531..=.......................*......139............-..........................5..969=....338.........464.......508.......843.....
|
||||||
|
.............*.......118.............999.915.....*.&.....................755.........501*.............-....2.........766........3......*792.
|
||||||
|
...............@706..&...258.#501.............442...469..........332.327.*....@..............................*..........*...661..*..........
|
||||||
|
...955.....900......................#.....986..............705%.*.....-..884..290...177*531........632......802...936..39.....$...952.......
|
||||||
|
..........@...........483........553..544..#......920............340.............................../.................*.................396..
|
||||||
|
...149................*..............#................446....405.......555...........................98....313........834.#.....%...../.....
|
||||||
|
....*...409...266...29.......283..............=................*........*............%830....748....*......*..............457...592.........
|
||||||
|
...310.*......./....................*......138...197..........169...308.387..923............*........598....50...980........................
|
||||||
|
........178.............236......865.134...........*.436..772...................%..688.&.....850.66.........................796*897....679..
|
||||||
|
................170.......=.784..........$463...871..*.....*...338.........16........*.876.......*....................368*..........#...*...
|
||||||
|
...........270.....*682.......+.....................129..802....*...505......*822.834.........575.....309....@.577........6......926..632...
|
||||||
|
.....448.....@...*................105..960.480..................293.*....................598......$...*....686....*..759....13..............
|
||||||
|
.........535...507.........@.........@....*............390..........42.........919....-........928..584.............%........*..............
|
||||||
|
....12#.................661.....535.............397........................*.....*.....788...................936....................314.....
|
||||||
|
................................./.........417......528..492.......202..654.143.476.73.....796........282....*........317..............-....
|
||||||
|
.....511....................396.............*.............#.........................%.............&........861........*....919..954.........
|
||||||
|
......*............704*853........396.772...407.....499......$.....393.....-317.648..........262.72..............482.418.....-.......661*466
|
||||||
|
...387...300...................+....=../........688*........895.2...*................/........*.......25.......................268..........
|
||||||
|
............+................253.........16.......................665.......936..451.222..299.498.....*......*669.....652........*.218......
|
||||||
|
.................49....43..........567...*............%...226.........188.....*...$.........*......510....717.....$53..*......474....*......
|
||||||
|
...........#..........................#...906...206..479...#.............*.599.............598.........................904..&.....808.......
|
||||||
|
.........876...............295..................-.................998...34.........=348........499.81...........@..........681..............
|
||||||
|
....452........407.350.......*..@971................721............*....................129..-...#.............508....744...................
|
||||||
|
.......*17.....*.......739.136........................%.........635.........310.487....*.....746.....964.....+...........*.....639..........
|
||||||
|
...........314..471.............................768.................640.......=.../..387.............*....420.............975...-..230...533
|
||||||
|
..............*.......389.........&.372........%.......537.....799.....*.................678....661...531......460..................-.......
|
||||||
|
............97.........=....180.943...*...=.......................*....424..................#.....*...........*..............$..............
|
||||||
|
...803..............................529....264...............$...810..........810..................183......429...730*52.709..896..696*481..
|
||||||
|
......*82.570-............$.......%...................754...528..........+399....*893.....176*............................%.................
|
||||||
|
........................731.......679...........110*.....*........749.........................325.803*.....130..................+.820*541...
|
||||||
|
......739*.....707..........@577......4....210......21.396..........*..........822.930................689......250.749.774...539............
|
||||||
|
..........113.........................*......-..477.........112.....881......$....*.....$....$...........................*..................
|
||||||
|
.....+...............................809..........*........................459.......496...354....979...231....31.....-..541...168...708....
|
||||||
|
.....954..........642...267........*.....453......812...993............................../.......*........*..........267..........-....@....
|
||||||
|
.............9.........$.........307......*...364.........*..266.757/...800......805*197.453...758.617.....390................329...........
|
||||||
|
..............*954...................*...358..*..........859...*........*....848...................*.....-...............+....*...43.671....
|
||||||
|
.........107........596...*.......941........555.....378......824.348...207.....*....469/..352*..........474...........961.848...*......*...
|
||||||
|
............*......*....659..............+86........*.............%...........898..............307...............824.............735...584..
|
||||||
|
....540....734...463...........374..............204.........633*......................................627.......%.......%...%...............
|
||||||
|
....*................727..606............752.......+............522........@.............187&...$..89.+................311.384..............
|
||||||
|
.........368*901...................120........308......783..................361................901..........770.163.............348...278...
|
||||||
|
.........................10..874....*...................*................................826.......*440.=.....#.*...631...........*.........
|
||||||
|
..........145...........*.......*..665..567....&.........486.........%869......679........*.....369......195....890.*...4........100........
|
||||||
|
......664....@...314.....49....859.....%.......8.............971..........905.....&......595........................742...............340...
|
||||||
|
........*................................852........120.......*....80....*...................823.....%.........476.........893..............
|
||||||
|
........748.178.50*479........863....=.$..............*...........*....808...495...305......*.....684...47............./......*634..........
|
||||||
|
.....#........*.........&.....$.....83..674.571....575...........756...............*....*....546........-.....*124...199.758................
|
||||||
|
...346..937..70.........569..................@.........12....=...............-...742....694................405...........*.......$..........
|
||||||
|
...................................617...........958....*.778........540+.293.............................................502.834.....537...
|
||||||
|
...#.......47................908....*..............*.622.......................980.390#......785.104....................-...................
|
||||||
|
....261....*.......756..............555...%......893...............810..........-........804...*...$.942........739..773....................
|
||||||
|
.........390...765.......655......&......497.........885.....2.....$.................89../....636......*.862.......%................717.....
|
||||||
|
.......2........*...897....@....354..+...........959...*....@......................@........*.......576..@.............698..................
|
||||||
|
...............307..../.$.............100..........*...324....*.821....377..........939..134.625.......................*....................
|
||||||
|
.........................556.....................63........307...$.....*......=.....................462.....427.........822............681..
|
||||||
|
....................+........................................................864..........8.....550*............665...........#...956..*....
|
||||||
|
.780.......864.......435......811....760.................452.........................................&..886......*...........792..@.....245.
|
||||||
|
....*.........../............*..............661%.213........=.........310/.726.....739...791...387.123............924...............281.....
|
||||||
|
..139..........45......#...316....=................*..........678*..........*..@10..*......*..*.........*...61-......................*......
|
||||||
|
.....................815.........786................186...........640......388.......415....4..36.....894.........................303.......
|
32
day3/task.txt
Normal file
32
day3/task.txt
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
--- Day 3: Gear Ratios ---
|
||||||
|
You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside.
|
||||||
|
|
||||||
|
It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving.
|
||||||
|
|
||||||
|
"Aaah!"
|
||||||
|
|
||||||
|
You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help.
|
||||||
|
|
||||||
|
The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing.
|
||||||
|
|
||||||
|
The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.) do not count as a symbol.)
|
||||||
|
|
||||||
|
Here is an example engine schematic:
|
||||||
|
|
||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
||||||
|
In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361.
|
||||||
|
|
||||||
|
Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic?
|
||||||
|
|
||||||
|
Your puzzle answer was 531561.
|
||||||
|
|
||||||
|
The first half of this puzzle is complete! It provides one gold star: *
|
10
day3/test_input.txt
Normal file
10
day3/test_input.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
467..114..
|
||||||
|
...*......
|
||||||
|
..35..633.
|
||||||
|
......#...
|
||||||
|
617*......
|
||||||
|
.....+.58.
|
||||||
|
..592.....
|
||||||
|
......755.
|
||||||
|
...$.*....
|
||||||
|
.664.598..
|
1
day3/test_input2.txt
Normal file
1
day3/test_input2.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
|
Reference in New Issue
Block a user