It took me the afternoon but I got a LSP working

This commit is contained in:
2023-12-02 16:16:57 +01:00
parent a36a4ccbe9
commit eedd910d67
8 changed files with 35 additions and 31 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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