/
day22.clj
224 lines (204 loc) · 34.1 KB
/
day22.clj
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
(ns adventofcode.y2022.day22
(:require [clojure.string :as str]
[com.akovantsev.blet.core :refer [blet blet!]]
[adventofcode.utils :as u]
[clojure.set :as set]))
(def sample " ...#\n .#..\n #...\n ....\n...#.......#\n........#...\n..#....#....\n..........#.\n ...#....\n .....#..\n .#......\n ......#.\n\n10R5L5R10L4R5L5")
(def input " ................................#............#.....#..........#.........................#........#.#\n .........................#.........##..#..#.#........#.#........#...........#....#...........#.#....\n .......#......##........##.....#.........................#.............#..................#.........\n .....................#..#.#.....##.......#.#...........#..#.....#......#..............#..#...#......\n ..........#...................#.....#.....................#...........................#.............\n .............................#.....................#........#...#.................#.......#.........\n ..............#...#..#.......................##..................##.#.......#........#..............\n .#.........#...........#.#...............#....##...#...##.............#...#...........#....#........\n ...#.................................................#...........#.............................#..#.\n ....................#......#......#..................#....................#..................##.....\n .....#...#.##..................#.............#.........#.##.............................#.#...##....\n ...#......#..........#.....................#....#........#...#...........##.#.....#......##.........\n .........#.....#.....#.....................#..................#.......#................#............\n .##.......#........#................................#.....#.#.......#...............................\n .........#.........#.......#...#...#......#.......#.#......#.......#......#.........................\n .#.#....#..#...............##..........##.#..#...............#..............#..#....................\n ......................##.............#......#......#...#..............#.............##...##.........\n #..........................................#.......................................................#\n ...........#........#...................#...........................................#...............\n ...#....#.......#..............#..........#............................#.......##..............#.##.\n .......#..................#..#......................#....................#...#...##..........#.##...\n ................................#.........................................##.#..........#........#..\n .......#..............#..#..........#..#....#....#.....................#...#..........#.......##....\n .#..#.......................#.#........................................#...........#...##.#.....#.##\n #.#............##...............##.............#........##...............#.....#...#................\n .........................#..#.......#......................................#.............#..........\n ..........#.#..##..#.........#.#.............#..#.....#......#..#......#.......................#....\n ......#...........#.#.......#........#.#...............#..........#...#..............#...........#..\n ........##.............#.#....#...........#..#............#...#....................#..........##...#\n ........#...#.......#...#..............#...................#...........##.....#...#...........#.....\n .....#....#...#................#..............#........#.....##......#..#.........#..#............#.\n .....#.#....#.....#.......#.#.......#.............#.##........................#..#........#.##.#...#\n .............#...........##........#..................#........#...............#....................\n .#.........#......#.........#...###.#.#......................#......#...#.............#...#.........\n ..................#..............#...#....................#....................#....................\n ...............#....................#...#..............#............#..#.#......#..#..#.............\n ....#.....#.........##..#..........##................##....#..............#.........#.#...#.....#.#.\n ...............#....#........#.#.......................#...#..........#...................#.........\n ..........#.........##..........#............#..................#.....................#....#..#...##\n ........#.............#.....................#..#............#......#.#..............#........#..#...\n ............#............#.........#...............##..##....#...#...........##....##...............\n .....#..#..........................#..#.....#.............#....#......#.....................#.#.....\n .................................#....##.......................#.......#...........#.#....#.........\n ...#...#...........#..#...............................#............#.....#....................#...#.\n .......##...#...#...#........#.#......#.....................................#.......#..##...........\n ..............#.#....###................#...........................#.................#.............\n ...#.......#..#....#.......#.......##.....#.......#.............#...............#..#................\n ...................#......#.......#..#.#..#....................#...............##...........#.......\n .#.#.................#.#..#.#........................#.........................#.............#.#....\n #...#.........................#........#..............................#...#......................##.\n ..................................#..#.##.........\n .#.......................##..#...#...#.......#....\n ........................##................#......#\n ......#.......................................#...\n ...#..........##.....#....#.#..#.........#.......#\n ........##...#....#..........#...#........#....#..\n ....#..#....#.#....................#..............\n .#.....#....#......................##......#......\n ...##.....#........#.....#.#............#.........\n ..................#....#........................#.\n ..........#.....#......##.......#..#.#.....#......\n ......................##...#......#......#....#...\n ##..............#........#......#..............#..\n ...............#...#.....................#..#..#..\n .................#.........#...##.....#..#.....#..\n #.#....#..#...#...#...##.....#....................\n ...#..#...#.................................#...#.\n .#...#............................#.....#.........\n ...................#....#..#...............#..#...\n .....#.............#................#.#..#...#....\n .............#..#..#.....#..#.#.#.................\n ..........#...#..........#..........#........##...\n ......#...................................#..#....\n #.............#.....#........#.#.....#..#...#.....\n ............##................#..........#........\n ..................##............................##\n .#.......#.#..........#.............#.............\n ..........#.............#....#..#.........#....#..\n .................#......#...#..........#.##.......\n .................#....................#......#.#..\n #.....#...........#.#..............#......#.......\n .......#....................#..##..........#..##..\n ..#....#.....#.........#..........................\n .#.#........#....##....#..........##..#...........\n ##...............#.............#..................\n .#.....#..#........#........#..#.......##.........\n ....................#....#............#...........\n #.##......#....#...#...#..#.#.......#.............\n ........#.....#.##...#...#....#.............#.#.#.\n ..........................###...............#.....\n .....#...........##.............#.#.##.....#.....#\n ....#.#................##..........#.....#..#.....\n ..............................................##..\n ......#.....#...............#........#............\n ....#...##.........#..#.....#........#...#........\n #....................#.#...#.................#....\n ..#...................................#.........#.\n ........#..........#...............#....#........#\n .......#.........#....#....#...#..................\n .#...................#......#..........#..........\n............#.#.......#...........#...##...............#...#.#...#.....#........#..#....#...........\n....................##...........#...........#..........#..........#....#....#............#.#.......\n#..................#........#.......#................#.....#.#......##..#.............#.........#...\n.##.................................................#........#...#.......................#........#.\n.#...................#........#..#..#...#............#.........................................#.#..\n....#.......#...#....................#......#....#.#.#........#.##.#.#..#.......#........#........#.\n......................#................##...#.#.......#..#..#.##..#......#...#...#..................\n...#...............#..#.......#.......#..#........................#..#........#..#...#..............\n...........#.....................##...................#..#.....#..............#...............#.....\n..........#......#........#....#......#.................#...........................#.............#.\n#..###............#.......#....#.#.#.............................#...#...#.##..............#....#...\n.............#.#.....#...............................#...................#........#.................\n..................#.................................#..#...................#...#....................\n.....#..#.........#......##...........#.......#......................#..............##..#...#.......\n.....#....#.........#..........#....#......##...#.#............................#...............#....\n.....#...#.#...#..#.#..............#.......##..........#...........##............#.......#..#.....#.\n#.....#............##............................#........................##.#.....................#\n.............#...#..#........##...............................#...........................##........\n.#.......#....#.........#.............#...#.#...................................#.....#...#......#..\n................#...........#..#......#...............#......#..#............#....#...............#.\n..#........................#...............................#..........................#.#....#......\n.........#.............#......#........#...#...#.....#.......#....#..##..#..#..................#....\n...........#....#...#.......#............................#.....##......#....#.......#.............#.\n.........#......................#.....#..........#..............#.......................#......#....\n..............#...........##...........#...........................................#................\n...............................#....#...#......................##...........#..............#...#..#.\n......#........#........#.................#.#...#........#........#..#....................#.........\n...........##..............................#...........##.............##.............#..............\n...#..###......................##...........#......#.......#..#..#........................#.#...#...\n................#.....#........#.....#....#.#...#.#......#.........................#................\n....#........##.....#..........................................#..........#...............#.......#.\n#.#.......#.#.......#...........#...........##..#..........................#.....#...........###.#..\n#.........#.........................#...................##......#........#..........................\n....#.......#...............................#.#..................................................#..\n...................#............#...#.....#.....#......#..........#..##.#........#..................\n.......#........#....#.....#.........................#.#.................#..#.........#..........#..\n.#..........#...#.......#.....#.......................#.......#....#..........#......#..............\n..#..#.#...........#.........#......................................#...#.......................#...\n..........................................................#.....#.....................#.............\n...............#......#.........#...........#.#.....................................................\n.#.#.......##...........#..........#..#......#.............#...............................#....#...\n.......#...#....#......#.......#...#....................#.....#..............##.....................\n.................#....#............#..................#.............#...............................\n#.......##...#.#....#..#...........#....................#.#...#..........##.#................#....##\n.....#.......#.........#............##..#...............#..........#......#................#...#...#\n.....#..........#.#.#....#............#...........#...........#......##....................#........\n..............#.......#...#................#.........#.#.#...........#.#...#........................\n........#.........#........##...#................#......#.....#.....#...........#...................\n.........................#...........................#.............#.......#....................#...\n.....#................................#.....#......#.........#........#.............#...#...........\n.........................................#.....#.#\n......#........................#.........##....#..\n.........#.............................#..........\n.....#..........#........................#........\n.............#................#...................\n...#...#..........#......#............#...........\n.###................................#.............\n..........#........................#..............\n..#...#.........##...........##....#..............\n............#.......#.............#.........#.....\n..#......#....#..#....#..#....#.....#......#......\n#.................................................\n.......#.#............#..............#...#........\n..................................................\n........................##.......#...........##...\n.#.............#.#...............#..#.............\n............................................#..#..\n.................##....#.....#..........#.........\n.............#.....##.....................#.......\n...#...........#............#..#.........#.#...#..\n.............#.......#.......#...#................\n...............##.................................\n............#.............................#.......\n.........#....#..#.....#..........#..........#....\n#.........#..............#.......#.#.........##...\n...#....................#.#..............#........\n...##........................##.#...#...#......#.#\n...........................#.......#..............\n..........#....#.................#.....#.......#..\n.........#.......##.......#.#....#.......##..###..\n#....................#......................#.....\n.............#...#.##................#.#..........\n.........#...#......#....#........................\n#....#..........#.....#............#...#..........\n..........#...#...#...............................\n.................#...............#..#.............\n#.................................#......#........\n.......#.#....#..................#................\n.##...........#...........#........#.#.....#....#.\n........#..#.................#..#.#.....#.......#.\n.......#..............#...#.........#.............\n.......#..#.............#...........#.#....#......\n......#..#...........................#..........#.\n.......#...#........#..#.....#....#.......##.#.#..\n......#.......#........#.........#..........#.....\n.....#..##....#...#.#..........##.........#..#...#\n#......##.....#...........#..#....#.#..........#..\n...#.........................#..#...........#.#...\n#....#...............................#............\n........#.........................#........#....#.\n\n23L34R4L9R5L23R42L32L17L25R37R35R40R44R3L37R13R12L28R39R23L34R28L39L45L23R35R22R22R16R4R12L11L16R50L36R32R20R15R9L42L42R33R44R14R5R11R4R13L29L11L3R33L8R16L48R8L32L33L7L25L36R7L40R5R28R22L18L12R12L28L11R14L6R37L7L46R26L35R29L10L32L18R9R47R4L27L42R19R38R43L29L33R43R27R18L28L11R3L39R23L47R41L40R47R43R39R25R9L27R34L24R18R39L42L37R29R29L39R19R23L49L9R50L19R33L36L32R27R50L47R40R20R10L18L40R8L5R43R22R6L48R49R19R3L39L11R28R19L19R43L47L40L29L12L38R36R15L13L44R10L26L34R29R23L11L31L13R33L36R46R18R46R41R30R24R25L1L1R4R48L30R4R44L26L19R20L47R46L5R28L12R40L41R45R12L36R28R19R48R46R45L47R7R33L12L29R17L41L4L19L3R47R41L27L7R10R16R37R48L17L31R39L29L46L48L46L33R14R19R10L14L1R39R2R50L13L39L48R33L18R25L37L22L30R17L36R12R32L12L30R30R31R2L46L25L39R47R31R23L1L40L8L8L29R22R6L30R44L47L3L24L10L37L2L31L32L41L16L24R3L1R3R12R1R38L14L29L3R42L38R16R26L33R41R30L48L5L23R22R15L38L4L26L50R30L45R37R17L49R16L19L20R44L46R17R3R16L6R43L33L11L16R27R15L18L25L3L42L33R43R17R41R50L40R44R2R30R17R20L41R37L21R15L48R1L38L8R25R23R26L33L2L38R9L40R13L8L32L19L43L16L39R23R6R12R35L42L25R40R20R31R32L25L28R27R10R19R17L34L18R25R31L27R24L10R7L10L17R24R32L39R31L27R48L12L33R24R6L2L50L43R20R47R33R33L40R21R48L40R12L47L19L1R47R29R46L21L4R28L28R34L37L14L48L17R2R44R8R40R47L44R22L13L6L43R5L25L19R37R36L14L36R22L33R31L3R44R35R45R17L50L7L47R13L10R23L34L32L43R17L18L11R5L49L46R20L25R41R8R3R26R13R35L41L42L27R5L31L16L45L18L23R32R2R23L4R47L3R10R30L41L46R37R17R36L20R40L48R27L48L50R40R44L25L23L40L3L38L45R15R28R33R31L6R36L27L44L21R10L9R10L26L34L48L16R12L7L33L6R25L17R40L6L26R17R33R38L35R18R41L26R19L17R45R40R16L42L18L48L33L6R45R39R18R48L17R2L48R27R31L25L46L19L36R9L9R8L26L41L14L47R30L32R18L47L10L34R40L27L34R27R36L50R20L22R33R23R24L22L19L23L6L28R49R47R31R23R10L3R6R25L5R28R18R50L40R47L1L20L39R4R39L34R21R2R39L33R31R47L21L46R15R41R17R32L2R1R26R9L1L12L31L7R45L32L39R46R29R13R5L13R41R21R29L6L12L20L50R43L32L22R5R7R37L43L13R42R11R21R14R49L46L23L15L32L10L17L17R45L10L22L7R49R4L32R36L45R16L32R4L39R8L24L38R43R22R36R26L18R29L13L41L43L9R31L5L41R14R10L3L22R27R3L35R4L35R15L33L21R31R5L35R35L26R35R44L42L29R46L20L44R38L32L16L5L16R15R3R48L10L36R16L8R34L29L21R22R29R22R10L23R22R45L38L18R17L38L1R8R48L18R10L25L42R22L44L33L41R44L13L34R10L22R1R11L8R15L18R15L9L21R11L38R4R45R29R41L45L18R12L36L9R16L38R33R15L31R15L33L42R28R49R1R17R5R46L44L13L33L40L19R42L18R25L28R44R4R46L32L2R34L25L46L6R49R10L20R20R29R12L13L21L48R46L37R21R9R21L23L36L36R1L12L8L30L3R35R20R4R32L22L50L46L25R32L45R50R3R50R36R39R21R32R39R36L48L49R49L29L13L15R36R47L30L49L33R24L28L29L44R1R16R27L39R8R33R31L37L20L26R32L1R38L16L21L44L23L46R49R42R31R43R14L45L8R37R7L39L24L49R41L2L31R15L49L29L24L21L40R16L32L50R22L10R10L47L4R17L1R35R17R29R12L27L19L42L44L50R39R1L19L30L38R44L21L44L33L32R18L34L6R16L6L46L40R36L20L46L42L24L44L34L8L24R25L13R26L37R38L10L49R22L45R12L26L6R25R45L42L6R7L34L26R7R37L39L43R6R46L41L35L34R42L12L39L35R17L25L37L13L20R29R22R13L21L31L3L23L6L15L43R21L2L47R11R1L39R23R16L41L25L38L39L48L49R36L27R27R24L20L26L37R15L14R15R42R4R9L38R22R48R15R5L34L7R26L7L13L2R35R21R9L11L9L40L43R42L23L45R8R36R5L19R30R9L45R23R46L50L47R48L19R18R25R8L27R39R29L32L38R13L50L11R45L47L50R49L25L47R10R37R19L18L22R43R47R22L28R43L49R46R38L47R28R43L21L19R4L10L6L31L41R35R31L10R22R45R43L49L48L18L34R50L46L50L43R24R24R48R47L6R49R44L37L7L37R5R9R24L8L15R1L33R9R5R40R45R48L37L40R17R41L38L24L23L31L21R23L11L39R3L42R27L13R18L49L28R40L42L31L30R43R43R14R26L12L35R25L4L14L28R48L47R3L30R8L44R10L45R26L5L27L40R35L5R37L22R26R19R41L15L19L21R12R37L26L47L35L37R48L45L16R20R39R41R8R25R29L14R4R32R1R4L17L23R34L2L19R26L27R2L11R35R24L36L17R32L41R31R12L50R49L25R25L44L15L13L17L50L29R29L26L25L28R5R24R16R35R17L44R48R38R12R13R7L49R2L22R44R34R19L29L38L1R37R36R6L34L4L16R25R39L22R47L50L30R28L10L35R48R36L27L8L13L27L38L30R3R31L29L23L13R38L41R45R43L12L15L45L4L9L1R23R34R14L33R26L35R41L3R5R28L39R27L44R17R46L2L18R11R34R14L26R49R37R45L12R21R31R18L5R37L19L36L39R4R33R33R39R13R9R29R43R20L18R17L39R37L37R9R23L34L40R26R40L10R34R21L13L34L7L18R15L15R20R20L31R33L44L38R30R20R45L9L41L22R11L17L8R5L49R24R28R13R34R4R33L16R30L31L47R7L14R45R41R17R14L18R11R41R41R1L43L4R31L16L18R1R47L43R2L37R29R9L4L13R6L47L2R47R34R38L44L10L49R41L37R16R41R50R9L41L49L16R33L25L1R45L32L33L1R17L22R22R9R18R20L37L10R4L44L26L50R30L35R6R36R24L19L25L24L48L43R6L44L12R45R36L28R46R7L40R19L37L47L42R1L15R28L48R37R25L36R50L25L6L34R47R10L17L49R21L17L24R7R2R28L11R50L30L37L20L22L9L45R10R22L36R10L41R49L11R1R20R27L45R15L32L30L43L50R40L32L19R27L45L13L26R27R46R39L46L20L28L5L6R20R4L10L44L47R5R27L34L37L39R44L30R5R5L49L38R35L9L19R15L35L33R6R17L38R40L40R38R14R8L5R10R38R2L8R47R23R27L50L39R12L5R6R39R48R26R17R48L3R6L34R42R38L14L34R20L4L4L37R6R1L9R11R13L14R30R22R30R39R3R22L29L48R16R11L14R17R43R46L16R31L5L1R33L6L35R31R18L35L41L11R43L10R41R41R28L20L34L9L6L46R30L9L28R7R9L23L1L14R50L24R27R22L13R45R1L42R48R18L27L6R13R36L43L35R24R46R4R28L31L28L50R19L37L39R30R41L32L27L18L44R3L27R34R5R25L50L32R12R47R13L48L10L28R40L15L17L17R17L19L18L22R1R47R40R35L21R12R49L35L2R27L8R44R35L48L4L37L24R37L36R28L48L37R23R25R32R21R37L45R18R19L48R5L46L3L42R33L22L4R3L14L44L25R46L45R12R40L6L24L40L21L40R3L8R34L34L40R34L19R22L48R15L30L34R19L12L14R20L3L9R30R33R33R14L44L50L2R5R7R7L33L25L9L21R29R29L10R37R17L35L17L16L37L33L27L50R33R35L7R6R24R50R7R25R41R43L12R45L8R46L1R9L36L25R39L24L3R7R16L39L44R45L46L4R14L20R21R32L10L48R33L45R24R34L4L32R11R44L34R19R21R26L14R36L43L17L16R34L15L16R9R19R45R29R43L13L7R14R1L47L29L9R29L50R47L47L18R45R44L49L24L23R11L20L35L10L33L3L27R30R16R50L24R14L37R34L40L15R13R40L46L30L17L45L41L4R20R12R11L32R27R48L13L40L6R38L13L37L43R45R24L45L46R16L24L9R47L43R28L43L7R7R44R23R47R47L9R38L45L47R10R32R12L45R4R2L7R27R34L11R24L32R8L50L29L44R1R40L46L35L21R26R12L20L27L38L48R42L14L9L45L26L16R21R49R23R7R32L18R45R12R27R50L44L43L39R26R11L48L13R40L9R41L29R32L5L16L23L26L12L39R1R22L11L31R29R40L31L18R39R30L35R40")
(defn p1 [ss]
(let [lines (->> ss str/split-lines)
steps (->> lines peek (re-seq #"\d+|[RL]") (map read-string))
tiles (->> lines pop)
width (->> tiles (map count) (reduce max))
height (->> lines count)
db (->>
(for [x (range 0 width)
y (range 0 height)]
[x y])
(reduce
(fn [m [x y]]
(case (get-in tiles [y x])
\. (-> m (update ::xys conj [x y]) (update ::floor conj [x y]))
\# (-> m (update ::xys conj [x y]) (update ::wall conj [x y]))
m))
{::floor #{}
::wall #{}
::xys #{}}))
{:keys [::floor ::wall ::xys]} db
col first
row second
minx (fn [xys] (apply min-key col xys))
miny (fn [xys] (apply min-key row xys))
maxx (fn [xys] (apply max-key col xys))
maxy (fn [xys] (apply max-key row xys))
START (->> floor sort first)
next-xy (memoize
(fn [dir [x y :as xy]]
(blet [=x #(-> % col (= x))
=y #(-> % row (= y))
xy+ (u/next-xy dir xy)
xy- (case dir ;;wrap around
:up (->> xys (filter =x) maxy)
:down (->> xys (filter =x) miny)
:left (->> xys (filter =y) maxx)
:right (->> xys (filter =y) minx))]
(cond
(wall xy+), xy
(floor xy+) xy+
(wall xy-), xy
:else,,,,,, xy-))))]
(loop [todo steps
xy START
dir :right]
(blet [op (first todo)
todo- (rest todo)
xy+ (->> xy (iterate #(next-xy dir %)) (drop op) first)
score (+
(-> xy row inc (* 1000))
(-> xy col inc (* 4))
(case dir :right 0 :down 1 :left 2 :up 3))]
(cond
(empty? todo) score
(= op 'L),,,, (recur todo- xy (u/turn-left dir))
(= op 'R),,,, (recur todo- xy (u/turn-right dir))
::move,,,,,,, (recur todo- xy+ dir))))))
(assert (= 6032 (p1 sample)))
(assert (= 57350 (p1 input)))
;;my input shape:
;aBC 1r-2r, 1b-3b, 1l-5r
;dEf 2r-
;GHi
;Jkl
; J J
; GBC BCH
; E E
; B
; G3C
; H
; E E
; BGH GHC
; J J
; G
; BJH
; C
(def W 50)
(def W- (dec W))
(def FXY '{,,,,,,, B [1 0] C [2 0]
,,,,,,, E [1 1],,,,,,,,
G [0 2] H [1 2],,,,,,,,
J [0 3],,,,,,,,,,,,,,,,})
(def FACE (set/map-invert FXY))
;;no time to factor out next-xy:
(defn p2 [ss]
(let [lines (->> ss str/split-lines)
steps (->> lines peek (re-seq #"\d+|[RL]") (map read-string))
tiles (->> lines pop)
width (->> tiles (map count) (reduce max))
height (->> lines count)
db (->>
(for [x (range 0 width)
y (range 0 height)]
[x y])
(reduce
(fn [m [x y]]
(case (get-in tiles [y x])
\. (-> m (update ::xys conj [x y]) (update ::floor conj [x y]))
\# (-> m (update ::xys conj [x y]) (update ::wall conj [x y]))
m))
{::floor #{}
::wall #{}
::xys #{}}))
{:keys [::floor ::wall ::xys]} db
col first
row second
COMP (fn [[x1 y1] [x2 y2]]
(compare [y1 x1] [y2 x2]))
START (reduce
(fn [[x1 y1] [x2 y2]]
(if (and (<= x1 x2) (<= y1 y2)) [x1 y1] [x2 y2]))
[##Inf ##Inf]
floor)
START (->> floor (sort COMP) first)
;_ (prn START)
next-dxy (memoize
(fn [[dir [x y :as xy]]]
(let [fx (quot x W)
fy (quot y W)
+x (rem x W)
+y (rem y W)
-x (- W 1 +x)
-y (- W 1 +y)
face (get FACE [fx fy])
d (-> dir name first str symbol)
; J J
; GBC BCH
; E E
; B
; GEC
; H
; E E
; BGH GHC
; J J
; G
; BJH
; C
;_ (prn face dir ['at-xy x y] ['face-xy fx fy])
[face+ dx dy dir+] (case [face d]
[B r] ['C -x +y :right]
[B d] ['E +x -y :down]
[B l] ['G +x -y :right]
[B u] ['J +y +x :right]
[C r] ['H +x -y :left]
[C d] ['E +y +x :left]
[C l] ['B -x +y :left]
[C u] ['J +x -y :up]
[E r] ['C +y +x :up]
[E d] ['H +x -y :down]
[E l] ['G +y +x :down]
[E u] ['B +x -y :up]
[G r] ['H -x +y :right]
[G d] ['J +x -y :down]
[G l] ['B +x -y :right]
[G u] ['E +y +x :right]
[H r] ['C +x -y :left]
[H d] ['J +y +x :left]
[H l] ['G -x +y :left]
[H u] ['E +x -y :up]
[J r] ['H +y +x :up]
[J d] ['C +x -y :down]
[J l] ['B +y +x :down]
[J u] ['G +x -y :up])
x+ (-> face+ FXY first (* W) (+ dx))
y+ (-> face+ FXY second (* W) (+ dy))
is-edge (zero?
(case dir
:left, (mod x W)
:right (mod (inc x) W)
:up,,, (mod y W)
:down, (mod (inc y) W)))
[nextdir
nextxy] (if is-edge
[dir+ [x+ y+]]
[dir (u/next-xy dir xy)])]
(if (wall nextxy)
[dir xy]
[nextdir nextxy]))))]
(loop [todo steps
xy START
dir :right]
(blet [op (first todo)
todo- (rest todo)
[dir+ xy+] (->> [dir xy] (iterate next-dxy) (drop op) first)
score (+
(-> xy row inc (* 1000))
(-> xy col inc (* 4))
(case dir :right 0 :down 1 :left 2 :up 3))]
;(prn dir xy op)
(cond
(empty? todo) score
(= op 'L),,,, (recur todo- xy (u/turn-left dir))
(= op 'R),,,, (recur todo- xy (u/turn-right dir))
::move,,,,,,, (recur todo- xy+ dir+))))))
> 61358
> 47521
(time (assert (= 104385 (p2 input))))