aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2017-12-22 19:01:45 +0100
committerYuchen Pei <me@ypei.me>2017-12-22 19:01:45 +0100
commit73c99fa1890b2d522fda1da423cc9db42c3a399c (patch)
tree2e173b611f6bbe2473032664bfb12d01737c137b
parentf8117394a4d0dfedec3af5dd3729eea2ed2b4db3 (diff)
finished Day 21
-rw-r--r--Puzzle21.hs47
1 files changed, 47 insertions, 0 deletions
diff --git a/Puzzle21.hs b/Puzzle21.hs
new file mode 100644
index 0000000..a900aa0
--- /dev/null
+++ b/Puzzle21.hs
@@ -0,0 +1,47 @@
+import Data.List (transpose)
+import Data.List.Split (splitOn)
+import Data.Map (Map)
+import qualified Data.Map as Map
+
+groupD2 :: [([[Char]] -> [[Char]])]
+groupD2 = [id, r, t, r . t, t . r . t, r . t . r . t, t . r . t . r . t, r . t . r . t . r . t]
+ where r = reverse
+ t = transpose
+
+parseLine :: [Char] -> ([[Char]], [[Char]])
+parseLine line = (splitOn "/" xs, splitOn "/" ys)
+ where [xs, ys] = splitOn " => " line
+
+parseInput :: [Char] -> [([[Char]], [[Char]])]
+parseInput xs = parseLine <$> lines xs
+
+getMap :: [([[Char]], [[Char]])] -> Map [[Char]] [[Char]]
+getMap xs = Map.fromList [(sigma x, y) | sigma <- groupD2, (x, y) <- xs]
+
+step :: Int -> [[Char]] -> Int
+step n xs
+ | n == 0 = length $ filter (=='#') $ mconcat xs
+ | otherwise = step (n - 1) (f (if even $ length xs then 2 else 3) xs [])
+
+f :: Int -> [[Char]] -> [[Char]] -> [[Char]]
+f _ [] ys = ys
+f k xs ys = f k (drop k xs) $ ys ++ (g k (take k xs) (replicate (k + 1) ""))
+
+g :: Int -> [[Char]] -> [[Char]] -> [[Char]]
+g k xs ys
+ | null $ mconcat xs = ys
+ | otherwise = g k (drop k <$> xs) $ zipWith (++) ys (table Map.! (take k <$> xs))
+
+table :: Map [[Char]] [[Char]]
+table = getMap $ parseInput input
+
+initShape :: [[Char]]
+initShape = [".#.", "..#", "###"]
+
+solve1 :: Int
+solve1 = step 18 initShape
+
+input0 :: [Char]
+input0 = "../.# => ##./#../...\n.#./..#/### => #..#/..../..../#..#"
+
+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###/###/### => ##../..#./##../..#."