This commit is contained in:
2023-12-03 13:27:50 +01:00
parent 357c6dd0c3
commit fd46f6dfa2
5 changed files with 135 additions and 0 deletions

35
day03/Part2.hs Normal file
View File

@@ -0,0 +1,35 @@
module Part2 where
import Commons
getNumberNeighbors :: Coordinates -> [NumberPart] -> Int
getNumberNeighbors _ [] = 0
getNumberNeighbors symbolCoordinates (h: t)
| symbolCoordinates `elem` neighbors h = 1 + getNumberNeighbors symbolCoordinates t
| otherwise = getNumberNeighbors symbolCoordinates t
isGear :: SymbolPart -> [NumberPart] -> Bool
isGear symbolPart numberParts | symbol symbolPart == '*' = getNumberNeighbors (coordinates symbolPart) numberParts == 2
| otherwise = False
getRatioNeighbors :: Coordinates -> [NumberPart] -> Int
getRatioNeighbors _ [] = 1
getRatioNeighbors symbolCoordinates (h: t)
| symbolCoordinates `elem` neighbors h = value h * getRatioNeighbors symbolCoordinates t
| otherwise = getRatioNeighbors symbolCoordinates t
getGearRatio :: SymbolPart -> [NumberPart] -> Int
getGearRatio symbolPart numberParts
| isGear symbolPart numberParts = getRatioNeighbors (coordinates symbolPart) numberParts
| otherwise = 0
getGearRatios :: [SymbolPart] -> [NumberPart] -> [Int]
getGearRatios [] _ = []
getGearRatios (h: t) numberParts = let gearRatio = getGearRatio h numberParts
in if gearRatio > 0
then gearRatio: getGearRatios t numberParts
else getGearRatios t numberParts
getGearRatiosFromEngine :: Engine -> [Int]
getGearRatiosFromEngine engine = getGearRatios (symbols engine) (numbers engine)