Play with syntax

This commit is contained in:
2023-12-04 19:05:20 +01:00
parent e746e22b0b
commit 53cad097f8
15 changed files with 27 additions and 26 deletions

View File

@@ -16,4 +16,4 @@ parse = do done <- isEOF
then return [] then return []
else do line <- parseLine else do line <- parseLine
doc <- parse doc <- parse
return (line:doc) return $ line: doc

View File

@@ -6,7 +6,7 @@ import Part2
main = do doc <- parse main = do doc <- parse
let part1Res = sum (Part1.getCalibrationValues doc) let part1Res = sum $ Part1.getCalibrationValues doc
print part1Res print part1Res
let part2Res = sum (Part2.getCalibrationValues doc) let part2Res = sum $ Part2.getCalibrationValues doc
print part2Res print part2Res

View File

@@ -9,7 +9,7 @@ getCalibrationValuePart (h: _) | isDigit h = h
getCalibrationValuePart (_: t) = getCalibrationValuePart t getCalibrationValuePart (_: t) = getCalibrationValuePart t
getCalibrationValue :: CalibrationLine -> Integer getCalibrationValue :: CalibrationLine -> Integer
getCalibrationValue line = read [getCalibrationValuePart line, getCalibrationValuePart (reverse line)] getCalibrationValue line = read [getCalibrationValuePart line, getCalibrationValuePart . reverse $ line]
getCalibrationValues :: CalibrationDocument -> [Integer] getCalibrationValues :: CalibrationDocument -> [Integer]
getCalibrationValues [h] = [getCalibrationValue h] getCalibrationValues [h] = [getCalibrationValue h]

View File

@@ -32,7 +32,7 @@ getCalibrationValueRight (h: _) | isDigit h = h
getCalibrationValueRight (_: t) = getCalibrationValueRight t getCalibrationValueRight (_: t) = getCalibrationValueRight t
getCalibrationValue :: CalibrationLine -> Integer getCalibrationValue :: CalibrationLine -> Integer
getCalibrationValue line = read [getCalibrationValueLeft line, getCalibrationValueRight (reverse line)] getCalibrationValue line = read [getCalibrationValueLeft line, getCalibrationValueRight . reverse $ line]
getCalibrationValues :: CalibrationDocument -> [Integer] getCalibrationValues :: CalibrationDocument -> [Integer]
getCalibrationValues [h] = [getCalibrationValue h] getCalibrationValues [h] = [getCalibrationValue h]

View File

