day 11 & day 12
This commit is contained in:
60
2024/day10/day10a.py
Normal file
60
2024/day10/day10a.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import sys
|
||||
import re
|
||||
import heapq
|
||||
from collections import defaultdict, Counter, deque
|
||||
#import pyperclip as pc
|
||||
def pr(s):
|
||||
print(s)
|
||||
# pc.copy(s)
|
||||
sys.setrecursionlimit(10**6)
|
||||
infile = sys.argv[1] if len(sys.argv)>=2 else '/home/bandeira/git.bandeira/aoc/2024/day10/input.day10'
|
||||
p1 = 0
|
||||
p2 = 0
|
||||
D = open(infile).read().strip()
|
||||
G = D.split('\n')
|
||||
G = [[int(x) for x in row] for row in G]
|
||||
R = len(G)
|
||||
C = len(G[0])
|
||||
|
||||
def ways1(sr,sc):
|
||||
"""How many different 0s can I reach going down from here?"""
|
||||
Q = deque([(sr,sc)])
|
||||
ans = 0
|
||||
SEEN = set()
|
||||
while Q:
|
||||
r,c = Q.popleft()
|
||||
if (r,c) in SEEN:
|
||||
continue
|
||||
SEEN.add((r,c))
|
||||
if G[r][c]==0:
|
||||
ans += 1
|
||||
for dr,dc in [(-1,0),(0,1),(1,0),(0,-1)]:
|
||||
rr = r+dr
|
||||
cc = c+dc
|
||||
if 0<=rr<R and 0<=cc<C and G[rr][cc] == G[r][c]-1:
|
||||
Q.append((rr,cc))
|
||||
return ans
|
||||
|
||||
DP = {}
|
||||
def ways(r,c):
|
||||
if G[r][c]==0:
|
||||
return 1
|
||||
if (r,c) in DP:
|
||||
return DP[(r,c)]
|
||||
ans = 0
|
||||
for dr,dc in [(-1,0),(0,1),(1,0),(0,-1)]:
|
||||
rr = r+dr
|
||||
cc = c+dc
|
||||
if 0<=rr<R and 0<=cc<C and G[rr][cc] == G[r][c]-1:
|
||||
ans += ways(rr,cc)
|
||||
DP[(r,c)] = ans
|
||||
return ans
|
||||
|
||||
for r in range(R):
|
||||
for c in range(C):
|
||||
if G[r][c]==9:
|
||||
p1 += ways1(r,c)
|
||||
p2 += ways(r,c)
|
||||
|
||||
pr(p1)
|
||||
pr(p2)
|
||||
@@ -0,0 +1,45 @@
|
||||
123454078104569871014321021321012349878967874
|
||||
002965169245678562105489110456701256767456983
|
||||
411873254324329453456976522343812323454305432
|
||||
320984589010012306567887431098943210563216761
|
||||
458976673407873210698790346787654329874109850
|
||||
367887654356912348787891245497645698556780943
|
||||
656792343105401059678712312338562107649891234
|
||||
343001643234332769589600404329431236032321104
|
||||
102198756121049878465521565010120345121430213
|
||||
234567987012156912374437674320345034560345412
|
||||
126898776103457809983398983011276127672126703
|
||||
045678945434967618812287034567689018984035894
|
||||
034989876325898507600176127698548109123445665
|
||||
121070165016785216010165018345630678006510778
|
||||
015676234245234345323234509216721565217329889
|
||||
104389892104101059654965432101892674398456788
|
||||
010210743233298128760870123438984583212105998
|
||||
327805654654567637201256754347673294303614854
|
||||
476912348723498542112349861243100185454783763
|
||||
585210239010101443009659870652231256765692152
|
||||
694332108212012356788778778701645899801087001
|
||||
783445098302345653299894389614556732112396503
|
||||
612956789401298764108763210543678945013765412
|
||||
107810987567889655895654101012109876894894321
|
||||
016721076101910346781565092325012345765765010
|
||||
145432345234581232690478783234540332345654321
|
||||
298703876501698701541089654129651231056310145
|
||||
387212965012787650132108543098774340987234236
|
||||
456903454323456043236719612012589656891105687
|
||||
125876561210798101045898703343478797650112793
|
||||
034329870325887232784387643296556788943289832
|
||||
105610711456976545693211234187645019850176541
|
||||
612789804567803456789800105098732134567654320
|
||||
523898714326512101234764306789678325478956010
|
||||
630145625613456780345605219812569210329547854
|
||||
549234034702102395653216984503431678017632903
|
||||
678432129899801234764567893689120589098701012
|
||||
569562101234710109803891012778021432176512343
|
||||
450691032105676541012764569865434501089400154
|
||||
541782145694589132123453678345123671078321965
|
||||
432679238785410016787652101254010982361267873
|
||||
121018019856321125696545610763010973450398654
|
||||
038967025657689434545236789892129887650367743
|
||||
127652134768976521430129865430034796341459812
|
||||
234543089867987010321010178921015601232378103
|
||||
@@ -0,0 +1,8 @@
|
||||
89010123
|
||||
78121874
|
||||
87430965
|
||||
96549874
|
||||
45678903
|
||||
32019012
|
||||
01329801
|
||||
10456732
|
||||
Reference in New Issue
Block a user