mirror of
https://github.com/mx42/adventofcode.git
synced 2026-01-14 22:09:50 +01:00
Merge AoC 2019 & 2020 Haskell codebases
This commit is contained in:
22
haskellAoC/src/Y2020/Day03.hs
Normal file
22
haskellAoC/src/Y2020/Day03.hs
Normal file
@@ -0,0 +1,22 @@
|
||||
module Y2020.Day03 (y20day03) where
|
||||
|
||||
walkMap :: (Int, Int) -> (Int, Int) -> [(Int, Int)]
|
||||
walkMap (w, h) (wstep, hstep) = zip wCoords hCoords
|
||||
where wCoords = map (`mod` w) $ take h $ iterate (+ wstep) 0
|
||||
hCoords = take h $ iterate (+ hstep) 0
|
||||
|
||||
getCharAtPos :: [String] -> (Int, Int) -> Char
|
||||
getCharAtPos myMap (w, h)
|
||||
| h <= length myMap = (myMap !! h) !! w
|
||||
| otherwise = '\0'
|
||||
|
||||
getNumberOfTrees :: [String] -> [(Int, Int)] -> Int
|
||||
getNumberOfTrees myMap coords = length $ filter (== '#') $ map (getCharAtPos myMap) coords
|
||||
|
||||
y20day03 :: [String] -> (String, String)
|
||||
y20day03 input = (part1, part2)
|
||||
where width = length $ input !! 0
|
||||
height = (length $ input)
|
||||
getNb slope = getNumberOfTrees input $ walkMap (width, height) slope
|
||||
part1 = show $ getNb (3, 1)
|
||||
part2 = show $ product $ map getNb [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
|
||||
Reference in New Issue
Block a user