Compare commits
2 Commits
70a14bd150
...
bb8075b0c0
Author | SHA1 | Date | |
---|---|---|---|
bb8075b0c0 | |||
97b9ec2826 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
input.txt
|
2265
day1/input.txt
2265
day1/input.txt
File diff suppressed because it is too large
Load Diff
2500
day2/input.txt
2500
day2/input.txt
File diff suppressed because it is too large
Load Diff
48
day3/main.hs
Normal file
48
day3/main.hs
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import System.IO
|
||||||
|
|
||||||
|
import Data.Char (ord)
|
||||||
|
|
||||||
|
import Data.Set (Set)
|
||||||
|
import qualified Data.Set as Set
|
||||||
|
|
||||||
|
import Data.List.Split
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
itemprio :: Char -> Int
|
||||||
|
itemprio c
|
||||||
|
| 'a' <= c && c <= 'z' = ord c - ord 'a' + 1
|
||||||
|
| 'A' <= c && c <= 'Z' = ord c - ord 'A' + 27
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
duplitems :: Ord a => [a] -> [a] -> [a]
|
||||||
|
duplitems c1 c2 = Set.elems $ Set.intersection s1 s2
|
||||||
|
where s1 = (Set.fromList c1)
|
||||||
|
s2 = (Set.fromList c2)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
splitEqual :: Int -> [a] -> [[a]]
|
||||||
|
splitEqual n x = chunksOf (length x `div` n) x
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
rsprio :: String -> Int
|
||||||
|
rsprio x = itemprio $ head $ duplitems (head rs) (last rs)
|
||||||
|
where rs = splitEqual 2 x
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
grpprio :: [String] -> Int
|
||||||
|
grpprio = itemprio . head . foldr1 duplitems
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
handler :: String -> String
|
||||||
|
handler s = (show $ sum $ map rsprio $ lines s) ++ "\n" ++
|
||||||
|
(show $ sum $ map grpprio $ chunksOf 3 $ lines s) ++ "\n"
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
interact handler
|
Loading…
x
Reference in New Issue
Block a user