-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day12.hs
59 lines (48 loc) · 1.88 KB
/
Day12.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
-- Day 12: Subterranean Sustainability --
--
-- There's no parser for this one, my input is already included.
--
-- Usage: runhaskell Day12.hs
import Graphics.EasyPlot
transform :: String -> String
transform ('#' : '#' : '#' : '#' : ".") = "####."
transform ('#' : '#' : '.' : '#' : ".") = "####."
transform ('#' : '#' : '.' : '.' : ".") = "###.."
transform ('#' : '.' : '#' : '#' : "#") = "#.###"
transform ('#' : '.' : '.' : '#' : ".") = "#.##."
transform ('.' : '#' : '#' : '#' : "#") = ".####"
transform ('.' : '#' : '#' : '.' : "#") = ".##.#"
transform ('.' : '#' : '.' : '.' : "#") = ".##.#"
transform ('.' : '#' : '.' : '.' : ".") = ".##.."
transform ('.' : '.' : '#' : '#' : "#") = "..###"
transform ('.' : '.' : '#' : '.' : "#") = "..#.#"
transform ('.' : '.' : '.' : '#' : "#") = "..###"
transform (l2 : l1 : _ : r1 : r2 ) = l2 : l1 : '.' : r1 : r2
initial :: String
initial = "######....##.###.#..#####...#.#.....#..#.#.##......###.#..##..#..##..#.##..#####.#.......#.....##.."
generation' :: String -> String
generation' (l2 : l1 : c : r1 : r2 : xs) =
c' : generation' (l1 : c : r1 : r2 : xs)
where
[_, _, c', _, _] = transform [l2, l1, c, r1, r2]
generation' [l1, c, r1, r2] = [r1, r2]
generation :: String -> String
generation str@(l2 : l1 : _) = l2 : l1 : generation' str
evolvedSum :: String -> Int -> Int
evolvedSum start generations = sum $ map fst plants
where
plants = filter ((== '#') . snd) $ zip [-padding..] evolved
evolved = foldl (\acc _ -> generation acc) padded [1..generations]
padded = pad start
padding :: Int
padding = 100
pad :: String -> String
pad str = replicate padding '.' ++ str ++ replicate padding '.'
part1 :: Int
part1 = evolvedSum initial 20
part2 :: [(Int, Int)]
part2 = zip [1..] $ map (evolvedSum initial) [1..50]
main :: IO Bool
main = do
print part1
plot X11 $ Data2D [] [] $ map (\(a, b) -> (fromIntegral a, fromIntegral b)) part2