From 6b010191936487a6ba057f950c6646dae82f6b0b Mon Sep 17 00:00:00 2001 From: Sloane Perrault Date: Wed, 21 Sep 2022 09:19:52 -0400 Subject: [PATCH] solve: day 3, part 1 --- 2020/03/README.ms | 5 + 2020/03/input.txt | 323 ++++++++++++++++++++++++++++++++++++++++++++++ 2020/03/part1.hs | 44 +++++++ 2020/03/small.txt | 11 ++ 2020/README.md | 3 +- 5 files changed, 385 insertions(+), 1 deletion(-) create mode 100644 2020/03/README.ms create mode 100644 2020/03/input.txt create mode 100755 2020/03/part1.hs create mode 100644 2020/03/small.txt diff --git a/2020/03/README.ms b/2020/03/README.ms new file mode 100644 index 0000000..5b3860f --- /dev/null +++ b/2020/03/README.ms @@ -0,0 +1,5 @@ +# Day 3 + +## Part 1 + +Advent of Code _loves_ some cycles... diff --git a/2020/03/input.txt b/2020/03/input.txt new file mode 100644 index 0000000..a8bdfa9 --- /dev/null +++ b/2020/03/input.txt @@ -0,0 +1,323 @@ +......#...........#...#........ +.#.....#...##.......#.....##... +......#.#....#................. +..............#.#.......#...... +.....#.#...##...#.#..#..#..#..# +.......##...#..#...........#... +.......#.##.#...#.#.........#.. +..#...##............##......#.# +.......#.......##......##.##.#. +...#...#........#....#........# +#............###.#......#.....# +..#........#....#..#..........# +..#..##....#......#..#......#.. +........#......#......#..#..#.. +..#...#....#..##.......#.#..... +.....#.#......#..#....#.##.#..# +......###.....#..#..........#.. +.#................#.#.......... +.........#..#...#......##...... +##...#....#...#.#...#.##..#.... +...##...#....#.........###..... +.#.#....#.........##........... +....#.#..#..#...........#...... +..#..#.#....#....#...#......... +..........##.....#.##.......... +..#.#....#..##......#.#.....##. +..#...#.##......#..........#... +......#....#..#.....#.....#...# +#.#...##.#.##.........#..#..... +...#.#.#.........#.....#.#.#... +..#.........#...............#.. +#..##.....#.........#....#..... +...#....##..##...........##..#. +......##.................#.#... +##.......#....#.#.#.....#...... +....#.#...#.................##. +#...#.........##.....#......... +#....#.###..#.....##.#....#.... +#..#....#...#....#.#.#......... +.......#...........#....#.....# +#...#.............#........#... +.......#.....#...#..#.........# +.##.....##.....##.......#...... +....##...##.......#..#.#.....#. +.##.........#......#........##. +.......#...#...###.#..#........ +..#..###......##..##........... +.#..#......##..#.#.........#... +...#.......#........#...#.#.... +...#....#..#....#.....##....... +............#......#..........# +.#.......#......#.#....#..#.#.. +##.........#.#.#..........#.... +....##.....#................... +.......#..#........#........... +....##.#..#......###.......#... +....#....#...#.#......#...#...# +.......#.....##..#....#...#.... +#...#........#.........#..##... +...........##.........#.#...#.. +....................#....#.##.. +.#..#..#.........#....#..#..##. +......................#........ +..###....#.......#.....###.##.. +......#......#.......#.....#..# +.....#...#.##...#......#....#.. +.....#.....##.............#.... +....#......##..#....#.......#.. +.##....#..##......###....#..#.. +...###.#.............##...#.#.. +.....#.....#.....#...#..#.#.... +..#.#.....###......#.......#... +..........#.##......#.........# +..##..#.......................# +........#......#............#.. +#..#..#..#.#......#..#....#.... +...##......#.............#....# +...........#..#..##.......#.... +.....#.........#.#..#.......... +##...#.......#.#....#..#..#.... +#.#.#...........#.##.#.#..###.. +#..#...........#.........##.... +............#.#..............#. +.#....#....##.#...........#..#. +....#...#..#...#....#....#..... +....#....#...#..#......#....... +.#.#.........#.......#.##...... +.#..##...#........#...........# +##...#..#...#...#.....#...#.... +....###.#..#.......##.#..#...#. +...##.......####...##.#........ +#....#....#.#............#..#.. +#.#.#...#...................##. +##......#...........#.......... +#..#..#....#.#...#......#...... +.##...#.....#...#........#..... +..#............#..............# +###........#..#....#...#......# +###..##......#.##...........#.. +........#......#..#.....#...... +...#..........#..#...........#. +....#..#..#....#........#....#. +.#.................#####..##..# +.....#...##..#..........#.##... +..#..............#...####...... +.....#.##..................#.#. +...#.#..#..#........#.......... +...........#....#.#..#......... +.....##.......#......#..#.#.#.. +...#.............##...#........ +...............#.......##.##.## +.....#........#........#.#..#.. +...#..#.........#...##...###... +...#.#.............###.#.....#. +.#..........#......###.#.#..... +....##..##.............###..... +..#..#.#...##...#.......##..... +..........###........#.....#.#. +#.#....#..#..#......#...#...#.. +.........#......##.......#.#..# +...#.....#.........##..#..#.... +.....##.#..##.##..##........... +...#.#.##....#..#..#......#..#. +#....#....#.............#...##. +#......#..#.####.#.##.#....##.. +##.#.#....##..................# +.....##......#.......##.......# +..#......#.#..#...##......##... +..#....##....#.........#..##... +.###.....#....##...........#... +.........#......#.#........#... +...#...#..#.#....######.#..#... +###......#.#.#.........##.#.... +.....#...#.........#...#....... +....#.............#.#.........# +..##...#...#.......#......#.... +.....#...#.#...#...#..#........ +.#......#...................... +...###..#..#....#...##.#....... +.#.#.....##...#...#.....#...##. +.....###..###....##............ +.....##....#..#.....#.##....... +#........#.........#...#..#.... +...#.#.........#..#.......#.#.. +....#.#....##.....#..........#. +.#..#....#..#.#..#..#.........# +#...#....#..............#...... +.........#.....#.##...##...###. +.....#....##............#..#... +.....#.#...........#..#....#... +.#..........#...#......#.....#. +.#...........#.....#..#........ +..............#......##...#..#. +...#.........#..#....#..##...## +..##...#..................#.... +#.....#.................#...... +...#......#..#..........#.#.... +......#..#.....#.....##...#..#. +......#........#..........#.... +...##.##....#..##.#..........#. +..........#..#.#.##............ +..##........................#.. +.....#.#.#......#....#....##... +#....#.........#........#...... +.##.......#...#...#........##.. +....##......#....#.#..........# +..#.......#..............#..... +.....#......#.#...#..#.#.#....# +.....#..#........#.##.##....... +##........#..........#......... +.....#..##....#.#......###..##. +#.#...##.........#.#.....#..#.. +#....#.#...#........#.....#..#. +........................#...... +....###......#............#...# +...#..##......#..##.........#.. +.............#...#......#..#..# +....#......#....#...........#.. +..#.#.####.#.....##........#..# +#..#...#..#..#.......#.#..#.... +..#..#..#....#.#.........##..#. +.......#......#.#............#. +...#.............#.#.....#..... +...#.#.........##...#.#.......# +........#...#...........##...#. +..........#....#......#....##.. +..........#...........#........ +...#..#...#..........#......#.. +......#......#....#.....#..#.#. +........##.................#..# +.#........#.#...........#...... +#...#........#.#.#.....#.#.#... +.........#........#..#..#....#. +##........#..........#....#..#. +.#.##...........#..#.#..##....# +.......#.#....#..#......#...... +..#.....#........##..#......### +..#...#..................#....# +......#...#..#.##.......#...... +........#...#.#................ +.........#............#........ +..#.....##....#.#..##.......... +#.....#..........#....#........ +....#.#...#...##....#.....##... +..#.#.......#.............#...# +...##..............#......#.... +#......#...#................##. +.#.#...#.#..#.................. +...##.......#...........#.#.#.. +#......#.#.#........#.##...#### +.......#..#.#.........#.#.##..# +..............#....#.........#. +...........#.#..#....##......#. +#.............#...##..#.......# +.........#............#...#.##. +.......#.........#.#.....#..#.. +........................#.#.##. +#......#.#......#.........#.... +...#.......#.......#.....#..... +#..#....#................#...#. +........#.#..##......#......... +#..#...##....##....##.........# +.......#...#...###............. +#.#..#........#.#.#............ +#.....#........##.........#.#.. +.#..........#....#.#....###.... +.#.....#...#.#........#..#.##.. +...#.##......#..#.............# +..##..#.#...................#.. +.....#....#...#.#...#...#...... +.....#..#.#....#.#............. +#.#....#.#.##..###..........#.. +........#.#.............#..#... +.........#.......#............. +.##.#............##...#........ +......#................#....... +...............#..#...........# +...#.......#...#.##.....#....#. +##..##..#..........#........... +.##.#.......#...#..#...#...#... +....#..#...........#....#.##... +.#........#........#....#...... +.......#...#.##.#..#.#..#...... +.#..#......#....#...##....#.#.. +......#...##.#.....##.###.....# +.#....#..#......#...#.#.....#.. +#............#....##...##.##... +#...#.#....#...#.......##...##. +#...........#.##..#....#.....#. +...#..#...#.........#.......#.. +.#....#.....#............#.#..# +.#.....#.#...#.#....##......### +..#..#.#.#...#..#.............# +...#...#..#....#........#...##. +.......#.....#...##...........# +#.##.................#...##...# +..............##........#.....# +............#...#..#.......#.#. +#.#.....#.........#...#......#. +#.###..#......#..#..#...#.....# +.....#.......#................. +........#..#......#.#...#...... +#.......#..#........#...#..#... +..#...#.......##.............#. +#.......#.......##...#......... +.........#....#.#..##.....#...# +..#.....#.#.......#....#....... +...#.......#.....#..##.#..#.... +....#.......#.#.#.............. +.#..#......#........#.#..##..## +....#...#.##.#...#....##...#... +#..##..#.....#.......#......... +....#..#..#.#............#..... +#.......##...##..##............ +............................... +....#.......#.##...#.....#.#... +...#........#....#.#..#..#..... +##.......#.....##.#.#....#....# +#.............#...........#.##. +#...........#.#..........#..... +#..#....#....#.#.........#.#... +......#.#.#..#.#.#............. +...#.....#........##....#...... +..#...#...#.#.......#......#... +.##........#...#..#..........#. +..#...........#..##.....##..... +............#..#.#...#.....#... +..........#....##.......#...... +....#....#.................#..# +....#...............#.........# +..#.#...#......#..........##... +.....#...........#.........#..# +.......#.....##.....#.#........ +.#.#..........#....#........... +.#..##....#........#....#...... +....#.#..#.......#..#.........# +..#....#.....#......#..#....... +......#........#.......#...#.#. +.......#.......#....#.....##... +....##........#..#...#.#..#...# +.#......#...........##....#.... +##....##......#.......#.......# +.##....#.##......#.......##..#. +...#..#.#.#.......#..#.###..... +..........##....#..#.##........ +...#........###.#..#........#.. +.....#....#..##....#.....#....# +#..........#..........#.#....#. +..#....#.....#..............#.. +#..................#......#.##. +.#...#.#.....#.........##...... +...#...........#.....#......#.. +......#.....#.#..##......##.... +...#....###..#.....#..#..##..## +......#.......##..#..#......... +#..#.#....#.#..#..........##.#. +..#..#..##..#.#.#.#.....#...... +..#.#...#..#.....###.#......... +##.#.#......#........#.####.... +.............#..#..#....#...... +...##..........#.......#.#....# +..#.....................#...... +..#..#...##...#.##........#.... diff --git a/2020/03/part1.hs b/2020/03/part1.hs new file mode 100755 index 0000000..cb2b3f6 --- /dev/null +++ b/2020/03/part1.hs @@ -0,0 +1,44 @@ +#!/usr/bin/env runghc + +import Data.Array + +data Cell = Open | Tree + deriving (Eq) + +charToCell '.' = Open +charToCell '#' = Tree + +isValidCell '.' = True +isValidCell '#' = True +isValidCell _ = False + +instance Read Cell where + readsPrec _ (c:rest) = [(charToCell c, rest)] + readList input = + let (valid, rest) = span isValidCell input + in [(map charToCell valid, rest)] + +cellToChar Open = '.' +cellToChar Tree = '#' + +instance Show Cell where + show cell = [cellToChar cell] + showList = (++) . map cellToChar + +main = interact (show . solve . map (read :: String -> [Cell]) . lines) + +solve rows = count (==Tree) $ map at path + where + board = toArray $ map toArray rows + (_, y) = fmap (+1) $ bounds board + (_, x) = fmap (+1) $ bounds $ board ! 0 + xIdx i = mod i x + yIdx i = mod i y + deltaY = 1 + deltaX = 3 + at (x', y') = (board ! (yIdx y')) ! (xIdx x') + path = [(i*3, i) | i <- [0..y]] + +count predicate xs = length $ filter predicate xs + +toArray l = listArray (0, length l - 1) l diff --git a/2020/03/small.txt b/2020/03/small.txt new file mode 100644 index 0000000..7e88cdc --- /dev/null +++ b/2020/03/small.txt @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# diff --git a/2020/README.md b/2020/README.md index a267682..150fb17 100644 --- a/2020/README.md +++ b/2020/README.md @@ -14,7 +14,7 @@ asdf install | S | M | T | W | T | F | S | | :-: | :-: | :-: | :-: | :-: | :-: | :-: | -| | | [1] | [2] | 3 | 4 | 5 | +| | | [1] | [2] | [3] | 4 | 5 | | 6 | 7 | 8 | 9 | 10 | 11 | 12 | | 13 | 14 | 15 | 16 | 17 | 18 | 19 | | 21 | 22 | 23 | 24 | 25 | | | @@ -23,3 +23,4 @@ asdf install [haskell]: https://www.haskell.org [1]: ./01 [2]: ./02 +[3]: ./03