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
|
-- requires cabal install --lib megaparsec parser-combinators vector
module Main (main) where
import Control.Monad (void, when)
import Data.Functor
import Data.List qualified as L
import Data.Map qualified as M
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
import System.Exit (die)
exampleExpectedOutput = 110
type Input = M.Map (Int, Int) Bool -- True if there is an elf
type Parser = Parsec Void String
inputToList :: String -> [((Int, Int), Bool)]
inputToList = snd . L.foldl' nextElf ((0, 0), [])
where
nextElf :: ((Int, Int), [((Int, Int), Bool)]) -> Char -> ((Int, Int), [((Int, Int), Bool)])
nextElf ((x, y), acc) '\n' = ((0, y+1), acc)
nextElf ((x, y), acc) '.' = ((x+1, y), acc)
nextElf ((x, y), acc) '#' = ((x+1, y), acc ++ [((x, y), True)])
parseInput :: String -> IO Input
parseInput filename = do
input <- readFile filename
return $ M.fromList (inputToList input)
type Proposals = M.Map (Int, Int) (Int, Int, Bool)
firstHalfStep :: Input -> Int -> Proposals
firstHalfStep input iter = M.filter (\(_, _, e) -> e) $ M.foldrWithKey eval M.empty input
where
eval :: (Int, Int) -> Bool -> Proposals -> Proposals
eval (x, y) _ = eval' (x, y, 0)
eval' :: (Int, Int, Int) -> Proposals -> Proposals
eval' (x, y, 4) acc = acc
eval' (x, y, r) acc | alone = acc
| otherwise = case (iter + r) `mod` 4 of
0 -> if M.member (x-1, y-1) input || M.member (x, y-1) input || M.member (x+1, y-1) input
then next
else if M.member (x, y-1) acc
then M.insert (x, y-1) (x, y, False) acc -- two or more elves attempted to move to the same spot
else M.insert (x, y-1) (x, y, True) acc
1 -> if M.member (x-1, y+1) input || M.member (x, y+1) input || M.member (x+1, y+1) input
then next
else if M.member (x, y+1) acc
then M.insert (x, y+1) (x, y, False) acc -- two or more elves attempted to move to the same spot
else M.insert (x, y+1) (x, y, True) acc
2 -> if M.member (x-1, y-1) input || M.member (x-1, y) input || M.member (x-1, y+1) input
then next
else if M.member (x-1, y) acc
then M.insert (x-1, y) (x, y, False) acc -- two or more elves attempted to move to the same spot
else M.insert (x-1, y) (x, y, True) acc
3 -> if M.member (x+1, y-1) input || M.member (x+1, y) input || M.member (x+1, y+1) input
then next
else if M.member (x+1, y) acc
then M.insert (x+1, y) (x, y, False) acc -- two or more elves attempted to move to the same spot
else M.insert (x+1, y) (x, y, True) acc
where
alone = M.notMember (x-1, y-1) input
&& M.notMember (x, y-1) input
&& M.notMember (x+1, y-1) input
&& M.notMember (x-1, y) input
&& M.notMember (x+1, y) input
&& M.notMember (x-1, y+1) input
&& M.notMember (x, y+1) input
&& M.notMember (x+1, y+1) input
next = eval' (x, y, r+1) acc
secondHalfStep :: Input -> Proposals -> Input
secondHalfStep = M.foldrWithKey eval
where
eval :: (Int, Int) -> (Int, Int, Bool) -> Input -> Input
eval (x', y') (x, y, _) acc = M.insert (x', y') True $ M.delete (x, y) acc
compute :: Input -> Int -> Int
compute input imax = compute' 0 input
where
compute' :: Int -> Input -> Int
compute' i input | imax == i = (xmax - xmin + 1) * (ymax - ymin + 1) - M.size input
| otherwise = compute' (i+1) $ secondHalfStep input (firstHalfStep input i)
where
keys = M.keys input
xs = L.map fst keys
ys = L.map snd keys
xmax = maximum xs
xmin = minimum xs
ymax = maximum ys
ymin = minimum ys
main :: IO ()
main = do
example <- parseInput "example"
--putStr $ showMap example
let exampleOutput = compute example 10
when (exampleOutput /= exampleExpectedOutput) (die $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
input <- parseInput "input"
print $ compute input 10
--showMap :: Input -> String
--showMap input = printMap' xmin ymin ""
-- where
-- printMap' :: Int -> Int -> String -> String
-- printMap' x y acc | M.member (x, y) input = printMap' (x+1) y (acc ++ "#")
-- | y > ymax = acc
-- | x <= xmax = printMap' (x+1) y (acc ++ ".")
-- | otherwise = printMap' xmin (y+1) (acc ++ "\n")
-- keys = M.keys input
-- xs = L.map fst keys
-- ys = L.map snd keys
-- xmax = maximum xs
-- xmin = minimum xs
-- ymax = maximum ys
-- ymin = minimum ys
|