-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMain.hs
85 lines (73 loc) · 1.95 KB
/
Main.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import Data.List (stripPrefix)
import Data.List.Split (splitOn)
import Data.Maybe (fromJust)
import Debug.Trace
import System.IO
main :: IO ()
main = do
handle <- openFile "input.txt" ReadMode
contents <- hGetContents handle
let linesList = lines contents
let res = fn1 linesList
print res
let res = fn2 linesList
print res
maxRed = 12
maxGreen = 13
maxBlue = 14
fn1 :: [String] -> Int
fn1 [] = 0
fn1 (x:xs)
| isPossible = id + fn1 xs
| otherwise = fn1 xs
where
(isPossible, id) = count x
count line = (isPossible gamesStr, gameID)
where
(gameID, gamesStr) = parseGame line
parseGame :: String -> (Int, [(Int, String)])
parseGame line = (gameID, games)
where
split = splitOn ": " line
gameID = read (fromJust (stripPrefix "Game " (split !! 0))) :: Int
gamesStr = concat [splitOn ", " x | x <- splitOn "; " (split !! 1)]
games = [parsePlay x | x <- gamesStr]
parsePlay :: String -> (Int, String)
parsePlay x = (count, color)
where
split = splitOn " " x
count = read (split !! 0) :: Int
color = split !! 1
isPossible :: [(Int, String)] -> Bool
isPossible [] = True
isPossible (x:xs)
| color == "red" =
if count > maxRed
then False
else isPossible xs
| color == "green" =
if count > maxGreen
then False
else isPossible xs
| color == "blue" =
if count > maxBlue
then False
else isPossible xs
where
(count, color) = x
fn2 :: [String] -> Int
fn2 [] = 0
fn2 (x:xs) = count2 x + fn2 xs
count2 line =
let (red, green, blue) = fewest gamesStr 0 0 0
in red * green * blue
where
(_, gamesStr) = parseGame line
fewest :: [(Int, String)] -> Int -> Int -> Int -> (Int, Int, Int)
fewest [] red green blue = (red, green, blue)
fewest (x:xs) red green blue
| color == "red" = fewest xs (max red count) green blue
| color == "green" = fewest xs red (max green count) blue
| color == "blue" = fewest xs red green (max blue count)
where
(count, color) = x