aboutsummaryrefslogtreecommitdiffstats
path: root/day23/both.fth
blob: d4108b8ae1e95cd0643527324a1d0a9fbac5a411 (plain)
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
\ Advent of Code 2022: Day 23, both parts (comment selected)
\ Written for gForth. TODO improve

46 CONSTANT MEMPTY
35 CONSTANT MELF
13 CONSTANT MCR
10 CONSTANT MLF

0 CONSTANT NOMOVE
1 CONSTANT NORTH
2 CONSTANT SOUTH
3 CONSTANT WEST
4 CONSTANT EAST
CREATE DIRECTIONS NORTH , SOUTH , WEST , EAST ,

5000 CONSTANT MAXELVES
VARIABLE ELVES MAXELVES 2 * CELLS ALLOT
VARIABLE NEWELVES MAXELVES 2 * CELLS ALLOT
VARIABLE ELFCOUNT

: RESET ( -- )
   0 ELFCOUNT ! ;

: NEXTDIR ( dir -- dir )
  3 AND CELLS DIRECTIONS + @ ;

: ELF ( i -- c-addr )
   2 * CELLS ELVES + ;

: GETELF ( i -- y x )
   ELF DUP @ SWAP CELL+ @ ;

: GETNEWELF ( i -- y x )
   2 * CELLS NEWELVES + DUP @ SWAP CELL+ @ ;

: SETELF ( i y x -- )
   SWAP ROT ELF TUCK ! CELL+ ! ;

: SETNEWELF ( i y x -- )
   SWAP ROT 2 * CELLS NEWELVES + TUCK ! CELL+ ! ;

: ADDELF ( y x -- )
   ELFCOUNT @ -ROT SETELF
   1 ELFCOUNT +! ;

: SAMEELF? ( i y x -- b )
   ROT GETELF ROT = -ROT = AND ;

: SAMENEWELF? ( i y x -- b )
   ROT GETNEWELF ROT = -ROT = AND ;

: ELFAT? ( y x -- b )
   ELFCOUNT @ 0 DO
      2DUP I -ROT SAMEELF?
      IF 2DROP TRUE UNLOOP EXIT THEN
   LOOP 2DROP FALSE ;

: NEIGHBOR? ( y x -- b )
   1- SWAP 1- SWAP 0 -ROT
   3 0 DO
      3 0 DO
         2DUP ELFAT? IF
            ROT 1+ DUP 1 > IF
               DROP 2DROP TRUE UNLOOP UNLOOP EXIT ELSE
               -ROT THEN THEN
         1+
      LOOP
      3 - SWAP 1+ SWAP
   LOOP 2DROP 1 > ;

: NNEIGHBOR? ( y x -- b )
   1- SWAP 1- SWAP \ y x
   3 0 DO
      2DUP ELFAT? IF 2DROP TRUE UNLOOP EXIT THEN
      1+
   LOOP 2DROP FALSE ;

: SNEIGHBOR? ( y x -- b )
   1- SWAP 1+ SWAP
   3 0 DO
      2DUP ELFAT? IF 2DROP TRUE UNLOOP EXIT THEN
      1+
   LOOP 2DROP FALSE ;

: WNEIGHBOR? ( y x -- b )
   1- SWAP 1- SWAP
   3 0 DO
      2DUP ELFAT? IF 2DROP TRUE UNLOOP EXIT THEN
      SWAP 1+ SWAP
   LOOP 2DROP FALSE ;

: ENEIGHBOR? ( y x -- b )
   1+ SWAP 1- SWAP
   3 0 DO
      2DUP ELFAT? IF 2DROP TRUE UNLOOP EXIT THEN
      SWAP 1+ SWAP
   LOOP 2DROP FALSE ;

: CANMOVE? ( y x dir -- b )
   CASE
   NORTH OF NNEIGHBOR? 0= ENDOF
   SOUTH OF SNEIGHBOR? 0= ENDOF
   WEST OF WNEIGHBOR? 0= ENDOF
   EAST OF ENEIGHBOR? 0= ENDOF
   ENDCASE ;

