-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.hs
31 lines (25 loc) · 858 Bytes
/
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
import Data.List
removeDash :: String -> String
removeDash = filter $ \c -> c /= '-'
sectorIdOrZero :: String -> Int
sectorIdOrZero room =
let (']':c5:c4:c3:c2:c1:'[':d3:d2:d1:xs) = reverse $ removeDash room
top5 = map head $ take 5 $ reverse $ sortOn length $ group $ reverse $ sort xs
in if [c1,c2,c3,c4,c5] == top5
then read [d1,d2,d3]
else 0
rotate :: Int -> Char -> Char
rotate _ '-' = ' '
rotate id c =
let i = fromEnum c - fromEnum 'a'
i' = (i + id) `mod` 26
in toEnum (i' + fromEnum 'a')
decrypt :: String -> (String, Int)
decrypt room =
let (']':_:_:_:_:_:'[':d3:d2:d1:xs) = reverse room
id = read [d1,d2,d3]
in (map (rotate id) $ reverse xs, id)
part1 :: String -> Int
part1 = sum . map sectorIdOrZero . lines
part2 :: String -> [(String, Int)]
part2 = filter (isInfixOf "north" . fst) . map decrypt . lines