-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay03Reorganization.hs
32 lines (27 loc) · 1.2 KB
/
Day03Reorganization.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
{-# LANGUAGE OverloadedStrings #-}
import Data.List (foldl', foldl1')
import Data.List.Split (chunksOf)
import qualified Data.Set as S
import Formatting
main :: IO ()
main = do
input <- readFile "input/day03.txt"
let rucksacks = lines input
duplicateSum = prioritySum . map splitHalfway $ rucksacks -- Expected: 8298
badgeSum = prioritySum . chunksOf 3 $ rucksacks -- Expected: 2708
fprintLn ("Part 1 (sum of duplicate element priorities): " % int) duplicateSum
fprintLn ("Part 2 (sum of badge priorities): " % int) badgeSum
where
priorityAccumulator = flip ((+) . itemPriority . commonItem)
prioritySum = foldl' priorityAccumulator 0
-- Numbers were chosen to make A map to 27 and a map to 1; everything else follows in ASCII :)
itemPriority :: Char -> Int
itemPriority item = mod (fromEnum item - 38) 58
-- Essentially splitAt but more convenient for reuse due to not returning a tuple
splitHalfway :: String -> [String]
splitHalfway line = [take halfLen line, drop halfLen line]
where
halfLen = div (length line) 2
-- This works because there should only be 1 common element in all cases
commonItem :: [String] -> Char
commonItem = S.elemAt 0 . foldl1' S.intersection . map S.fromList