-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.hs
36 lines (31 loc) · 1008 Bytes
/
Day10.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
module Day10
( part1
, part2
) where
import Data.Either (fromRight)
import Data.Maybe (fromJust)
import Data.Sequence as S (Seq, index, length)
import Helpers.Parsers (Parser, nums)
import KnotHash (fullHash, hash, initialCircle)
import Numeric (showHex)
import Text.Megaparsec (parse, sepBy)
import Text.Megaparsec.Char (char)
parseInput :: Parser [Int]
parseInput = do
sepBy (fromJust <$> nums) (char ',')
score :: (Int, Int, Seq Int) -> Int
score (curpos, _, circle) = a * b
where
origin = S.length circle - curpos
a = index circle origin
next = (origin + 1) `mod` S.length circle
b = index circle next
part1 :: Bool -> String -> String
part1 test =
show
. score
. hash (0, 0, initialCircle test)
. fromRight []
. parse parseInput ""
part2 :: Bool -> String -> String
part2 _ = concatMap (`showHex` "") . fullHash