1
+ from collections import defaultdict , deque , Counter
2
+ # d = deque()
3
+ # d.append(5)
4
+ # x = d.popleft()
5
+ import re
6
+ # m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
7
+ # # or re.search
8
+ # >>> m.group(0) # The entire match
9
+ # 'Isaac Newton'
10
+ # >>> m.group(1) # The first parenthesized subgroup.
11
+ # 'Isaac'
12
+ # >>> m.group(2) # The second parenthesized subgroup.
13
+ # 'Newton'
14
+ # >>> m.group(1, 2) # Multiple arguments give us a tuple.
15
+ # ('Isaac', 'Newton')
16
+ from heapq import heappush , heappop
17
+ # >>> heap = []
18
+ # >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
19
+ # >>> for item in data:
20
+ # ... heappush(heap, item)
21
+ # heap[0] is the smallest item
22
+ import string
23
+ # string.ascii_lowercase == 'abcde...'
24
+ # string.ascii_uppercase == 'ABCDE...'
25
+ from functools import lru_cache
26
+ # @lru_cache(maxsize=None)
27
+ import numpy as np
28
+
29
+ import sys
30
+
31
+ sys .setrecursionlimit (100000 )
32
+
33
+ def get_ints (s ):
34
+ return list (map (int , re .findall (r"-?\d+" , s ))) # copied from mcpower from mserrano on betaveros' recommendation
35
+ dirs = [(0 ,1 ), (1 ,0 ), (0 ,- 1 ), (- 1 ,0 )]
36
+ directions = 'RDLU'
37
+ octs = [(0 ,1 ),(1 ,1 ),(1 ,0 ),(1 ,- 1 ),(0 ,- 1 ),(- 1 ,- 1 ),(- 1 ,0 ),(- 1 ,1 )]
38
+ def is_grid_valid (n ,m , r ,c ,):
39
+ return (0 <= r < n ) and (0 <= c < m )
40
+ def sign_of (x ):
41
+ if x == 0 :
42
+ return 0
43
+ return x / abs (x )
44
+ ####################################
45
+
46
+ """Wow, sad one today because I finished this pretty quickly but then spent minutes and minutes debugging
47
+ and stupidly didn't think to just read the problem statement a bit more carefully, where I would have realized
48
+ that you're not supposed to count (0,0) :(
49
+
50
+ First day using my new AOC CLI tool though, which was fun!
51
+ """
52
+
53
+ PART = 1
54
+ # PART = 2
55
+ if PART == 1 :
56
+ ans = 0
57
+ inps = []
58
+ while True :
59
+ try :
60
+ inps .append (input ())
61
+ except EOFError :
62
+ break
63
+
64
+ def dfs (r , c , dr , dc , i ):
65
+ pass
66
+ pq = [(0 , 0 , 0 , 0 , 0 , 0 , None )]
67
+ been = dict ()
68
+ while len (pq ) > 0 :
69
+ ex = heappop (pq )
70
+ x , r , c , dr , dc , i , prev = ex
71
+ # print(ex)
72
+ y = (r , c , dr , dc , i )
73
+ if y in been :
74
+ continue
75
+ been [y ] = prev
76
+ if r == len (inps )- 1 and c == len (inps [0 ])- 1 :
77
+ ans = x
78
+ break
79
+ for ddr , ddc in dirs :
80
+ nr , nc = r + ddr , c + ddc
81
+ if not is_grid_valid (len (inps ), len (inps [0 ]), nr , nc ):
82
+ continue
83
+ if ddr == - dr and ddc == - dc :
84
+ continue
85
+ if (ddr != dr or ddc != dc ) and i < 4 and not (dr == 0 and dc == 0 ):
86
+ continue
87
+ ii = i + 1 if ddr == dr and ddc == dc else 1
88
+ if ii > 10 :
89
+ continue
90
+ heappush (pq , (x + int (inps [nr ][nc ]), nr , nc , ddr , ddc , ii , y ))
91
+
92
+ while True :
93
+ y = been [y ]
94
+ r , c , dr , dc , i = y
95
+ print (r ,c )
96
+ if r == 0 and c == 0 :
97
+ break
98
+
99
+
100
+
101
+ print (ans )
102
+ else :
103
+ pass
0 commit comments