Done, but buggy

This commit is contained in:
Martin Potier 2020-12-03 22:11:19 +02:00
parent 40858ab68e
commit fff23228a4

View file

@ -43,11 +43,12 @@ line a b = Position 0 0 : [ Position (x*a) (x*b) | x <- [1..] ]
-- SquareSortEmpty or SquareSortTree
data SquareSort = SquareSortTree | SquareSortEmpty
deriving Eq
instance Show SquareSort
where
show SquareSortTree = "#"
show SquareSortEmpty = "."
show SquareSortEmpty = "_"
char2ss :: Char -> Maybe SquareSort
char2ss '#' = Just SquareSortTree
@ -61,7 +62,7 @@ newtype Grid = Grid [[SquareSort]]
instance Show (Grid)
where
show (Grid x) = intersperse '\n' $ map show x
show (Grid x) = concat $ intersperse "\n" $ map show x
parseInput :: [String] -> Maybe Grid
parseInput x = Grid <$> sequence ( (map str2sss) x )
@ -72,10 +73,48 @@ getSquareSortAtPosition (Grid grid) (Position x y) =
where
n = length (grid !! 0)
solveDay3Part1 :: Grid -> (Int,Int) -> Int
solveDay3Part1 grid@(Grid lx) (x',y')=
length $ filter (== SquareSortTree) $ map (getSquareSortAtPosition grid) px
where
px = take n $ line x' y'
n = (length lx) `div` y'
-- Determine the number of trees you would encounter if, for each of the
-- following slopes, you start at the top-left corner and traverse the map all
-- the way to the bottom:
--
-- - Right 1, down 1.
-- - Right 3, down 1. (This is the slope you already checked.)
-- - Right 5, down 1.
-- - Right 7, down 1.
-- - Right 1, down 2.
--
-- What do you get if you multiply together the number of trees encountered on
-- each of the listed slopes?
solveDay3Part2 :: Grid -> Int
solveDay3Part2 grid =
foldl (*) 1 [ solveDay3Part1 grid (1,1)
, solveDay3Part1 grid (3,1)
, solveDay3Part1 grid (5,1)
, solveDay3Part1 grid (7,1)
, solveDay3Part1 grid (1,2)
]
main :: IO ()
main = do
putStrLn "Day 3 - Part 1"
inputData <- readFile "day3/input"
print $ take 5 $ line 3 1
print $ take 5 $ line 7 1
let (Just parsedTestData) = parseInput testData
print parsedTestData
print $ getSquareSortAtPosition parsedTestData (Position 3 0)
print $ getSquareSortAtPosition parsedTestData (Position 10 10)
let (Just parsedInputData) = parseInput (lines inputData)
putStrLn "Part 1:"
print $ solveDay3Part1 parsedTestData (3,1)
print $ solveDay3Part1 parsedInputData (3,1)
putStrLn "Part 2:"
print $ solveDay3Part2 parsedTestData -- This gives the right answer, but
print $ solveDay3Part2 parsedInputData -- This gives the wrong answer :s