-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexagony.hs
540 lines (486 loc) · 21.5 KB
/
hexagony.hs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
module Main ( main ) where
import Control.Monad ( when )
import qualified Data.Array as A
import Data.Array ( (!), (//) )
import qualified Data.Char as C
import qualified Data.Map as Map
import qualified Data.Maybe as M
import System.Environment ( getArgs )
import System.IO ( hPutStr, isEOF, stderr, stdin, stdout )
import Text.Printf ( printf )
-- Utilities
-- Pads a string to a given length using a specific character
pad :: String -> Int -> Char -> String
pad s n c = s ++ replicate (n - n') c
where
n' = length s
-- Core
-- A Regular centered hexagon
data Hexagon = Hexagon { maxCommands :: Int, minCommands :: Int, size :: Int }
-- Directions inside a flat-topped hexagon
data Direction = East | West | SouthEast | SouthWest | NorthEast | NorthWest
instance Show Direction where
show direction = case direction of
East -> "E"
West -> "W"
SouthEast -> "SE"
SouthWest -> "SW"
NorthEast -> "NE"
NorthWest -> "NW"
-- We will be using axial coordinates https://www.redblobgames.com/grids/hexagons/#coordinates-axial
data HexCoordinate =
HexCoordinate { q :: Integer, r :: Integer, direction :: Direction }
-- The current state of the program
data State = State { memory :: Map.Map (Integer, Integer) Integer
, mp :: HexCoordinate
, ip :: HexCoordinate
, ipIdx :: Int
, ips :: A.Array Int HexCoordinate
, terminated :: Bool -- Is the execution over
, errorMessage :: String -- The reason for termination
}
data Program = Program { instructions :: Map.Map (Integer, Integer) Char
, state :: State
, size2 :: Integer
}
instance Show State where
show State{memory = memory, mp = mp, ip = ip} = ""
instance Show Hexagon where
show Hexagon{maxCommands = maxCommands, size = s} =
show s ++ '\n' : show maxCommands
chn :: Int -> Int
chn n = 3 * n * (n + 1) + 1
-- Centered Hexagonal Numbers: https://oeis.org/A003215
chns :: [(Int, Int)]
chns = [ (chn n, n + 1) | n <- [ 0 .. ] ]
-- Smallest regular centered hexagon that fits a program of given length
-- TODO handle len 0
getHexagon :: Int -> Hexagon
getHexagon programLen = Hexagon { maxCommands = maxCommands
, minCommands = (+) 1 . chn $ n - 1
, size = n
}
where
(maxCommands, n) = head . dropWhile ((< programLen) . fst) $ chns
-- Pads and cleans up a hexagony program with .
cleanProgram :: String -> String
cleanProgram p = pad program maxCommands '.'
where
program = filter (\c -> c /= ' ' && c /= '\n') p
Hexagon{maxCommands = maxCommands} = getHexagon . length $ program
-- Formats a hexagony program
formatProgram :: String -> String
formatProgram p = format' 1 n program
where
program = cleanProgram p
Hexagon{size = n} = getHexagon . length $ program
format' _ _ "" = ""
format' i n' p =
let i' = if n' == 2 * n - 1 then -1 else i
in
(reverse . foldl (\s c -> c : ' ' : s) (replicate (2 * n - n') ' ')
. take n' $ p) ++ '\n' : format' i' (n' + i') (drop n' p)
-- Create an empty hexagon with given dimensions
emptyHexagon :: Int -> String
emptyHexagon n = formatProgram $ replicate (chn (n - 1)) '.'
{-* ======================================================================
MEMORY OPERATIONS
====================================================================== *-}
--Gets the value of a memory edge
getMemEdge :: State -> HexCoordinate -> Integer
getMemEdge State{memory = memory} HexCoordinate{q = q, r = r} =
M.fromMaybe 0 $ Map.lookup (q, r) memory
-- Sets the value of a memory edge
setMemEdge :: State -> HexCoordinate -> Integer -> State
setMemEdge state@State{memory = memory} HexCoordinate{q = q, r = r} value =
state { memory = Map.insert (q, r) value memory }
memLeft :: HexCoordinate -> HexCoordinate
memLeft HexCoordinate{q = q, r = r, direction = East} =
HexCoordinate { q = q + 1, r = r + 0, direction = NorthEast }
memLeft HexCoordinate{q = q, r = r, direction = West} =
HexCoordinate { q = q - 1, r = r + 0, direction = SouthWest }
memLeft HexCoordinate{q = q, r = r, direction = SouthEast} =
HexCoordinate { q = q + 0, r = r + 1, direction = East }
memLeft HexCoordinate{q = q, r = r, direction = SouthWest} =
HexCoordinate { q = q - 1, r = r + 1, direction = SouthEast }
memLeft HexCoordinate{q = q, r = r, direction = NorthEast} =
HexCoordinate { q = q + 1, r = r - 1, direction = NorthWest }
memLeft HexCoordinate{q = q, r = r, direction = NorthWest} =
HexCoordinate { q = q + 0, r = r - 1, direction = West }
memRight :: HexCoordinate -> HexCoordinate
memRight HexCoordinate{q = q, r = r, direction = East} =
HexCoordinate { q = q + 0, r = r + 1, direction = SouthEast }
memRight HexCoordinate{q = q, r = r, direction = West} =
HexCoordinate { q = q + 0, r = r - 1, direction = NorthWest }
memRight HexCoordinate{q = q, r = r, direction = SouthEast} =
HexCoordinate { q = q - 1, r = r + 1, direction = SouthWest }
memRight HexCoordinate{q = q, r = r, direction = SouthWest} =
HexCoordinate { q = q - 1, r = r + 0, direction = West }
memRight HexCoordinate{q = q, r = r, direction = NorthEast} =
HexCoordinate { q = q + 1, r = r + 0, direction = East }
memRight HexCoordinate{q = q, r = r, direction = NorthWest} =
HexCoordinate { q = q + 1, r = r - 1, direction = NorthEast }
memReverse :: HexCoordinate -> HexCoordinate
memReverse coord@HexCoordinate{direction = East} = coord { direction = West }
memReverse coord@HexCoordinate{direction = West} = coord { direction = East }
memReverse coord@HexCoordinate{direction = NorthEast} =
coord { direction = SouthWest }
memReverse coord@HexCoordinate{direction = NorthWest} =
coord { direction = SouthEast }
memReverse coord@HexCoordinate{direction = SouthEast} =
coord { direction = NorthWest }
memReverse coord@HexCoordinate{direction = SouthWest} =
coord { direction = NorthEast }
{------------------------------------------------------------------------
MOVEMENT OPERATIONS
------------------------------------------------------------------------}
-- Move the current IP forward
moveIP :: Integer -> State -> State
moveIP size
state@State{ mp = mp
, ip = ip@HexCoordinate{q = q, r = r, direction = direction}
} =
let (q', r') = case direction of
East -> (q + 1, r)
SouthEast -> (q, r + 1)
SouthWest -> (q - 1, r + 1)
West -> (q - 1, r)
NorthWest -> (q, r - 1)
NorthEast -> (q + 1, r - 1)
in
let (q'', r'') = overflow q' r'
in
state { ip = ip { q = q'', r = r'' } }
where
overflow q' r'
| abs q' >= size && r' /= 0 && s' /= 0 = (-q, -s)
| abs q' >= size && r' == 0 && isMemEdge = (-q, -s)
| abs q' >= size && r' == 0 = (0, s)
--
| abs r' >= size && s' /= 0 && q' /= 0 = (-s, -r)
| abs r' >= size && s' == 0 && isMemEdge = (0, -r)
| abs r' >= size && s' == 0 = (r, 0)
--
| abs s' >= size && q' /= 0 && r' /= 0 = (-r, -q)
| abs s' >= size && q' == 0 && isMemEdge = (-r, -q)
| abs s' >= size && q' == 0 = (r, s)
--
| otherwise = (q', r')
where
s' = -q' - r'
s = -q - r
memEdge = getMemEdge state mp
isMemEdge = memEdge > 0
-- Reads an Int from STDIN, if no int is found return 0
getInt :: IO Integer
getInt = do
l <- getInt' True
return $ head l * foldl (\n d -> 10 * n + d) 0 (tail l)
where
getInt' isFst = do
done <- isEOF
if done
then return [ 1, 0 ]
else do
c <- getChar
helper isFst c
helper :: Bool -> Char -> IO [Integer]
helper isFst c
| isFst && c == '+' = (1 :) <$> getInt' False
| isFst && c == '-' = (-1 :) <$> getInt' False
| isFst && C.isDigit c = (1 :) . (fromIntegral (C.digitToInt c) :)
<$> getInt' False
| C.isDigit c = (fromIntegral (C.digitToInt c) :) <$> getInt' False
| not isFst = return []
| otherwise = getInt' True
{------------------------------------------------------------------------
CORE
------------------------------------------------------------------------}
-- Runs a binary operation
binop :: State -> (Integer -> Integer -> Integer) -> State
binop state@State{memory = memory, mp = mp} f = setMemEdge state mp $ f l r
where
l = getMemEdge state $ memLeft mp
r = getMemEdge state $ memRight mp
-- Executes the instruction on the current cell
execute :: State -> Integer -> Char -> IO State
execute state@State{ mp = mp
, ip = ip@HexCoordinate{direction = ipDirection}
, ipIdx = ipIdx
, ips = ips
}
size
c
|
{------------------------- Arithmetic Operations -------------------------}
C.isDigit c = moveIP' $ setCurMemEdge $ 10 * memEdge
+ fromIntegral (C.digitToInt c)
|
-- increments the current memory edge
c == ')' = moveIP' . setCurMemEdge $ memEdge + 1
|
-- decrements the current memory edge
c == '(' = moveIP' $ setCurMemEdge $ memEdge - 1
|
-- sets the current memory edge to the sum of the left and right neighbors.
c == '+' = moveIP' $ binop state (+)
|
--sets the current memory edge to the difference of the left and right neighbors
-- (left - right).
c == '-' = moveIP' $ binop state (-)
|
-- sets the current memory edge to the product of the left and right neighbors.
c == '*' = moveIP' $ binop state (*)
|
-- sets the current memory edge to the quotient of the left and right neighbors
-- (left / right, rounded towards negative infinity).
c == ':' = moveIP' $ binop state quot -- DIVISION BY 0
|
-- sets the current memory edge to the modulo of the left and right neighbors
-- (left % right, the sign of the result is the same as the sign of right).
c == '%' = moveIP' $ binop state mod -- DIVISION BY 0
|
-- multiplies the current memory edge by -1
c == '~' = moveIP' $ setCurMemEdge $ memEdge * (-1)
|
{------------------------- IO Operations -------------------------}
-- reads a single byte from STDIN and sets the current memory edge to its value, or -1 if EOF is reached.
c == ',' = do
done <- isEOF
value <- if done then return (-1) else C.ord <$> getChar
moveIP' $ setCurMemEdge (fromIntegral value)
|
-- reads and discards from STDIN until a digit, a - or a + is found.
-- Then reads as many characters as possible to form a valid (signed) decimal integer
-- and sets the current memory edge to its value. Returns 0 once EOF is reached.
c == '?' = moveIP size . setCurMemEdge <$> getInt
|
-- takes the current memory edge modulo 256 (positive) and writes the corresponding byte to STDOUT.
c == ';' = do
putChar $ C.chr . fromInteger $ (256 + (memEdge `mod` 256))
`mod` 256 -- TODO better positive modulus
moveIP' state
|
-- writes the decimal representation of the current memory edge to STDOUT.
c == '!' = do
putStr . show $ memEdge
moveIP' state
|
{------------------------- Control flow -------------------------}
-- `$` is a jump. When executed, the IP completely ignores the next command in its current direction.
-- This is like Befunge's #
c == '$' = moveIP' $ moveIP size state
-- _, |, /, \ are mirrors. They reflect the IP in the direction you'd expect.
| c == '/' = let d = case ipDirection of
East -> NorthWest
SouthEast -> West
SouthWest -> SouthWest
West -> SouthEast
NorthWest -> East
NorthEast -> NorthEast
in
moveIP' $ state { ip = ip { direction = d } }
| c == '\\' = let d = case ipDirection of
East -> SouthWest
SouthEast -> SouthEast
SouthWest -> East
West -> NorthEast
NorthWest -> NorthWest
NorthEast -> West
in
moveIP' $ state { ip = ip { direction = d } }
| c == '_' = let d = case ipDirection of
East -> East
SouthEast -> NorthEast
SouthWest -> NorthWest
West -> West
NorthWest -> SouthWest
NorthEast -> SouthEast
in
moveIP' $ state { ip = ip { direction = d } }
| c == '|' = let d = case ipDirection of
East -> West
SouthEast -> SouthWest
SouthWest -> SouthEast
West -> East
NorthWest -> NorthEast
NorthEast -> NorthWest
in
moveIP' $ state { ip = ip { direction = d } }
-- < and > act as either mirrors or branches, depending on the incoming direction.
| c == '<' = let d = case ipDirection of
East -> if isMemEdge then SouthEast else NorthEast
SouthEast -> NorthWest
SouthWest -> West
West -> East
NorthWest -> West
NorthEast -> SouthWest
in
moveIP' $ state { ip = ip { direction = d } }
| c == '>' = let d = case ipDirection of
East -> West
SouthEast -> East
SouthWest -> NorthEast
West -> if isMemEdge then NorthWest else SouthWest
NorthWest -> SouthEast
NorthEast -> East
in
moveIP' $ state { ip = ip { direction = d } }
|
-- switches to the previous IP (wrapping around from 0 to 5).
c == '[' = do
let State{ip = ip'} = moveIP size state
let newIps = ips // [ (ipIdx, ip') ]
let newIpIdx = (ipIdx + 5) `mod` 6
return state { ip = newIps ! newIpIdx
, ipIdx = newIpIdx
, ips = newIps
}
|
-- switches to the next IP (wrapping around from 5 to 0).
c == ']' = do
let State{ip = ip'} = moveIP size state
let newIps = ips // [ (ipIdx, ip') ]
let newIpIdx = (ipIdx + 1) `mod` 6
return state { ip = newIps ! newIpIdx
, ipIdx = newIpIdx
, ips = newIps
}
|
--takes the current memory edge modulo 6 and switches to the IP with that index.
c == '#' = do
let State{ip = ip'} = moveIP size state
let newIps = ips // [ (ipIdx, ip') ]
let newIpIdx = fromInteger (memEdge `mod` 6 + 6) `mod` 6
return state { ip = newIps ! newIpIdx
, ipIdx = newIpIdx
, ips = newIps
}
|
{------------------------- Memory Manipulation -------------------------}
-- moves the MP to the left neighbor.
c == '{' = moveIP' $ state { mp = memLeft mp }
|
-- moves the MP to the right neighbor.
c == '}' = moveIP' $ state { mp = memRight mp }
|
-- moves the MP backwards and to the left. This is equivalent to =}=.
c == '"' =
moveIP' $ state { mp = memReverse . memRight . memReverse $ mp }
|
-- moves the MP backwards and to the right. This is equivalent to ={=.
c == '\'' =
moveIP' $ state { mp = memReverse . memLeft . memReverse $ mp }
|
-- reverses the direction of the MP.
-- (This doesn't affect the current memory edge, but changes which edges are considered the left and right neighbor.)
c == '=' = moveIP' $ state { mp = memReverse mp }
|
-- moves the MP to the left neighbor if the current edge is zero or negative and to the right neighbor if it's positive.
c == '^' =
moveIP' $ state { mp = if isMemEdge then memRight mp else memLeft mp }
|
-- copies the value of left neighbor into the current edge if the current edge is zero or negative
-- and the value of the right neighbor if it's positive.
c == '&' = do
let neighbor = if isMemEdge then memRight mp else memLeft mp
moveIP' . setCurMemEdge $ getMemEdge state neighbor
|
{------------------------- Miscellaneous -------------------------}
-- terminates the program.
c == '@' = moveIP' $ state { terminated = True, errorMessage = "@" }
|
-- '.' is a no-op: the IP will simply pass through.
c == '.' = moveIP' state
| otherwise = moveIP' . setCurMemEdge . fromIntegral . C.ord $ c
where
memEdge = getMemEdge state mp
isMemEdge = memEdge > 0
setCurMemEdge = setMemEdge state mp
moveIP' = return . moveIP size
loadProgram :: String -> Program
loadProgram s =
let instructions = foldr (\(q, r, x) -> Map.insert (q, r) x) Map.empty
. concat $ populate s'
in
Program { instructions = instructions, state = state, size2 = size }
where
s' = lines . filter (/= ' ') $ formatProgram s
size :: Integer
size = fromIntegral . length . head $ s'
ssize :: Integer
ssize = size - 1
state :: State
state =
State { memory = Map.empty
, ip =
HexCoordinate { q = 0, r = -ssize, direction = East }
, ipIdx = 0
, ips =
A.listArray (0, 5)
[ HexCoordinate { q = 0
, r = -ssize
, direction = East
}
, HexCoordinate { q = ssize
, r = -ssize
, direction = SouthEast
}
, HexCoordinate { q = ssize
, r = 0
, direction = SouthWest
}
, HexCoordinate { q = 0
, r = ssize
, direction = West
}
, HexCoordinate { q = -ssize
, r = ssize
, direction = NorthWest
}
, HexCoordinate { q = -ssize
, r = 0
, direction = NorthEast
}
]
, mp = HexCoordinate { q = 0, r = 0, direction = East }
, terminated = False
, errorMessage = "Quit unexpectedly"
}
populate :: [String] -> [[(Integer, Integer, Char)]]
populate =
zipWith (\r l -> zipWith (\q x -> (q, r, x))
[ (max (-r - (size - 1)) (-size + 1)) .. ]
l)
[ -(size - 1) .. ]
run :: Program -> IO ()
run program@Program{ instructions = instructions
, state =
state@State{ ip = ip@HexCoordinate{ q = q
, r = r
, direction = direction
}
, ipIdx = ipIdx
, terminated = terminated
, errorMessage = errorMessage
}
, size2 = size
}
| terminated = do
putStrLn ""
when (errorMessage /= "@") $ hPutStr stderr errorMessage
| otherwise = do
let instruction = M.fromJust $ Map.lookup (q, r) instructions
-- printf "\t\tip{q=%d, r=%d, direction=%s} ipIdx=%d\n"
-- q
-- r
-- (show direction)
-- ipIdx
state <- execute state size instruction
run $ program { state = state }
main :: IO ()
main = do
args <- getArgs
s <- readFile (head args)
let program = loadProgram s
run program