blob: bee4780e510b51bdd55a6b2eddfdb3ec959bd54a (
plain)
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
|
module Main where
import Data.Foldable
import Data.List
import Data.Maybe
import Data.Ord
import Input
import Util
data Board = Board [[Maybe Int]]
deriving (Show)
main :: IO ()
main = do
inp <- getInput 4
let order = read <$> splitOn (== ',') (head inp)
boards = Board . map (map (Just . read) . words) <$> splitOn null (drop 2 inp)
won (Board l) = any (all isNothing) l || any (all isNothing) (transpose l)
cross (Board bd) n = Board (map (map (>>= \x -> if x == n then Nothing else Just x)) bd)
timetowin bd = find (won . snd) . zip [0::Int ..] $ scanl cross bd (toList order)
wins = catMaybes . toList $ timetowin <$> boards
score idx (Board l) = (toList order !! (idx - 1)) * sum (catMaybes (concat l))
wonboard = minimumBy (comparing fst) wins
lostboard = maximumBy (comparing fst) wins
print (uncurry score wonboard)
print (uncurry score lostboard)
|