-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day20.hs
63 lines (49 loc) · 1.81 KB
/
Day20.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
module Day20
( part1
, part2
) where
import Text.Regex.TDFA (getAllTextMatches, (=~))
import Data.CircularList (CList, focus, fromList, insertR, removeR,
rightElements, rotN, rotR, rotateTo, size,
toList)
import Data.Maybe (fromJust)
decryptionKey = 811589153
index1 = 1000
index2 = 2000
index3 = 3000
parseInput :: [String] -> [(Int, Int)]
parseInput = zip [1 ..] . map read
move :: (Int, Int) -> CList (Int, Int) -> CList (Int, Int)
move pair@(_, number) clist =
insertR pair . rotN modded . removeR . fromJust . rotateTo pair $ clist
where
modded = mod number (size clist - 1)
mix :: CList (Int, Int) -> [(Int, Int)] -> CList (Int, Int)
mix = foldl (flip move)
remix :: [(Int, Int)] -> CList (Int, Int) -> CList (Int, Int)
remix = flip mix
score :: (Int, Int) -> CList (Int, Int) -> Int
score zero clist = score1 + score2 + score3
where
zlist = fromJust . rotateTo zero $ clist
score1 = snd . fromJust . focus . rotN index1 $ zlist
score2 = snd . fromJust . focus . rotN index2 $ zlist
score3 = snd . fromJust . focus . rotN index3 $ zlist
decrypt :: (Int, Int) -> (Int, Int)
decrypt (a, b) = (a, decryptionKey * b)
part1 :: Bool -> String -> String
part1 _ input = show . score zero . mix file $ indices
where
indices = parseInput . lines $ input
file = fromList indices
zero = head . filter (\x -> snd x == 0) $ indices
part2 :: Bool -> String -> String
part2 _ input =
show . score zero . last . take 11 . iterate (remix decryptedIndices) $
decryptedFile
where
indices = parseInput . lines $ input
file = fromList indices
zero = head . filter (\x -> snd x == 0) $ indices
decryptedFile = fmap decrypt file
decryptedIndices = map decrypt indices