@@ -16,10 +16,10 @@ parseCubes ([h, "green"]: t) round = parseCubes t round {green = read h}
parseCubes ([h, "blue"]: t) round = parseCubes t round {blue = read h} parseCubes ([h, "blue"]: t) round = parseCubes t round {blue = read h}
parseRound :: String -> Round parseRound :: String -> Round
parseRound roundRaw = parseCubes (map words (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 :: String -> String
parseGameId ('G': 'a': 'm': 'e': ' ': t) = parseGameId t parseGameId ('G': 'a': 'm': 'e': ' ': t) = parseGameId t
@@ -28,7 +28,7 @@ parseGameId [] = []
parseGame :: String -> Game parseGame :: String -> Game
parseGame line = let (h: t: _) = split ": " line parseGame line = let (h: t: _) = split ": " line
in Game {gid = read (parseGameId h), rounds = parseRounds t} in Game {gid = read . parseGameId $ h, rounds = parseRounds t}
parse :: IO [Game] parse :: IO [Game]
parse = do done <- isEOF parse = do done <- isEOF
@@ -37,4 +37,4 @@ parse = do done <- isEOF
else do line <- getLine else do line <- getLine
let game = parseGame line let game = parseGame line
otherGames <- parse otherGames <- parse
return (game: otherGames) return $ game: otherGames

View File

@@ -6,7 +6,7 @@ import Part2
main = do games <- parse main = do games <- parse
let part1Res = sum (Part1.getPossibleIds games) let part1Res = sum $ Part1.getPossibleIds games
print part1Res print part1Res
let part2Res = sum (Part2.getPower games) let part2Res = sum $ Part2.getPower games
print part2Res print part2Res

View File

@@ -1,13 +1,14 @@
module Part2 where module Part2 where
import Commons import Commons
import Data.Function (on)
getMinCubesRound :: [Round] -> Round getMinCubesRound :: [Round] -> Round
getMinCubesRound [round] = round getMinCubesRound [round] = round
getMinCubesRound (round: otherRound: t) = getMinCubesRound (Round {red = max (red round) (red otherRound), getMinCubesRound (round: otherRound: t) = getMinCubesRound $ Round {red = (max `on` red) round otherRound,
green = max (green round) (green otherRound), green = (max `on` green) round otherRound,
blue = max (blue round) (blue otherRound)}: t) blue = (max `on` blue) round otherRound}: t
getPowerGame :: Game -> Int getPowerGame :: Game -> Int
getPowerGame Game{rounds=rounds} = let minRound = getMinCubesRound rounds getPowerGame Game{rounds=rounds} = let minRound = getMinCubesRound rounds

View File

@@ -37,7 +37,7 @@ parseNumberPart row column rawNumber = let value = parseNumberPartValue rawNumbe
parseLine :: Int -> Int -> String -> Engine -> Engine parseLine :: Int -> Int -> String -> Engine -> Engine
parseLine _ _ [] engine = engine parseLine _ _ [] engine = engine
parseLine row column (h: t) engine | isDigit h = let newNumberPart = parseNumberPart row column (h: t) parseLine row column (h: t) engine | isDigit h = let newNumberPart = parseNumberPart row column $ h: t
newNumberLength = numberLength newNumberPart newNumberLength = numberLength newNumberPart
in parseLine row (column + newNumberLength) in parseLine row (column + newNumberLength)
(drop (newNumberLength - 1) t) (drop (newNumberLength - 1) t)
@@ -56,7 +56,7 @@ parseEngine row engine = do done <- isEOF
then return engine then return engine
else do line <- getLine else do line <- getLine
let newEngine = parseLine row 1 line engine let newEngine = parseLine row 1 line engine
parseEngine (row+1) newEngine parseEngine (row + 1) newEngine
parse :: IO Engine parse :: IO Engine
parse = parseEngine 1 Engine {numbers = [], symbols = []} parse = parseEngine 1 Engine {numbers = [], symbols = []}

View File

@@ -6,7 +6,7 @@ import qualified Part2
main = do engine <- parse main = do engine <- parse
let part1Res = sum (Part1.getActualPartValuesNumbersFromEngine engine) let part1Res = sum $ Part1.getActualPartValuesNumbersFromEngine engine
print part1Res print part1Res
let part2Res = sum (Part2.getGearRatiosFromEngine engine) let part2Res = sum $ Part2.getGearRatiosFromEngine engine
print part2Res print part2Res

View File

@@ -15,4 +15,4 @@ getActualPartNumbers (h: t) symbolsCoordinates
getActualPartValuesNumbersFromEngine :: Engine -> [Int] getActualPartValuesNumbersFromEngine :: Engine -> [Int]
getActualPartValuesNumbersFromEngine engine = getActualPartValuesNumbersFromEngine engine =
map value (getActualPartNumbers (numbers engine) (map coordinates (symbols engine))) map value $ getActualPartNumbers (numbers engine) $ map coordinates $ symbols engine

View File

@@ -32,4 +32,4 @@ getGearRatios (h: t) numberParts = let gearRatio = getGearRatio h numberParts
else getGearRatios t numberParts else getGearRatios t numberParts
getGearRatiosFromEngine :: Engine -> [Int] getGearRatiosFromEngine :: Engine -> [Int]
getGearRatiosFromEngine engine = getGearRatios (symbols engine) (numbers engine) getGearRatiosFromEngine engine = getGearRatios (symbols engine) $ numbers engine

View File

@@ -25,4 +25,4 @@ parse = do done <- isEOF
else do line <- getLine else do line <- getLine
let card = parseCard line let card = parseCard line
otherCards <- parse otherCards <- parse
return (card: otherCards) return $ card: otherCards

View File

@@ -6,7 +6,7 @@ import qualified Part2
main = do cards <- parse main = do cards <- parse
let part1Res = sum (Part1.getAllPoints cards) let part1Res = sum $ Part1.getAllPoints cards
print part1Res print part1Res
let part2Res = sum (Part2.getNCopies cards) let part2Res = sum $ Part2.getNCopies cards
print part2Res print part2Res

View File

@@ -9,7 +9,7 @@ getNWinningNumbers winningNumbers (n: t) | n `elem` winningNumbers = 1 + getNWin
| otherwise = getNWinningNumbers winningNumbers t | otherwise = getNWinningNumbers winningNumbers t
getPoints :: Card -> Int getPoints :: Card -> Int
getPoints card = let nWinningNumbers = getNWinningNumbers (winningNumbers card) (numbers card) getPoints card = let nWinningNumbers = getNWinningNumbers (winningNumbers card) $ numbers card
in if nWinningNumbers == 0 in if nWinningNumbers == 0
then 0 then 0
else 2 ^ (nWinningNumbers - 1) else 2 ^ (nWinningNumbers - 1)

View File

@@ -13,7 +13,7 @@ updateNCopies 0 card otherCards = otherCards
updateNCopies n card (c: t) = c {nCopies = nCopies card + nCopies c}: updateNCopies (n - 1) card t updateNCopies n card (c: t) = c {nCopies = nCopies card + nCopies c}: updateNCopies (n - 1) card t
updateCopies :: Card -> [Card] -> [Card] updateCopies :: Card -> [Card] -> [Card]
updateCopies card otherCards = let nWinningNumbers = getNWinningNumbers (winningNumbers card) (numbers card) updateCopies card otherCards = let nWinningNumbers = getNWinningNumbers (winningNumbers card) $ numbers card
in updateNCopies nWinningNumbers card otherCards in updateNCopies nWinningNumbers card otherCards
updateAllCopies :: [Card] -> [Card] updateAllCopies :: [Card] -> [Card]
@@ -21,4 +21,4 @@ updateAllCopies [] = []
updateAllCopies (c: t) = c: updateAllCopies (updateCopies c t) updateAllCopies (c: t) = c: updateAllCopies (updateCopies c t)
getNCopies :: [Card] -> [Int] getNCopies :: [Card] -> [Int]
getNCopies cards = map nCopies (updateAllCopies cards) getNCopies cards = map nCopies $ updateAllCopies cards