Improvements for day 1 and day 2
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
module Commons where
|
module Commons where
|
||||||
|
|
||||||
import System.IO
|
import GHC.IO.Handle (isEOF)
|
||||||
import Text.Read
|
|
||||||
|
|
||||||
|
|
||||||
type CalibrationLine = String
|
type CalibrationLine = String
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
module Part1 where
|
module Part1 where
|
||||||
|
|
||||||
import Commons
|
import Commons
|
||||||
import Data.Char
|
import Data.Char (isDigit)
|
||||||
|
|
||||||
|
|
||||||
getCalibrationValuePart :: CalibrationLine -> Char
|
getCalibrationValuePart :: CalibrationLine -> Char
|
||||||
|
|||||||
@@ -1,32 +1,33 @@
|
|||||||
module Part2 where
|
module Part2 where
|
||||||
|
|
||||||
import Commons
|
import Commons
|
||||||
import Data.Char
|
import Data.Char (isDigit)
|
||||||
|
import Data.List (isPrefixOf)
|
||||||
|
|
||||||
|
|
||||||
getCalibrationValueLeft :: CalibrationLine -> Char
|
getCalibrationValueLeft :: CalibrationLine -> Char
|
||||||
getCalibrationValueLeft ('o': 'n': 'e': _) = '1'
|
getCalibrationValueLeft line | "one" `isPrefixOf` line = '1'
|
||||||
getCalibrationValueLeft ('t': 'w': 'o': _) = '2'
|
| "two" `isPrefixOf` line = '2'
|
||||||
getCalibrationValueLeft ('t': 'h': 'r': 'e': 'e': _) = '3'
|
| "three" `isPrefixOf` line = '3'
|
||||||
getCalibrationValueLeft ('f': 'o': 'u': 'r': _) = '4'
|
| "four" `isPrefixOf` line = '4'
|
||||||
getCalibrationValueLeft ('f': 'i': 'v': 'e': _) = '5'
|
| "five" `isPrefixOf` line = '5'
|
||||||
getCalibrationValueLeft ('s': 'i': 'x': _) = '6'
|
| "six" `isPrefixOf` line = '6'
|
||||||
getCalibrationValueLeft ('s': 'e': 'v': 'e': 'n': _) = '7'
|
| "seven" `isPrefixOf` line = '7'
|
||||||
getCalibrationValueLeft ('e': 'i': 'g': 'h': 't': _) = '8'
|
| "eight" `isPrefixOf` line = '8'
|
||||||
getCalibrationValueLeft ('n': 'i': 'n': 'e': _) = '9'
|
| "nine" `isPrefixOf` line = '9'
|
||||||
getCalibrationValueLeft (h: _) | isDigit h = h
|
getCalibrationValueLeft (h: _) | isDigit h = h
|
||||||
getCalibrationValueLeft (_: t) = getCalibrationValueLeft t
|
getCalibrationValueLeft (_: t) = getCalibrationValueLeft t
|
||||||
|
|
||||||
getCalibrationValueRight :: CalibrationLine -> Char
|
getCalibrationValueRight :: CalibrationLine -> Char
|
||||||
getCalibrationValueRight ('e': 'n': 'o': _) = '1'
|
getCalibrationValueRight line | "eno" `isPrefixOf` line = '1'
|
||||||
getCalibrationValueRight ('o': 'w': 't': _) = '2'
|
| "owt" `isPrefixOf` line = '2'
|
||||||
getCalibrationValueRight ('e': 'e': 'r': 'h': 't': _) = '3'
|
| "eerht" `isPrefixOf` line = '3'
|
||||||
getCalibrationValueRight ('r': 'u': 'o': 'f': _) = '4'
|
| "ruof" `isPrefixOf` line = '4'
|
||||||
getCalibrationValueRight ('e': 'v': 'i': 'f': _) = '5'
|
| "evif" `isPrefixOf` line = '5'
|
||||||
getCalibrationValueRight ('x': 'i': 's': _) = '6'
|
| "xis" `isPrefixOf` line = '6'
|
||||||
getCalibrationValueRight ('n': 'e': 'v': 'e': 's': _) = '7'
|
| "neves" `isPrefixOf` line = '7'
|
||||||
getCalibrationValueRight ('t': 'h': 'g': 'i': 'e': _) = '8'
|
| "thgie" `isPrefixOf` line = '8'
|
||||||
getCalibrationValueRight ('e': 'n': 'i': 'n': _) = '9'
|
| "enin" `isPrefixOf` line = '9'
|
||||||
getCalibrationValueRight (h: _) | isDigit h = h
|
getCalibrationValueRight (h: _) | isDigit h = h
|
||||||
getCalibrationValueRight (_: t) = getCalibrationValueRight t
|
getCalibrationValueRight (_: t) = getCalibrationValueRight t
|
||||||
|
|
||||||
|
|||||||
@@ -1,34 +1,34 @@
|
|||||||
module Commons where
|
module Commons where
|
||||||
|
|
||||||
import Data.String.Utils
|
import Data.List.Utils (split)
|
||||||
import System.IO
|
import GHC.IO.Handle (isEOF)
|
||||||
import Text.Read
|
import Data.List (isSuffixOf)
|
||||||
|
|
||||||
|
|
||||||
data Round = Round { red :: Int, green :: Int, blue :: Int }
|
data Round = Round { red :: Int, green :: Int, blue :: Int }
|
||||||
data Game = Game { gid :: Int, rounds :: [Round] }
|
data Game = Game { gid :: Int, rounds :: [Round] }
|
||||||
|
|
||||||
|
|
||||||
parseCubes :: [String] -> Round -> Round
|
parseCubes :: [[String]] -> Round -> Round
|
||||||
parseCubes [] round = round
|
parseCubes [] round = round
|
||||||
parseCubes ([h, ' ', 'r', 'e', 'd']: t) round = parseCubes t round {red = read [h]}
|
parseCubes ([h, "red"]: t) round = parseCubes t round {red = read h}
|
||||||
parseCubes ([h1, h2, ' ', 'r', 'e', 'd']: t) round = parseCubes t round {red = read [h1, h2]}
|
parseCubes ([h, "green"]: t) round = parseCubes t round {green = read h}
|
||||||
parseCubes ([h, ' ', 'g', 'r', 'e', 'e', 'n']: t) round = parseCubes t round {green = read [h]}
|
parseCubes ([h, "blue"]: t) round = parseCubes t round {blue = read h}
|
||||||
parseCubes ([h1, h2, ' ', 'g', 'r', 'e', 'e', 'n']: t) round = parseCubes t round {green = read [h1, h2]}
|
|
||||||
parseCubes ([h, ' ', 'b', 'l', 'u', 'e']: t) round = parseCubes t round {blue = read [h]}
|
|
||||||
parseCubes ([h1, h2, ' ', 'b', 'l', 'u', 'e']: t) round = parseCubes t round {blue = read [h1, h2]}
|
|
||||||
|
|
||||||
parseRound :: String -> Round
|
parseRound :: String -> Round
|
||||||
parseRound roundRaw = parseCubes (split ", " roundRaw) Round {red = 0, green = 0, blue = 0}
|
parseRound roundRaw = parseCubes (map words (split ", " roundRaw)) Round {red = 0, green = 0, blue = 0}
|
||||||
|
|
||||||
parseRounds :: String -> [Round]
|
parseRounds :: String -> [Round]
|
||||||
parseRounds roundsRaw = map parseRound (split "; " roundsRaw)
|
parseRounds roundsRaw = map parseRound (split "; " roundsRaw)
|
||||||
|
|
||||||
|
parseGameId :: String -> String
|
||||||
|
parseGameId ('G': 'a': 'm': 'e': ' ': t) = parseGameId t
|
||||||
|
parseGameId (h: t) = h: parseGameId t
|
||||||
|
parseGameId [] = []
|
||||||
|
|
||||||
parseGame :: String -> Game
|
parseGame :: String -> Game
|
||||||
parseGame ('G': 'a': 'm': 'e': ' ': h: ':': ' ': t) = Game {gid = read [h], rounds = parseRounds t}
|
parseGame line = let (h: t: _) = split ": " line
|
||||||
parseGame ('G': 'a': 'm': 'e': ' ': h1: h2: ':': ' ': t) = Game {gid = read [h1, h2], rounds = parseRounds t}
|
in Game {gid = read (parseGameId h), rounds = parseRounds t}
|
||||||
parseGame ('G': 'a': 'm': 'e': ' ': h1: h2: h3: ':': ' ': t) = Game {gid = read [h1, h2, h3], rounds = parseRounds t}
|
|
||||||
parseGame (_: t) = parseGame t
|
|
||||||
|
|
||||||
parse :: IO [Game]
|
parse :: IO [Game]
|
||||||
parse = do done <- isEOF
|
parse = do done <- isEOF
|
||||||
|
|||||||
Reference in New Issue
Block a user