-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.hs
33 lines (26 loc) · 1.08 KB
/
part1.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
33
import Data.Text (Text, pack, splitOn, strip, takeWhileEnd, unpack)
substringWithoutCard = takeWhileEnd (/= ':')
splitOndSpace = map (splitOn (pack " "))
getNumbersThatAppearsInBothLists :: [Int] -> [Int] -> [Int]
getNumbersThatAppearsInBothLists xs ys = [x | x <- xs, y <- ys, x == y]
getScoreRec :: [Int] -> Int -> Int
getScoreRec [] acc = acc
getScoreRec arr acc = getScoreRec (tail arr) (acc * 2)
getScore :: [Int] -> Int
getScore [] = 0
getScore [x] = 1
getScore arr = getScoreRec (tail arr) 1
main = do
-- contents <- readFile "example"
input <- readFile "input"
let bits = map (splitOn (pack "|") . pack) (lines input)
let bitsWithoutCard = map (map substringWithoutCard) bits
let stripped = map (map strip) bitsWithoutCard
let numsAsStr = map splitOndSpace stripped
let unpacked = map (map (map unpack)) numsAsStr
let filtered = map (map (filter (/= ""))) unpacked
let nums = map (map (map (read :: String -> Int))) filtered
let overlapp = map (foldl1 getNumbersThatAppearsInBothLists) nums
let scores = map getScore overlapp
putStrLn "Part 1:"
print (sum scores)