This commit is contained in:
ctsk
2023-08-29 09:24:31 +02:00
parent 3134dcb526
commit 404d2180af
7 changed files with 342 additions and 8 deletions

34
src/Days/D03.hs Normal file
View File

@@ -0,0 +1,34 @@
{-# LANGUAGE ImportQualifiedPost #-}
module Days.D03 where
import Data.Char (ord)
import Data.List (intersect)
import Data.List.Extra (chunksOf)
import Lib (Day (StringDay), definitive)
type Intermediate = [String]
halves :: [a] -> ([a], [a])
halves l = splitAt (length l `div` 2) l
priority :: Char -> Int
priority c =
let o = ord c
in if o < ord 'a'
then o - ord 'A' + 26 + 1
else o - ord 'a' + 1
part1 :: [String] -> Int
part1 = sum . map (priority . head . uncurry intersect . halves)
part2 :: [String] -> Int
part2 = sum . map (priority . head . foldl1 intersect) . chunksOf 3
day :: Day
day =
StringDay
( \s ->
let l = lines s
in Right (definitive $ part1 l, definitive $ part2 l)
)