From dd2bcc94a5d216a417772cd5207156649afe0158 Mon Sep 17 00:00:00 2001 From: Thorn Avery Date: Thu, 3 Dec 2020 18:34:36 +1300 Subject: [PATCH] day 3 --- day3.hs | 40 ++++++++ day3.txt | 323 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 363 insertions(+) create mode 100644 day3.hs create mode 100644 day3.txt diff --git a/day3.hs b/day3.hs new file mode 100644 index 0000000..3be1c18 --- /dev/null +++ b/day3.hs @@ -0,0 +1,40 @@ +main :: IO () +main = do + raw <- readFile "day3.txt" + let ls = map parse $ lines raw + ansA = solveSlope 3 1 ls + ansB = foldr (*) 1 $ + [ (solveSlope 1 1 ls) + , (solveSlope 3 1 ls) + , (solveSlope 5 1 ls) + , (solveSlope 7 1 ls) + , (solveSlope 1 2 ls) + ] + in do + putStrLn $ "day3a: " ++ (show ansA) + putStrLn $ "day3b: " ++ (show ansB) + +parse :: String -> [Bool] +parse = map (== '#') + +solveLine :: [Bool] -> Int -> Bool +solveLine [] _ = error "empty list" +solveLine bs i = bs !! index + where + index = i `mod` (length bs) + +solveSlope :: Int -> Int -> [[Bool]] -> Int +solveSlope r d bs = + length $ filter (== True) $ solveSlope' r d bs 0 [] + +solveSlope' :: Int -> Int -> [[Bool]] -> Int -> [Bool] -> [Bool] +solveSlope' _ _ [] _ as = as +solveSlope' r d bs@(b:cs) i as = + if ns == [] + then as + else solveSlope' r d ns ni $ (solveLine (head ns) ni) : as + where + ni = (i + r) `mod` (length b) + ns = if (length bs) <= d + then [] + else (drop d bs) diff --git a/day3.txt b/day3.txt new file mode 100644 index 0000000..13fac46 --- /dev/null +++ b/day3.txt @@ -0,0 +1,323 @@ +..#..#......#..#.......#...#.#. +...##.....##..#..#....#.##.##.# +...#...#.##...##.....#.....#.#. +..#....#.....#...##.##.###.#... +.#.....#......#..#.##.#...###.. +#..#..#.#......#...........###. +#......####.#....##........##.. +.#.......#.....#......#...#.... +...#...#.....#.......##.....##. +#...##.........#.#..##..#..#.## +#.#.##.........#.#..#.#.....### +.##..##...#....##.....#..#..... +........#.......###.#.#.....#.# +...#.#....#.##..#...##..##..#.. +......#....#........######.#... +.##...#.#...###......#.#.#..#.# +........#.##...##.#...#..#...## +.#..#.#..##....###..#.#.......# +..#..##..#.#...#.##......#..... +##.....#..##.#.#..#......##...# +......................#..#..#.. +..#.##....####.........###.##.. +##..###..#...#....#..#.#...#... +.##.#......#..#....#........#.. +.#.....#..#..#.#.#....#.....##. +..........#..#....#..##...#..## +.#...#.#....#.##..#.....#....#. +#..............#.#.#..#..#....# +...#.#...............##........ +#.#.##...#.##..##.....#........ +...#.......###..###..#...#..#.. +####..#.#..##.....##.#.#......# +.#.#.......#..##.......#....... +#....#...#.##.#.......#..#..... +.#...##..#..#..##.......##...#. +.#..#......#.........#......... +#.##.#.....#....#..##..#.....#. +#.#....#.#....#...#.#..#....#.. +#..#.....#.##..#.....#...##...# +#....#...##.#.........#.#....## +.......##.##......##.......##.. +#.....#..#........#........#... +#....#.#..#.#........##.#...#.. +#.......#.#.#.#....#.......##.# +...#..###..........#...#.#.###. +....#..#....#...#....##.#.....# +.#..##.....#..#....##..##...#.# +#.........#....#.#..###...##... +.#.#.........#.#.......#.#.#..# +..........#........##..#....... +.....#.......#...#.....#..##.## +...#.........#.............#### +##..#...#..#.#......#...#...... +.#..###...#.#.#.#...#...#...... +....#..##.#....#..#.#..##..##.# +..#.......#......#..#.......#.. +....###......#...##...#....#... +..#..#.....#...#..###....#.#..# +.........##..#.##....#..##..#.. +##...#...#.#.........##......#. +###..#.#....#......##..##.#...# +.##...##..#.#.#.#......#..#.... +###......#..#..#.....#..#....#. +.#.#..##....##........##..#.#.. +###...####.#....#.......###.... +..#....###..#.#.#..#.......##.. +.......#.#...#.....#.#....##.#. +......#......#.#....#..##..###. +....####..........#.....#...... +.###.....#...#..#...##.#...###. +...##....##....###....#.#..#.#. +##.#..........##.........#.##.. +..#..#.#.###..##..#....##.....# +..#....##.....#...##....###..## +....#.......##..#..#..........# +............#..#.###..#.#...... +...........##......#.#.#...#..# +...##.##....#...##.##.....#.#.. +.####...#....###...#.....#....# +.##........#..##..#.#.....#.... +..................#.....#..##.. +..###.....#.##..#..#....##...#. +...#.##.#.####.#.###.#....#..## +.#....##..##......####.#####... +#...#.#....##.........##....#.. +..#.##.....##.............#.##. +###.....#.#..#..#......#.##.#.. +...#..##.....#...##...#......#. +.##.#...#......##.#..##....#... +.....##.....#......#.#......... +#.....#.....#........##........ +.#......##...#..#.#....###.#..# +#.####...#....#.........#..#... +#..##.#.....#.##.##.#....#...#. +#########..#....#..#...#......# +..##..##...###.######...##.##.. +##.......#.......#.#....###..#. +.....#...#.######..#.....#..... +.#......#..#.............#.##.# +..###.#.#......##...###........ +.......####.#..##....#........# +..#......#.##....##.##....#.... +....#......#.#....#..#.#.....## +####.....#....#.#......#.#.#.## +#...##....#.#.##.........#....# +....#..###......#......#...#... +.....##.#..#..#...#..#.#.#..... +.##............#.....#......... +##...#..#.....##.#..#.......... +#.....#####.......#..#....#.#.. +.........#..#.....###........#. +#....#..#...###........#..#.#.. +...##...#..#...#.##..#......... +.........#.#.....#.......#...#. +.#.....#..####....#.##.......## +...............##....##.##..##. +............#....#....#...##.#. +..#...#........#.......#..#.... +##....####....#.##...#..##..#.# +.#.#.....#......#.#........#.#. +....#......#.#....##..##....... +.#..#.#..#..##.....#........... +..#........#.##..#......##..#.. +...##.#...#...#..#........#.... +##..##....#......#...#..#.#.#.# +......#.....#..#..#....#....... +.....##......#..#.#.##...#..... +...#.....#.#..........#..##...# +.####.##....#...........#.....# +.....###..##...#....##..#...#.. +..##...#.#...#..........#..#.#. +...#..#..............#.##.#.... +##.#....#...#..#....#.......... +.##..........#..#........#..... +#...#.#......#...#.....##..#... +.##...#.#.#....###.####..#....# +.#......#.#...#.#....#.#...#... +#....##.###.............#.#.... +....#.###..##..##.##...##...... +##....#..###.##.##.....#......# +..#..#..#......#..#..#......... +#.##......#.#....#..#..#....... +....#.#...#..###......##....... +.###.......##.......#....###... +..#..#.##..#.#....#..#.#.....#. +.#..##.##..............#....#.. +#...#.#...#..#.##..##.#.#...... +#...#..#..##..##.###......#.... +.#..#.....#...#....#.....#...#. +.....#....#..#.....###...#.#### +.#.....#......#...##...#..#.... +.#......#............#.#....... +....##....#.#..#..#...#..#.#... +#...#.....###...##...#.##.....# +.......#.....#....#.......#...# +#.......###.......#.#.......... +...#.#.###.#........#.###...#.. +....#............#....#..#..... +#......#.##.#...##.......#..#.# +.....#....#....#.#.#...###..#.. +.....#.#...#...#.#..#....#.#..# +.#.......#.#..#...###.......##. +.......#..#.##.........#....... +.##.#........#.##...##....#.... +.#....#..#...#......####...#..# +...#.....#..##.#..#.#....#....# +...##....#........#.#........#. +.....#....##..#.##..........#.. +#.....#.#.#......##....##.#..#. +.#.#.##..#.#....##.#....##..... +.....#.....#..#.#....#..#....## +...#........#....#......###.#.. +.....##...#.....##.##.#.#.##... +...#.....#####....##.#.#.###.#. +.#..#.#..##...###.........#.#.# +#...#...#.#..#...#...........#. +.##..............#...#..#....#. +....###.........#.#.#....#..... +..#...##.#.#....##.#..#...#..#. +..#.....#.#......#....#......#. +.......##....#.#.##....#...#..# +##.#.#...#..#......#..#..#....# +...#.#......#............###.## +..###..#..##..#...##........#.. +.#...#...##...#....#....##.#..# +..##...####....#....#..#....#.# +...#......##....#.........##.#. +##.#.......#..#..#............. +..#.#.#.#......#...#.#..##..... +.#..##.....###...##.#..#......# +##...#..........#.####....##... +#..........#...#..##....#...... +....##...#....#..####...#.##.## +.#.######...##...#..##......... +....##.........#.......##.##... +.#.....#.#..........##......#.. +...#..#.#.###..#........#.....# +..##..#............##.......#.. +......##....##..#.##..#.......# +.......##....#.......#..#...#.# +#.#......#.###.....#.##........ +.#..##..........#..#.....#.##.. +..#.#...#....#.........#..##..# +.#......#.......#...#..#..###.. +......#.##.....#.#......#....#. +....#....#...#.......#...##.##. +#....#...##...#..##........###. +##......#.#..#.......#.......#. +...##.##..#......#.###..#.#.##. +.............#..#.............# +..#.......##..#..#....##...#... +...............##..##........#. +##...#.##.......#....#.......## +....##.##.#.#.....##.....##.##. +#.#......#.......#..#.#..#..... +....##....#.##........##.##.#.. +......##....#..##..#..##....##. +.............#.....#.......#... +.......###.......#..........#.. +......##.#..#.....#.#...#.#...# +.#...#..#..###.###...#....##... +#......#..#.#...#...#.....#..## +.###.....#..#.#......##..#.##.. +.##.#.....#..#.#..#....##...... +#......#..............#.....#.# +...#..#....#.....#.....##.#...# +......#..##..##.....#...#...... +.....####..#..#.##.......#..#.# +###.#.#........#.......#.....## +..#.#.#.#...#...#........#....# +....##.#.#..#...##.....#......# +#..#.##....#..#.##..####....... +...####.#...#......#......##..# +#....#.#..###......#..#..##..#. +...........#....#...#......#... +......###.#.....#.#....#.#...#. +.......#.##..............#..##. +..##...........#..#.#...#.....# +#..#............##.........#.#. +.......###.#...#.#...#.#.#...#. +..#...##.......#..#......#.#.## +#.#...#.....#...##.#.#.......## +.#.#.##...#..##.#......#....... +#.......#.......#.#....#.....#. +.....#..#..#.......#..#........ +##...##...##......#..##.###.... +..#...#.###.#.###..#.....###... +.....####.......#.#.....##....# +....#....#.#....#...#..#.#..#.. +..##.....#....#.#.#.###...#.... +......#.#....#.#..#....#.#..#.. +#...#...#....#.......#......#.# +#..#.#......#..#...........#.## +...............#....#.....#...# +.#.#.#...#.##...#.#.#..#....#.. +...#.#.####..##.#...##......... +##.........##.##.....##....#... +................#...#.##.#.#..# +.#..#....#...#..#..#..###.#..#. +...#..#.##.#.####..........#..# +........#....##......#..#.#.... +........##.........#..#..#..#.# +#......#.#...#...#...##.....#.. +#...#.....#..#..##.#...#.#.#... +....#..##...##.....#...#.#..... +..#..##....#....#...#....#..... +.#..#...##.......###...#...#... +.#......#......#..##..#..##.... +....##....#..#.#....#.#..##.... +###......#...........#.....###. +.....#...#..##.#..#..#.....#..# +#.#....#...........#.##..#..### +#....#...###.#...#..##..#.....# +.#....#......##.#..#....#.#.... +....#.#....#..#.#....#..#..#... +..#......#..#.#.#....#......... +.#...#.#.....#........#.#...### +....#..##.......#.###....##.... +#.#.......#......#.###........# +#.........#.....####.##..#..#.. +.#.#..##...#.#.....##.#.#..#... +.#..#..#..#.##..#...###.#...#.. +.....##..##..##..#.#.#.....###. +.#..#...#..#......##.#......... +....#..##....#.##.........#...# +........#...#...###.........##. +#.........#..##....#.#...#..... +.......#.......#..#.......#.... +#......##......#.#.##.......... +.#..##..####...#.....####.....# +........#.#....#..##..###.#...# +.#...#...#.........###..#...#.# +#.........#....##...#.......... +.#.#....#..........#........... +.#.#..........#.##.....#.##.... +..#....#...##..###..........##. +.#.#..#.##..#..#.##.##..##..... +........#...#....#...#.#..##... +......#......##..#..#.....#..#. +.##.#....#...#....#...#..##..## +##............#..........###... +....#.......#.#..#.#####.....#. +#......#.....#...#........#.... +..##.....###..#.#.#.#....#....# +#...#...#.#..#..#....#..#...... +......#....#...#..#....#####... +....#.......##....#....##...... +.....##...#.##.#.....##....#... +.#....###.#..##...##.##.......# +....#.#.#.##.............#..##. +...........##......#...#.#.##.. +....##......#....#....##..##.#. +.#.#...#.....##.....#.........# +#.#..........#.......#.##...#.. +....#.##..#.#....#.....#...#... +##.............##.......#.##.#. +....#...#.....##...#..........# +##..#...#...#.#.##...#.......## +..#........#.....###...##..##.# +.....#...##.#.#.##.....#...#... +####.###...##..##...#..#..#..## +......#..#..#.........#...#.#.. +....###.....##.##....#.##.....#