-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay16.hs
88 lines (73 loc) · 2.29 KB
/
Day16.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
module Day16
( part1
, part2
) where
import Data.Either (fromRight)
import Data.Sequence as S (Seq, elemIndexL, empty, fromList,
index, length, splitAt, update,
(><))
import Helpers.Parsers (Parser, nums)
import Helpers.Search (findPattern)
import Text.Megaparsec (optional, parse, (<|>))
import Text.Megaparsec.Char (char, eol, lowerChar, string)
dance test
| test = fromList ['a' .. 'e']
| otherwise = fromList ['a' .. 'p']
type Dance = Seq Char
parseInput :: Parser (Dance -> Dance)
parseInput = spin <|> exchange <|> partner <|> finish
spin :: Parser (Dance -> Dance)
spin = do
char 's'
Just a <- nums
optional . char $ ','
(. splitted a) <$> parseInput
splitted :: Int -> Dance -> Dance
splitted a dance = after >< before
where
splitPoint = S.length dance - a
(before, after) = S.splitAt splitPoint dance
exchange :: Parser (Dance -> Dance)
exchange = do
char 'x'
Just a <- nums
char '/'
Just b <- nums
optional . char $ ','
(. exchanged a b) <$> parseInput
exchanged :: Int -> Int -> Dance -> Dance
exchanged a b dance = update a to . update b from $ dance
where
from = index dance a
to = index dance b
partner :: Parser (Dance -> Dance)
partner = do
char 'p'
a <- lowerChar
char '/'
b <- lowerChar
optional . char $ ','
(. partnered a b) <$> parseInput
partnered :: Char -> Char -> Dance -> Dance
partnered a b dance = update indexA b . update indexB a $ dance
where
(Just indexA) = elemIndexL a dance
(Just indexB) = elemIndexL b dance
finish :: Parser (Dance -> Dance)
finish = do
eol
return id
makeDance :: String -> (Dance -> Dance)
makeDance = fromRight id . parse parseInput ""
findResult :: Bool -> Int -> (Dance -> Dance) -> String
findResult test point steps = foldr (:) [] $ shortList !! patterned
where
shortList = take 150 . iterate steps . dance $ test
pat = findPattern 0 10 (==) . fromList $ shortList
patterned = point `mod` pat
part1 :: Bool -> String -> String
part1 test input = foldr (:) [] . steps . dance $ test
where
steps = makeDance input
part2 :: Bool -> String -> String
part2 test = findResult test (10 ^ 9) . makeDance