31 lines
680 B
Haskell
31 lines
680 B
Haskell
module Day3.Part1 (
|
|||
|
|
splitContents,
|
||
|
|
itemsInBoth,
|
||
|
|
itemPriority,
|
||
|
|
priority,
|
||
|
|
rearrangementPriority,
|
||
|
|
)
|
||
|
|
where
|
||
|
|
|
||
|
|
import qualified Data.Set as Set
|
||
|
|
import Day3.Shared
|
||
|
|
|
||
|
|
|
||
|
|
splitContents :: Contents -> (Contents, Contents)
|
||
|
|
splitContents c = splitAt middle c
|
||
|
|
where
|
||
|
|
middle = length c `div` 2
|
||
|
|
|
||
|
|
itemsInBoth :: Contents -> Contents
|
||
|
|
itemsInBoth cs = Set.toList $ leftSet `Set.intersection` rightSet
|
||
|
|
where
|
||
|
|
(left, right) = splitContents cs
|
||
|
|
leftSet = Set.fromList left
|
||
|
|
rightSet = Set.fromList right
|
||
|
|
|
||
|
|
rearrangementPriority :: String -> Int
|
||
|
|
rearrangementPriority input = sum priorities
|
||
|
|
where
|
||
|
|
contents = lines input
|
||
|
|
priorities = map (priority . itemsInBoth) contents
|