diff --git a/day10/input b/day10/input new file mode 100644 index 0000000..bae22d0 --- /dev/null +++ b/day10/input @@ -0,0 +1,58 @@ +4310123125454565678965012358943210989010210012304563563434 +3211054034565654561676983467659823874323398943215654654323 +4302369879654243010385474563458765665489487654398723763210 +8498478988732132121292365698369034568676574503123017898432 +9567565676543001432101076547278125679676543211094323496501 +8543676507867652549212981036107089588789232103785210587611 +7652783412998543678903620125612176495610103212636707676210 +6701698321037933567216710534523125324321254103529898764389 +7810543232347874454325823678654034010110769054410940123476 +8921610110356783456934934569869894321023898764321456794565 +7432588741265492107821043234578785456954727101078329885103 +6541099654378322345458945105678976597805610892189810876212 +1232987653569011650367636789549565087012523483456734984301 +0343070144432100701214521567034456176543434576598125675698 +5452161230741019874303060158122367236521019665567018789789 +6761256301876548965692176549411098547430978703456909874321 +7890347892925437654782389432301387658945898112345870165230 +6776598743210126541001457621001234987836761021012561276121 +4985419654925676532156598517652125678787650130543490389030 +5692308767834983413967873408743006569596543249610385476543 +4781212546543072107856982189812217656678956658721276789102 +3210327635632143896541001098700398540565656767812312650211 +0105498126541234785432112387321478931234549898903403441300 +3236787012670105656789003456430560121001430098874454532411 +4145898001589012943297898987567654349432321127465367643589 +5016892122410343821156907016698701298547671436540298654677 +6787743433323456730045216326786543007678980569031109783288 +5698659854014387645632305438987012114523871078122567892199 +4321078765125499898701478367872123423014562147243498701088 +7892101609872310761010569156989896545195543234789654310567 +6543210018781423452329678043858787434387634989458743189430 +7894780129690565563478932132567096125210125676367012078921 +6785699434521874323567123021656101016702123401298323165854 +5210568765430985015676054120743219650893034898765414234763 +4323478100109871234982169437899658761282345601656500165102 +5610789287234760673433478546778747654321096782347679877201 +8767894396145654586524567612101238763011287891298981098389 +9454765345039654497013456703212109812320345680307672387474 +8303406230198763308902109874301238908765456765216787456565 +3212312121347012210167878965614367819876310894345891015054 +4301423001256756501456927874763456920101228701210432121123 +4567434654105847832325210129871237843289339876398987430434 +3438985783221938989414332012360345414576544325457676521543 +0129676590130121098506541023456786105697630410056789439632 +5432128976541232347687632010109897234788921569145697828701 +6910067989867801256198924101256798105696512478234556910890 +7823451678756988765011013212345011076787400321243235820789 +7893210501243289454526767345432176585012341210134124321658 +8761023432014107589439858956563089494654322301765010122545 +9654107898123478676510347967874589323783215489834587636430 +7543216967632569212141237876966678012894108566121098547821 +8710125430541043103054321768345567600143289677032783456932 +9651239821256456764569010109236768765234678788943894321013 +2343256710347347895678765218189899874387667697656788965456 +1954105409898216712989834367070786765496589874505497874387 +0867134106701007603494325454321605834101476723214367801296 +1798013265432198556583410765234514923452365214523254900145 +5654329870120123467898327892101623014565454301234103213234 diff --git a/day10/sample1.in b/day10/sample1.in new file mode 100644 index 0000000..a305b9d --- /dev/null +++ b/day10/sample1.in @@ -0,0 +1,4 @@ +0123 +1234 +8765 +9876 diff --git a/day10/sample2.in b/day10/sample2.in new file mode 100644 index 0000000..db56de0 --- /dev/null +++ b/day10/sample2.in @@ -0,0 +1,7 @@ +...0... +...1... +...2... +6543456 +7.....7 +8.....8 +9.....9 diff --git a/day10/sample3.in b/day10/sample3.in new file mode 100644 index 0000000..17655af --- /dev/null +++ b/day10/sample3.in @@ -0,0 +1,7 @@ +..90..9 +...1.98 +...2..7 +6543456 +765.987 +876.... +987.... diff --git a/day10/sample4.in b/day10/sample4.in new file mode 100644 index 0000000..185787f --- /dev/null +++ b/day10/sample4.in @@ -0,0 +1,7 @@ +10..9.. +2...8.. +3...7.. +4567654 +...8..3 +...9..2 +.....01 diff --git a/day10/sample5.in b/day10/sample5.in new file mode 100644 index 0000000..cada9b3 --- /dev/null +++ b/day10/sample5.in @@ -0,0 +1,8 @@ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732 diff --git a/day10/sol.py b/day10/sol.py new file mode 100644 index 0000000..9439fdc --- /dev/null +++ b/day10/sol.py @@ -0,0 +1,56 @@ +import itertools +from collections import Counter +def read(file): + return [line.strip() for line in file] + +def solve(file): + grid = read(file) + ny = len(grid) + nx = len(grid[0]) + points = lambda: itertools.product(range(nx), range(ny)) + + def get(x,y): + if 0 <= x < nx and 0 <= y < ny: + return grid[y][x] + return '.' + + def visit(x,y,h,n): + c = get(x,y) + if c == h: + assert (x,y) not in done + queue.append((x,y)) + count[x,y] += n + + trailheads = [(x,y) for x,y in points() if get(x,y) == '0'] + + t1 = 0 + t2 = 0 + for p in trailheads: + queue = [p] + done = set() + count = Counter() + count[p] = 1 + while queue: + x,y = queue.pop(0) + #print(x,y,queue) + if (x,y) in done: + continue + done.add((x,y)) + n = count[x,y] + c = get(x,y) + if c != '.' and c != '9': + h = str(int(c)+1) + visit(x+1,y,h,n) + visit(x-1,y,h,n) + visit(x,y+1,h,n) + visit(x,y-1,h,n) + for x,y in points(): + if get(x,y) == '9' and (x,y) in done: + t1 += 1 + t2 += count[x,y] + + print(t1) + print(t2) + +solve(open("sample5.in")) +solve(open("input"))