GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — AdventOfCode/languages/py/2023/17_ClumsyCrucible.py at main · wolflu05/AdventOfCode

Expand file tree

/

17_ClumsyCrucible.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
41 lines (31 loc) · 1.04 KB
/

17_ClumsyCrucible.py

Copy path

File metadata and controls

41 lines (31 loc) · 1.04 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import heapq
from lib import *
inp = get_input()
src = (0,0)
dst = (len(inp)-1, len(inp[0])-1)
INV_DIR = {"U":"D", "L":"R", "R":"L", "D":"U"}
def solve(min_walk, max_walk):
Q = [(0,*src,None,0)]
dist = {}
P=[]
while len(Q):
w,y,x,d,dc = heapq.heappop(Q)
if (y,x) == dst:
if dc >= min_walk:
P.append(w)
continue
for (yn,xn), dn in iterate_positions(y,x,get_positions(diagonal=False, include_pos_names=True), inp):
if d == INV_DIR[dn]: # ignore going backwards
continue
dcn = dc + 1 if d == dn else 1
if dcn > max_walk:
continue
if dc < min_walk and d != dn and d is not None:
continue
new_dist = w + int(inp[yn][xn])
if new_dist < dist.get((yn,xn,dn,dcn), float("inf")):
dist[(yn,xn,dn,dcn)] = new_dist
heapq.heappush(Q, (new_dist,yn,xn,dn,dcn))
print(min(P))
solve(1, 3) # part 1
solve(4, 10) # part 2
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.