More day 4 improvements

This commit is contained in:
Riley Apeldoorn 2021-12-04 13:09:47 +01:00
parent 70bd51277e
commit ff171b3e02

View file

@ -14,41 +14,30 @@ parse (l : ls) = (n, b)
parseBoards :: [String] -> [Board]
parseBoards [] = []
parseBoards l = parse a : parseBoards b
where (a,b) = splitAt 5 l
parse = map $ map (Unmarked . read) . words
where (a,b) = splitAt 5 l
parse = map $ map ((,False) . read) . words
type Board = [[Cell]]
rows :: Board -> [[Cell]]
rows = id
cols :: Board -> [[Cell]]
cols = transpose
hasWon :: Board -> Bool
hasWon b = check (rows b) || check (cols b)
hasWon b = check b || check (transpose b)
where check = any $ all marked
score :: Int -> Board -> Int
score n b = n * sum [ x | c <- b, Unmarked x <- c ]
score n b = n * sum [ x | c <- b, (x, False) <- c ]
call :: Int -> Board -> Board
call n = map (map mark)
where mark (Unmarked x) | x == n = Marked x
mark x = x
where mark (x,s) = (x, s || x == n)
data Cell = Unmarked Int
| Marked Int
type Cell = (Int, Bool)
marked :: Cell -> Bool
marked (Marked _) = True
marked _ = False
marked = snd
solve :: Input -> (Int, Int)
solve input = (f input, g input)
where wins extr (n,b) = uncurry score $ extr $ bingo n b
f = wins head
g = wins last
solve (n, b) = (head r, last r)
where r = map (uncurry score) $ bingo n b
bingo :: [Int] -> [Board] -> [(Int, Board)]
bingo _ [] = []