Compare commits

...

2 commits

Author SHA1 Message Date
Xiretza 1757609950
day3: add haskell solution 2020-12-03 22:33:03 +01:00
Xiretza 8503335031
day3: add python solution 2020-12-03 20:10:48 +01:00
3 changed files with 44 additions and 0 deletions

View file

@ -8,3 +8,4 @@ https://adventofcode.com/2020/
|---|--------|---------|------|
| 1 | `**` | `**` | |
| 2 | `**` | | `**` |
| 3 | `**` | | |

23
day3/day3.hs Normal file
View file

@ -0,0 +1,23 @@
import Control.Applicative (empty)
import Control.Monad.Zip (mzip)
import Data.List (intercalate)
import Data.Maybe (catMaybes)
compose2 :: (a' -> b' -> c) -> (a -> a') -> (b -> b') -> a -> b -> c
compose2 f g h x y = f (g x) (h y)
(.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
f .: g = (f .) . g
infixl 8 .:
countHits :: (Int, Int) -> [[Bool]] -> Int
countHits = length . filter id . map (uncurry $ flip (!!)) . catMaybes .: compose2 (zipWith mzip) (uncurry . flip $ compose2 intercalate (flip replicate empty . pred) (fmap pure . map pure . enumFromThen 0)) (map pure)
treeCharToBool :: Char -> Bool
treeCharToBool = (== '#')
main = do
charLines <- lines <$> readFile "input.txt"
let boolLines = cycle <$> map treeCharToBool <$> charLines
print $ countHits (3, 1) boolLines
print $ product $ flip countHits boolLines <$> [(1,1), (3,1), (5,1), (7,1), (1,2)]

20
day3/day3.py Executable file
View file

@ -0,0 +1,20 @@
#!/usr/bin/env python
def count_trees(right, down, lines):
n = 0
for i, line in enumerate(lines[::down]):
c = line[(i*right) % len(line)]
if c == '#':
n += 1
return n
if __name__ == '__main__':
with open('input.txt', 'r') as f:
lines = [line.strip() for line in f.readlines()]
print(count_trees(3, 1, lines))
hits_multiplied = 1
for slope in ((1,1), (3,1), (5,1), (7,1), (1,2)):
hits_multiplied *= count_trees(*slope, lines)
print(hits_multiplied)