Play with syntax
This commit is contained in:
@@ -16,4 +16,4 @@ parse = do done <- isEOF
|
||||
then return []
|
||||
else do line <- parseLine
|
||||
doc <- parse
|
||||
return (line:doc)
|
||||
return $ line: doc
|
||||
|
||||
@@ -6,7 +6,7 @@ import Part2
|
||||
|
||||
|
||||
main = do doc <- parse
|
||||
let part1Res = sum (Part1.getCalibrationValues doc)
|
||||
let part1Res = sum $ Part1.getCalibrationValues doc
|
||||
print part1Res
|
||||
let part2Res = sum (Part2.getCalibrationValues doc)
|
||||
let part2Res = sum $ Part2.getCalibrationValues doc
|
||||
print part2Res
|
||||
|
||||
@@ -9,7 +9,7 @@ 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]
|
||||
|
||||
@@ -32,7 +32,7 @@ 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]
|
||||
|
||||
@@ -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}
|
||||
|
||||
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 roundsRaw = map parseRound (split "; " roundsRaw)
|
||||
parseRounds roundsRaw = map parseRound $ split "; " roundsRaw
|
||||
|
||||
parseGameId :: String -> String
|
||||
parseGameId ('G': 'a': 'm': 'e': ' ': t) = parseGameId t
|
||||
@@ -28,7 +28,7 @@ parseGameId [] = []
|
||||
|
||||
parseGame :: String -> Game
|
||||
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 = do done <- isEOF
|
||||
@@ -37,4 +37,4 @@ parse = do done <- isEOF
|
||||
else do line <- getLine
|
||||
let game = parseGame line
|
||||
otherGames <- parse
|
||||
return (game: otherGames)
|
||||
return $ game: otherGames
|
||||
|
||||
@@ -6,7 +6,7 @@ import Part2
|
||||
|
||||
|
||||
main = do games <- parse
|
||||
let part1Res = sum (Part1.getPossibleIds games)
|
||||
let part1Res = sum $ Part1.getPossibleIds games
|
||||
print part1Res
|
||||
let part2Res = sum (Part2.getPower games)
|
||||
let part2Res = sum $ Part2.getPower games
|
||||
print part2Res
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
module Part2 where
|
||||
|
||||
import Commons
|
||||
import Data.Function (on)
|
||||
|
||||
|
||||
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)
|
||||
getMinCubesRound (round: otherRound: t) = getMinCubesRound $ Round {red = (max `on` red) round otherRound,
|
||||
green = (max `on` green) round otherRound,
|
||||
blue = (max `on` blue) round otherRound}: t
|
||||
|
||||
getPowerGame :: Game -> Int
|
||||
getPowerGame Game{rounds=rounds} = let minRound = getMinCubesRound rounds
|
||||
|
||||
@@ -37,7 +37,7 @@ parseNumberPart row column rawNumber = let value = parseNumberPartValue rawNumbe
|
||||
|
||||
parseLine :: Int -> Int -> String -> 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
|
||||
in parseLine row (column + newNumberLength)
|
||||
(drop (newNumberLength - 1) t)
|
||||
@@ -56,7 +56,7 @@ parseEngine row engine = do done <- isEOF
|
||||
then return engine
|
||||
else do line <- getLine
|
||||
let newEngine = parseLine row 1 line engine
|
||||
parseEngine (row+1) newEngine
|
||||
parseEngine (row + 1) newEngine
|
||||
|
||||
parse :: IO Engine
|
||||
parse = parseEngine 1 Engine {numbers = [], symbols = []}
|
||||
|
||||
@@ -6,7 +6,7 @@ import qualified Part2
|
||||
|
||||
|
||||
main = do engine <- parse
|
||||
let part1Res = sum (Part1.getActualPartValuesNumbersFromEngine engine)
|
||||
let part1Res = sum $ Part1.getActualPartValuesNumbersFromEngine engine
|
||||
print part1Res
|
||||
let part2Res = sum (Part2.getGearRatiosFromEngine engine)
|
||||
let part2Res = sum $ Part2.getGearRatiosFromEngine engine
|
||||
print part2Res
|
||||
|
||||
@@ -15,4 +15,4 @@ getActualPartNumbers (h: t) symbolsCoordinates
|
||||
|
||||
getActualPartValuesNumbersFromEngine :: Engine -> [Int]
|
||||
getActualPartValuesNumbersFromEngine engine =
|
||||
map value (getActualPartNumbers (numbers engine) (map coordinates (symbols engine)))
|
||||
map value $ getActualPartNumbers (numbers engine) $ map coordinates $ symbols engine
|
||||
|
||||
@@ -32,4 +32,4 @@ getGearRatios (h: t) numberParts = let gearRatio = getGearRatio h numberParts
|
||||
else getGearRatios t numberParts
|
||||
|
||||
getGearRatiosFromEngine :: Engine -> [Int]
|
||||
getGearRatiosFromEngine engine = getGearRatios (symbols engine) (numbers engine)
|
||||
getGearRatiosFromEngine engine = getGearRatios (symbols engine) $ numbers engine
|
||||
|
||||
@@ -25,4 +25,4 @@ parse = do done <- isEOF
|
||||
else do line <- getLine
|
||||
let card = parseCard line
|
||||
otherCards <- parse
|
||||
return (card: otherCards)
|
||||
return $ card: otherCards
|
||||
|
||||
@@ -6,7 +6,7 @@ import qualified Part2
|
||||
|
||||
|
||||
main = do cards <- parse
|
||||
let part1Res = sum (Part1.getAllPoints cards)
|
||||
let part1Res = sum $ Part1.getAllPoints cards
|
||||
print part1Res
|
||||
let part2Res = sum (Part2.getNCopies cards)
|
||||
let part2Res = sum $ Part2.getNCopies cards
|
||||
print part2Res
|
||||
|
||||
@@ -9,7 +9,7 @@ getNWinningNumbers winningNumbers (n: t) | n `elem` winningNumbers = 1 + getNWin
|
||||
| otherwise = getNWinningNumbers winningNumbers t
|
||||
|
||||
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
|
||||
then 0
|
||||
else 2 ^ (nWinningNumbers - 1)
|
||||
|
||||
@@ -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
|
||||
|
||||
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
|
||||
|
||||
updateAllCopies :: [Card] -> [Card]
|
||||
@@ -21,4 +21,4 @@ updateAllCopies [] = []
|
||||
updateAllCopies (c: t) = c: updateAllCopies (updateCopies c t)
|
||||
|
||||
getNCopies :: [Card] -> [Int]
|
||||
getNCopies cards = map nCopies (updateAllCopies cards)
|
||||
getNCopies cards = map nCopies $ updateAllCopies cards
|
||||
|
||||
Reference in New Issue
Block a user