: READELVES ( -- )
   0 0 \ Initial Y and X
   BEGIN
      KEY CASE
      MELF OF 2DUP ADDELF 1+ ENDOF
      MEMPTY OF 1+ ENDOF
      MCR OF DROP 1+ 0 ENDOF
      MLF OF DROP 1+ 0 ENDOF
      DROP 2DROP EXIT
      ENDCASE
   AGAIN ;

: PROPOSE ( i dir -- dir )
   SWAP GETELF
   2DUP NEIGHBOR? 0= IF 2DROP DROP NOMOVE EXIT THEN
   4 0 DO
      2DUP 4 PICK CANMOVE? IF 2DROP UNLOOP EXIT THEN
      ROT NEXTDIR -ROT
   LOOP 2DROP DROP NOMOVE ;

: PROPOSED ( i dir -- y x )
   SWAP GETELF ROT CASE
   NORTH OF SWAP 1- SWAP ENDOF
   SOUTH OF SWAP 1+ SWAP ENDOF
   WEST OF 1- ENDOF
   EAST OF 1+ ENDOF
   ENDCASE ;

: MAKEPROPOSALS ( dir -- )
   ELFCOUNT @ 0 DO
      I OVER PROPOSE
      I SWAP PROPOSED
      I -ROT SETNEWELF
   LOOP DROP ;

: UNDOELF ( i -- )
   DUP GETNEWELF SETELF ;

: COLLISION? ( i -- b )
   DUP GETNEWELF
   ELFCOUNT @ 0 DO
      2DUP I -ROT SAMENEWELF? 3 PICK I <> AND
      IF 2DROP DROP TRUE UNLOOP EXIT THEN
   LOOP 2DROP DROP FALSE ;

: COMMITELVES ( -- )
   ELFCOUNT @ 0 DO
      I DUP COLLISION? 0= IF DUP GETNEWELF SETELF ELSE DROP THEN
   LOOP ;

: STILL? ( -- )
   ELFCOUNT @ 0 DO
      I DUP GETNEWELF SAMEELF? 0= IF FALSE UNLOOP EXIT THEN
   LOOP TRUE ;

: DISPELVES ( -- )
   ELFCOUNT @ 0 DO
      I GETELF SWAP . . ." , "
   LOOP CR ;

: DISPNEWELVES ( -- )
   ELFCOUNT @ 0 DO
      I GETNEWELF SWAP . . ." , "
   LOOP CR ;

: SCATTER ( init-dir n -- n )
   0 -ROT
   0 DO
      DUP MAKEPROPOSALS
      SWAP 1+ SWAP
      STILL? IF DROP UNLOOP EXIT THEN
      COMMITELVES
      NEXTDIR
   LOOP DROP ;

: XMINMAX ( -- min max )
   10000 -10000
   ELFCOUNT @ 0 DO
      I GETELF NIP \ mi ma x
      2DUP < IF NIP DUP THEN \ mi ma x
      DUP 3 PICK < IF ROT DROP SWAP ELSE DROP THEN
   LOOP ;

: YMINMAX ( -- min max )
   10000 -10000
   ELFCOUNT @ 0 DO
      I GETELF DROP \ mi ma y
      2DUP < IF NIP DUP THEN
      DUP 3 PICK < IF ROT DROP SWAP ELSE DROP THEN
   LOOP ;

: PROGRESS ( -- n )
   XMINMAX SWAP - 1+
   YMINMAX SWAP - 1+ *
   ELFCOUNT @ - ;

RESET
READELVES
ELFCOUNT @ . ." elves" CR
\ NORTH 10 SCATTER PROGRESS . CR \ part 1
NORTH 0 SCATTER . ." rounds" CR \ part 2 (exec time: 9m31)
BYE