day 10
This commit is contained in:
parent
642ee82a80
commit
c13bab03bd
66
2024/Day 10/main.py
Normal file
66
2024/Day 10/main.py
Normal file
@ -0,0 +1,66 @@
|
||||
import time
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
with open('puzzle.txt', 'r') as f:
|
||||
lines = f.read().splitlines()
|
||||
|
||||
trailhead_map = {}
|
||||
|
||||
total_trailhead_score = 0
|
||||
|
||||
class endpoint:
|
||||
def __init__(self, y, x):
|
||||
self.y = y
|
||||
self.x = x
|
||||
|
||||
def __hash__(self):
|
||||
return (self.y, self.x).__hash__()
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.y == other.y and self.x == other.x
|
||||
|
||||
def next_step(depth, trailhead_map, line, column, reachable_ends, trailhead_rating):
|
||||
if depth == 9:
|
||||
reachable_ends.add(endpoint(line, column))
|
||||
trailhead_rating += 1
|
||||
return reachable_ends, trailhead_rating
|
||||
|
||||
if line + 1 in trailhead_map and trailhead_map[line + 1][column]['value'] == depth + 1:
|
||||
reachable_ends, trailhead_rating = next_step(depth + 1, trailhead_map, line + 1, column, reachable_ends, trailhead_rating)
|
||||
|
||||
if column + 1 in trailhead_map[line] and trailhead_map[line][column + 1]['value'] == depth + 1:
|
||||
reachable_ends, trailhead_rating = next_step(depth + 1, trailhead_map, line, column + 1, reachable_ends, trailhead_rating)
|
||||
|
||||
if line - 1 in trailhead_map and trailhead_map[line - 1][column]['value'] == depth + 1:
|
||||
reachable_ends, trailhead_rating = next_step(depth + 1, trailhead_map, line - 1, column, reachable_ends, trailhead_rating)
|
||||
|
||||
if column - 1 in trailhead_map[line] and trailhead_map[line][column - 1]['value'] == depth + 1:
|
||||
reachable_ends, trailhead_rating = next_step(depth + 1, trailhead_map, line, column - 1, reachable_ends, trailhead_rating)
|
||||
|
||||
return reachable_ends, trailhead_rating
|
||||
|
||||
|
||||
for line_index, line in enumerate(lines):
|
||||
for column_index, column in enumerate(list(line)):
|
||||
if line_index not in trailhead_map:
|
||||
trailhead_map[line_index] = {}
|
||||
|
||||
if column_index not in trailhead_map[line_index]:
|
||||
trailhead_map[line_index][column_index] = {
|
||||
'value': int(line[column_index])
|
||||
}
|
||||
|
||||
trailhead_rating = 0
|
||||
|
||||
for line in trailhead_map:
|
||||
for column in trailhead_map[line]:
|
||||
if trailhead_map[line][column]['value'] == 0:
|
||||
reachable_ends = set()
|
||||
reachable_ends, trailhead_rating = next_step(0, trailhead_map, line, column, reachable_ends, trailhead_rating)
|
||||
total_trailhead_score += len(reachable_ends)
|
||||
|
||||
print(total_trailhead_score)
|
||||
print(trailhead_rating)
|
||||
|
||||
print(f'Total time: {time.time() - start_time}')
|
45
2024/Day 10/puzzle.txt
Normal file
45
2024/Day 10/puzzle.txt
Normal file
@ -0,0 +1,45 @@
|
||||
987123434330121232101001234730123456781067632
|
||||
876076576521010345692340349823212347892398701
|
||||
945087689432105676787659856714503210987445610
|
||||
332196576587654989801456787609654502376530923
|
||||
211543210298923215432321098128778901430121894
|
||||
300692340147210106523543210039569876589836765
|
||||
456781678236103267015693016543410231276745650
|
||||
576890549345234178106782187612320140345654321
|
||||
985098432100125089235493498109876056034765012
|
||||
834127102345456978340362569018765487123876678
|
||||
123236221976347869651251078729034398101985589
|
||||
014545340889298958707867897430120987012834432
|
||||
105965456770107843216950956541231276543124501
|
||||
896872378761016930345441019876501345678023670
|
||||
787901069654325321210332398545432330589012981
|
||||
107821543213034321089206787638901421432103210
|
||||
215430694102123475670115896129876548901210349
|
||||
126989780210014984308924925014578037654321458
|
||||
037878921001235675217833210123669123109452367
|
||||
549865438901045102346542106548754321278501476
|
||||
678954987432696201256430087239689870347699985
|
||||
230143006501787349961021298101236787656788014
|
||||
123272112981010458872787034010345691875107623
|
||||
054387623472129867763698125676210010961234510
|
||||
565694502561036789854567012980387121250129878
|
||||
676783411051045672343218763901296030343278569
|
||||
989872123432345891050109654812345145467303450
|
||||
012763094321056700891760345765432256958912341
|
||||
103450185789763211709851236876301967843211032
|
||||
814321276656854345612345654954101878701208983
|
||||
923434434565956745678036783063210989870345674
|
||||
874532345410345832989123192178981876781456564
|
||||
265101654323234901808765013265432185692387565
|
||||
103216765432101267814554323476501094501893474
|
||||
232109856321011876923601098789678923432102985
|
||||
343898707896540945498712367765672310567891078
|
||||
456789010987231234321203456894581455454986569
|
||||
556776125670102343100157654503490166303890432
|
||||
543895434894321765212348983212321876212761201
|
||||
432104898765010894301054581200110955211654300
|
||||
301256567656987105498765690341034567300563212
|
||||
434567430547896234787654785652123498456767843
|
||||
321798121032345375696543098743096567877854952
|
||||
210899021121036789781232143456787656928923761
|
||||
326765430110145678710123232109876543210010890
|
Loading…
x
Reference in New Issue
Block a user