It took me the afternoon but I got a LSP working
This commit is contained in:
@@ -3,13 +3,13 @@ module Commons where
|
||||
import System.IO
|
||||
import Text.Read
|
||||
|
||||
type CalibrationLine = String
|
||||
|
||||
type CalibrationLine = String
|
||||
type CalibrationDocument = [CalibrationLine]
|
||||
|
||||
|
||||
parseLine :: IO CalibrationLine
|
||||
parseLine = do line <- getLine
|
||||
return line
|
||||
parseLine = do getLine
|
||||
|
||||
parse :: IO CalibrationDocument
|
||||
parse = do done <- isEOF
|
||||
|
||||
@@ -4,8 +4,9 @@ import Commons
|
||||
import Part1
|
||||
import Part2
|
||||
|
||||
|
||||
main = do doc <- parse
|
||||
let part1Res = sum (Part1.getCalibrationValues doc)
|
||||
putStrLn (show (part1Res))
|
||||
print part1Res
|
||||
let part2Res = sum (Part2.getCalibrationValues doc)
|
||||
putStrLn (show (part2Res))
|
||||
print part2Res
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
module Part1 where
|
||||
|
||||
import Commons
|
||||
import Data.Char
|
||||
|
||||
|
||||
getCalibrationValuePart :: CalibrationLine -> Char
|
||||
getCalibrationValuePart (h: _) | h >= '0' && h <= '9' = h
|
||||
getCalibrationValuePart (h: _) | isDigit h = h
|
||||
getCalibrationValuePart (_: t) = getCalibrationValuePart t
|
||||
|
||||
getCalibrationValue :: CalibrationLine -> Integer
|
||||
getCalibrationValue line = read [(getCalibrationValuePart line), (getCalibrationValuePart (reverse line))]
|
||||
getCalibrationValue line = read [getCalibrationValuePart line, getCalibrationValuePart (reverse line)]
|
||||
|
||||
getCalibrationValues :: CalibrationDocument -> [Integer]
|
||||
getCalibrationValues (h: []) = [(getCalibrationValue h)]
|
||||
getCalibrationValues (h: t) = ((getCalibrationValue h): (getCalibrationValues t))
|
||||
getCalibrationValues [h] = [getCalibrationValue h]
|
||||
getCalibrationValues (h: t) = getCalibrationValue h: getCalibrationValues t
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
module Part2 where
|
||||
|
||||
import Commons
|
||||
import Data.Char
|
||||
|
||||
|
||||
getCalibrationValueLeft :: CalibrationLine -> Char
|
||||
getCalibrationValueLeft ('o': 'n': 'e': _) = '1'
|
||||
@@ -12,7 +14,7 @@ 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 (h: _) | h >= '0' && h <= '9' = h
|
||||
getCalibrationValueLeft (h: _) | isDigit h = h
|
||||
getCalibrationValueLeft (_: t) = getCalibrationValueLeft t
|
||||
|
||||
getCalibrationValueRight :: CalibrationLine -> Char
|
||||
@@ -25,12 +27,12 @@ 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 (h: _) | h >= '0' && h <= '9' = h
|
||||
getCalibrationValueRight (h: _) | isDigit h = h
|
||||
getCalibrationValueRight (_: t) = getCalibrationValueRight t
|
||||
|
||||
getCalibrationValue :: CalibrationLine -> Integer
|
||||
getCalibrationValue line = read [(getCalibrationValueLeft line), (getCalibrationValueRight (reverse line))]
|
||||
getCalibrationValue line = read [getCalibrationValueLeft line, getCalibrationValueRight (reverse line)]
|
||||
|
||||
getCalibrationValues :: CalibrationDocument -> [Integer]
|
||||
getCalibrationValues (h: []) = [(getCalibrationValue h)]
|
||||
getCalibrationValues (h: t) = ((getCalibrationValue h): (getCalibrationValues t))
|
||||
getCalibrationValues [h] = [getCalibrationValue h]
|
||||
getCalibrationValues (h: t) = getCalibrationValue h: getCalibrationValues t
|
||||
|
||||
@@ -6,18 +6,17 @@ import Text.Read
|
||||
|
||||
|
||||
data Round = Round { red :: Int, green :: Int, blue :: Int }
|
||||
|
||||
data Game = Game { gid :: Int, rounds :: [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, ' ', '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]}
|
||||
|
||||
parseRound :: String -> Round
|
||||
parseRound roundRaw = parseCubes (split ", " roundRaw) Round {red = 0, green = 0, blue = 0}
|
||||
|
||||
@@ -4,8 +4,9 @@ import Commons
|
||||
import Part1
|
||||
import Part2
|
||||
|
||||
|
||||
main = do games <- parse
|
||||
let part1Res = sum (Part1.getPossibleIds games)
|
||||
putStrLn (show (part1Res))
|
||||
print part1Res
|
||||
let part2Res = sum (Part2.getPower games)
|
||||
putStrLn (show (part2Res))
|
||||
print part2Res
|
||||
|
||||
@@ -5,13 +5,13 @@ import Commons
|
||||
|
||||
checkRoundsPossible :: [Round] -> Bool
|
||||
checkRoundsPossible [] = True
|
||||
checkRoundsPossible (round: t) = red round <= 12 && green round <= 13 && blue round <= 14 && (checkRoundsPossible t)
|
||||
checkRoundsPossible (round: t) = red round <= 12 && green round <= 13 && blue round <= 14 && checkRoundsPossible t
|
||||
|
||||
checkGamePossible :: Game -> Bool
|
||||
checkGamePossible Game{rounds=rounds} = checkRoundsPossible rounds
|
||||
|
||||
getPossibleIds :: [Game] -> [Int]
|
||||
getPossibleIds [] = []
|
||||
getPossibleIds (game: t) = if (checkGamePossible game)
|
||||
then (gid game: getPossibleIds t)
|
||||
else do getPossibleIds t
|
||||
getPossibleIds (game: t) = if checkGamePossible game
|
||||
then gid game: getPossibleIds t
|
||||
else getPossibleIds t
|
||||
|
||||
@@ -4,15 +4,14 @@ import Commons
|
||||
|
||||
|
||||
getMinCubesRound :: [Round] -> Round
|
||||
getMinCubesRound (round: []) = round
|
||||
getMinCubesRound [round] = round
|
||||
getMinCubesRound (round: otherRound: t) = getMinCubesRound (Round {red = max (red round) (red otherRound),
|
||||
green = max (green round) (green otherRound),
|
||||
blue = max (blue round) (blue otherRound)}: t)
|
||||
|
||||
getPowerGame :: Game -> Int
|
||||
getPowerGame Game{rounds=rounds} = let minRound = getMinCubesRound rounds
|
||||
in (red minRound) * (green minRound) * (blue minRound)
|
||||
in red minRound * green minRound * blue minRound
|
||||
|
||||
getPower :: [Game] -> [Int]
|
||||
getPower [] = []
|
||||
getPower (game: t) = (getPowerGame game: getPower t)
|
||||
getPower = map getPowerGame
|
||||
|
||||
Reference in New Issue
Block a user