From ff171b3e022a612d4ed348b5f5dad6bd9fe4562a Mon Sep 17 00:00:00 2001 From: Riley Apeldoorn Date: Sat, 4 Dec 2021 13:09:47 +0100 Subject: [PATCH] More day 4 improvements --- 4/solution.hs | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/4/solution.hs b/4/solution.hs index 32c95b7..797519d 100644 --- a/4/solution.hs +++ b/4/solution.hs @@ -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 _ [] = []