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