did day10 and day12 part 1
This commit is contained in:
1 parent
078c7dc82c
commit
adb6512003
7 files changed
+465
No files matched your search
@@ -0,0 +1,57 @@
|
||||
543213210349876210129890176034034565966563210587109092121
|
||||
656104101236762121456761789125120677877432101498218187030
|
||||
787215678945643012369852652196541986778943452387367256541
|
||||
898764101223454921058943543087632345657654363456456349654
|
||||
234459814312367838901012634678983954108932276545234438743
|
||||
105308765101298987632108768901017873217841189034105698894
|
||||
076218965290345676543289457632124561678750025121236787101
|
||||
987987874387654989123498398543401410589867014240901223218
|
||||
128876210260123478004567212898532323475678923457816314509
|
||||
019564320178874565213216500187645654129854503966765405678
|
||||
323469834569982104304307431234578765036763212875896876989
|
||||
767878706521676235425498120110569834545678923214387932376
|
||||
876967017430501543216326013223432728901298012103098541205
|
||||
985054328941432652107817154370121112854347543892107670314
|
||||
834123217632965469234908965987430004761256556701256789323
|
||||
729654306541874678945699874876548123450236789210345430110
|
||||
618783210930123012876789103765659874210101654345014521221
|
||||
105690129850019823498632212014567865321256789765623012434
|
||||
104941010761236710567541003423438978762349809854789923455
|
||||
203832123450145623456410212789829569451056012343210854396
|
||||
312745898567876514589321345657810438342347898901345765987
|
||||
405656776343998105679430103456910123239897687432216705476
|
||||
512346785434867234578345612765878762101798576521009812345
|
||||
694567698123452105507656709894349656123603401098967826565
|
||||
783878981030143245412349814763234341014512032367654983456
|
||||
012969976543201236093218923452123210012432145456543452187
|
||||
121001876124210187187108765421005391143465434505412341093
|
||||
321232565035303298296089012321016789834874328212303433282
|
||||
210543458749456334345676521056525470765966019301601214101
|
||||
109851239658765478034785435437434321014987458456732105010
|
||||
238760548789678969123699876348945610523456367234845256723
|
||||
321017656698767056782176501267876525676789210105996367894
|
||||
438998765589850145891089437678801234989654101986087458965
|
||||
567321996434743234106543228769960145676543243877124349896
|
||||
675490887525612103257890119454877658983450112568233210787
|
||||
587586716014101678965432001323768943232162103499545012345
|
||||
896675105003234567876721017012057890123078213487636776596
|
||||
745564234123098656989830398701146321265459812345629889487
|
||||
932213047894187765216541235610235430876343501676712012376
|
||||
871302120765276894307890344320145210901265430989800193401
|
||||
560456961294345653210787653410236389810178125476543287632
|
||||
410367854386543464678876544567107458103269076398389326543
|
||||
321298765677812104589965433218998567234387681267276410014
|
||||
123457654308903243218760129809889234985898790354105569123
|
||||
016534567210211038909678978321010125676787063203234678874
|
||||
105673898323302347874541065410981589094543154112356787965
|
||||
234982765432423456743232456723873672187612267053543298874
|
||||
122801894541510161250101365834712543870101348765632107985
|
||||
021289843690678870367010212945603434983289659054901001276
|
||||
130126732784569965478101204988914301874378778123892104345
|
||||
245035011098430156789678345677765210165134589104743077656
|
||||
356544324567821025898569101056879321051021678201654988745
|
||||
987676543056932110185430202346978432896120214312345679034
|
||||
678989432108945523679021312567566543787034305478943456123
|
||||
549034549087876654578110453498754694986545456967012987101
|
||||
432123678896521783063234569787103785675676567852173985432
|
||||
101210510123430192154345478776212656548989656743089876501
|
||||
@@ -0,0 +1,66 @@
|
||||
with open('input.text', 'r') as file:
|
||||
data: list[str] = file.readlines()
|
||||
|
||||
topo_map = [[int(c) if c != '.' else -1 for c in line.strip()] for line in data]
|
||||
|
||||
x_bound: int = len(topo_map[0]) - 1
|
||||
y_bound: int = len(topo_map) - 1
|
||||
|
||||
class Coord:
|
||||
x: int
|
||||
y: int
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f'({self.x}, {self.y})'
|
||||
|
||||
def __eq__(self, o) -> bool:
|
||||
return o.x == self.x and o.y == self.y
|
||||
|
||||
def in_bounds(c: Coord) -> bool:
|
||||
if c.y > y_bound or c.y < 0:
|
||||
return False
|
||||
|
||||
if c.x > x_bound or c.x < 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def score_trail(c: Coord, last: int | None = 0) -> list[Coord]:
|
||||
|
||||
if not in_bounds(c):
|
||||
return None
|
||||
|
||||
v: int = topo_map[c.y][c.x]
|
||||
if v == 9:
|
||||
return [c]
|
||||
|
||||
if v - last != 1 and last != 0:
|
||||
return None
|
||||
|
||||
up, down, left, right = Coord(c.x, c.y - 1), Coord(c.x, c.y + 1), Coord(c.x - 1, c.y), Coord(c.x + 1, c.y)
|
||||
|
||||
nines = []
|
||||
|
||||
for dir in [up, down, left, right]:
|
||||
if in_bounds(dir):
|
||||
if topo_map[dir.y][dir.x] - v == 1:
|
||||
for nine in score_trail(dir, v):
|
||||
if nine not in nines:
|
||||
nines.append(nine)
|
||||
|
||||
return nines
|
||||
|
||||
total = 0
|
||||
for y, line in enumerate(topo_map):
|
||||
for x, n in enumerate(line):
|
||||
if n == 0:
|
||||
total += len(score_trail(Coord(x, y)))
|
||||
|
||||
print(total)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
with open('input.text', 'r') as file:
|
||||
data: list[str] = file.readlines()
|
||||
|
||||
topo_map = [[int(c) if c != '.' else -1 for c in line.strip()] for line in data]
|
||||
|
||||
x_bound: int = len(topo_map[0]) - 1
|
||||
y_bound: int = len(topo_map) - 1
|
||||
|
||||
class Coord:
|
||||
x: int
|
||||
y: int
|
||||
|
||||
def __init__(self, x: int, y: int):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f'({self.x}, {self.y})'
|
||||
|
||||
def in_bounds(c: Coord) -> bool:
|
||||
if c.y > y_bound or c.y < 0:
|
||||
return False
|
||||
|
||||
if c.x > x_bound or c.x < 0:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def score_trail(c: Coord, last: int | None = 0) -> int:
|
||||
|
||||
if not in_bounds(c):
|
||||
return 0
|
||||
|
||||
v: int = topo_map[c.y][c.x]
|
||||
if v == 9:
|
||||
return 1
|
||||
|
||||
if v - last != 1 and last != 0:
|
||||
return 0
|
||||
|
||||
up, down, left, right = Coord(c.x, c.y - 1), Coord(c.x, c.y + 1), Coord(c.x - 1, c.y), Coord(c.x + 1, c.y)
|
||||
|
||||
dirs = []
|
||||
|
||||
for dir in [up, down, left, right]:
|
||||
if in_bounds(dir):
|
||||
if topo_map[dir.y][dir.x] - v == 1:
|
||||
dirs.append(score_trail(dir, v))
|
||||
|
||||
return sum(dirs)
|
||||
|
||||
total = 0
|
||||
for y, line in enumerate(topo_map):
|
||||
for x, n in enumerate(line):
|
||||
if n == 0:
|
||||
total += score_trail(Coord(x, y))
|
||||
|
||||
print(total)
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
89010123
|
||||
78121874
|
||||
87430965
|
||||
96549874
|
||||
45678903
|
||||
32019012
|
||||
01329801
|
||||
10456732
|
||||
Reference in new issue
Block a user