1 module Main(main) where
3 import Data.List (last, intersperse, sortBy, intercalate, isInfixOf)
4 import Data.List.Split (splitOn)
5 import Data.Char (isLetter, ord, chr)
6 import qualified Data.Map.Lazy as Map
8 data Room = Room { name :: String
15 instrText <- readFile "advent04.txt"
16 let rooms = map (parseLine) $ lines instrText
21 part1 :: [Room] -> IO ()
23 print $ sum $ map (sector) validRooms
25 validChecksum room = (checksum room) == makeChecksum (name room)
26 validRooms = filter (validChecksum) rooms
28 part2 :: [Room] -> IO ()
30 print $ fst $ head $ filter (\sn -> isInfixOf "north" (snd sn)) sectorNames
32 validChecksum room = (checksum room) == makeChecksum (name room)
33 validRooms = filter (validChecksum) rooms
34 sectorNames = [((sector r),
35 shiftWord (sector r) (name r)) | r <- validRooms]
38 parseLine :: String -> Room
39 parseLine line = Room {name=name, sector=sector, checksum=checksum}
40 where components = splitOn "-" line
41 name = intercalate "-" $ reverse $ tail $ reverse components
42 sector = read $ head $ splitOn "[" $ last components
43 checksum = filter (isLetter) $ last components
45 countedLetters :: String -> [(Char, Int)]
46 countedLetters name = sortBy sortCLetter $ unsortedCountedLetters name
47 where unsortedCountedLetters name =
48 Map.toList $ Map.fromListWith (+) [(c, 1) | c <- filter (isLetter) name]
50 sortCLetter :: (Char, Int) -> (Char, Int) -> Ordering
51 sortCLetter (l1, n1) (l2, n2)
54 | n1 == n2 = compare l1 l2
56 makeChecksum :: String -> String
57 makeChecksum name = [l | (l, _) <- take 5 $ countedLetters name]
60 shiftWord :: Int -> String -> String
61 shiftWord shift letters = map (shiftLetter shift) letters
63 shiftLetter :: Int -> Char -> Char
64 shiftLetter shift letter
65 | isLetter letter = chr $ (ord letter - ord 'a' + shift) `mod` 26 + ord 'a'