-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day4.hs
63 lines (55 loc) · 1.66 KB
/
Day4.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 Day4
( part1
, part2
) where
import Data.Char (chr, isAlpha, isDigit, ord)
import Data.List as L (group, intersperse, isInfixOf, sort,
sortBy, concat)
import Data.Maybe (catMaybes)
import Data.Ord (Down(..), comparing)
import Data.Text as T (Text, pack, concat, unpack)
import Helpers.Parsers.Text (Parser, parseByLine, decimal)
import Text.Megaparsec (many, optional, takeWhileP)
import Text.Megaparsec.Char (char, eol)
type Cipher = ([String], Int)
parseInput :: Text -> [Cipher]
parseInput = catMaybes . parseByLine parseCipher
parseCipher :: Parser (Maybe Cipher)
parseCipher = do
name <- many word
iD <- decimal
char '['
checksum <- unpack <$> takeWhileP Nothing isAlpha
char ']'
optional eol
let checked =
take 5
. map head
. sortBy (comparing (Down . length))
. group
. sort
. L.concat
$ name
result
| checked == checksum = Just (name, iD)
| otherwise = Nothing
return result
word :: Parser String
word = do
result <- unpack <$> takeWhileP Nothing isAlpha
char '-'
return result
decipher :: Cipher -> (String, Int)
decipher (wordList, iD) = (unwords . map (map decode) $ wordList, iD)
where
decode c = chr . (97 +) . mod (ord c - 97 + iD) $ 26
part1 :: Bool -> Text -> String
part1 _ = show . sum . map snd . parseInput
part2 :: Bool -> Text -> String
part2 _ =
show
. snd
. head
. filter (\x -> "northpole" `isInfixOf` fst x)
. map decipher
. parseInput