From eedd910d6793ed7c7b145ea5f1db5312411ad661 Mon Sep 17 00:00:00 2001 From: RhiobeT Date: Sat, 2 Dec 2023 16:16:57 +0100 Subject: [PATCH] It took me the afternoon but I got a LSP working --- day01/Commons.hs | 6 +++--- day01/Main.hs | 5 +++-- day01/Part1.hs | 10 ++++++---- day01/Part2.hs | 12 +++++++----- day02/Commons.hs | 13 ++++++------- day02/Main.hs | 5 +++-- day02/Part1.hs | 8 ++++---- day02/Part2.hs | 7 +++---- 8 files changed, 35 insertions(+), 31 deletions(-) diff --git a/day01/Commons.hs b/day01/Commons.hs index e9b63c9..2a5ae39 100644 --- a/day01/Commons.hs +++ b/day01/Commons.hs @@ -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 diff --git a/day01/Main.hs b/day01/Main.hs index af9e660..3a15b8d 100644 --- a/day01/Main.hs +++ b/day01/Main.hs @@ -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 diff --git a/day01/Part1.hs b/day01/Part1.hs index d45008b..a749519 100644 --- a/day01/Part1.hs +++ b/day01/Part1.hs @@ -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 diff --git a/day01/Part2.hs b/day01/Part2.hs index d03c572..3ad0b4a 100644 --- a/day01/Part2.hs +++ b/day01/Part2.hs @@ -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 diff --git a/day02/Commons.hs b/day02/Commons.hs index bbcaf0c..4965eca 100644 --- a/day02/Commons.hs +++ b/day02/Commons.hs @@ -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} diff --git a/day02/Main.hs b/day02/Main.hs index aa9c2e6..9fc049e 100644 --- a/day02/Main.hs +++ b/day02/Main.hs @@ -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 diff --git a/day02/Part1.hs b/day02/Part1.hs index 1873614..05dab6a 100644 --- a/day02/Part1.hs +++ b/day02/Part1.hs @@ -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 diff --git a/day02/Part2.hs b/day02/Part2.hs index 78505ce..f050c03 100644 --- a/day02/Part2.hs +++ b/day02/Part2.hs @@ -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