1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+ import re
5+
6+ def parse (data ):
7+ grid = {}
8+ board , instructions = data .split ('\n \n ' )
9+
10+ for y , line in enumerate (board .split ('\n ' )):
11+ for x , char in enumerate (line ):
12+ if char == ' ' : continue
13+ grid [(x , y )] = char
14+
15+ return grid , re .findall (r'\d+|L|R' , instructions )
16+
17+ split_data = parse
18+ completed = 1
19+ raw_data = None # Not To be touched
20+
21+ def part1 (data ):
22+ grid , instructions = data
23+ # Super weird puzzle
24+ movements = [(1 , 0 ), (0 , 1 ), (- 1 , 0 ), (0 , - 1 )] # Go right, down, left, up
25+ rotation = 0 # Starting out facing right
26+
27+ # Find the start
28+ x , y = min (x for x , y in grid .keys () if y == 0 ), 0
29+
30+ # print('We start at:', x, y, rotation)
31+
32+ for op in instructions :
33+ if op == 'R' :
34+ rotation = (rotation + 1 ) % 4
35+ continue
36+ elif op == 'L' :
37+ rotation = (rotation - 1 ) % 4
38+ continue
39+
40+ dx , dy = movements [rotation ]
41+ # print("rot:", dx, dy, op)
42+ for _ in range (int (op )):
43+ nx , ny = x + dx , y + dy
44+ future = grid .get ((nx , ny ))
45+ if not future :
46+ # We can do this because of how the map looks. Special to only these kinds of map
47+ if rotation == 0 :
48+ nx = min (gx for gx , gy in grid .keys () if gy == ny )
49+ elif rotation == 1 :
50+ ny = min (gy for gx , gy in grid .keys () if gx == nx )
51+ elif rotation == 2 :
52+ nx = max (gx for gx , gy in grid .keys () if gy == ny )
53+ elif rotation == 3 :
54+ ny = max (gy for gx , gy in grid .keys () if gx == nx )
55+ future = grid .get ((nx , ny ))
56+
57+ if future == '.' :
58+ x , y = nx , ny # We move
59+ elif future == '#' :
60+ break
61+
62+ # print("We end at", x, y, rotation)
63+
64+ return 1000 * (y + 1 ) + 4 * (x + 1 ) + (rotation % 4 )
65+
66+ def part2 (data ):
67+ # HELL NO
68+ ...
0 commit comments