From 64261b54f560d91150d1910c17ef4461d05e66a7 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 3 Dec 2020 09:25:28 -0500 Subject: [PATCH] Day 3 --- lib/day3/day3.ex | 63 +++++++++ lib/day3/input.txt | 323 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 386 insertions(+) create mode 100644 lib/day3/day3.ex create mode 100644 lib/day3/input.txt diff --git a/lib/day3/day3.ex b/lib/day3/day3.ex new file mode 100644 index 0000000..270617e --- /dev/null +++ b/lib/day3/day3.ex @@ -0,0 +1,63 @@ +defmodule Day3 do + @example """ + ..##....... + #...#...#.. + .#....#..#. + ..#.#...#.# + .#...##..#. + ..#.##..... + .#.#.#....# + .#........# + #.##...#... + #...##....# + .#..#...#.# + """ + + def part1(example \\ false) do + count_trees(example, {3, 1}) + end + + def part2(example \\ false) do + one_one = count_trees(example, {1, 1}) + three_one = count_trees(example, {3, 1}) + five_one = count_trees(example, {5, 1}) + seven_one = count_trees(example, {7, 1}) + one_two = count_trees(example, {1, 2}) + + one_one * three_one * five_one * seven_one * one_two + end + + def count_trees(example, slope) do + example + |> parse_input() + |> count_trees_for_slope(slope) + end + + def parse_input(example) do + if(example, do: @example, else: File.read!("lib/day3/input.txt")) + |> String.trim() + |> String.split("\n") + |> Enum.map(fn line -> + line + |> String.codepoints() + |> Enum.map(fn + "#" -> 1 + _ -> 0 + end) + |> Stream.cycle() + end) + end + + def count_trees_for_slope(lines, slope) do + do_count(lines, slope, 0, 0) + end + + def do_count([current | rest], {dx, dy} = slope, x, trees) do + rest = Enum.drop(rest, dy - 1) + do_count(rest, slope, x + dx, trees + Enum.at(current, x)) + end + + def do_count([], _, _, trees) do + trees + end +end diff --git a/lib/day3/input.txt b/lib/day3/input.txt new file mode 100644 index 0000000..2d49b8b --- /dev/null +++ b/lib/day3/input.txt @@ -0,0 +1,323 @@ +.#....#..##.#..####....#....... +......#..#....#....###......#.# +#..#.....#..............##.#.#. +#.#...#....#...#......##..#..#. +...#..#.##..#..#........###.#.# +...#.#..........#.........##... +...#.#....#.#....#..#......#... +..##.#.....#.......#.###..#..## +..#.......#.......#....##...... +....##........##.##...#.###.##. +#.......#.......##..#......#... +..##.............##.#......#... +...#.####....#.....#...##...... +.............##.#......#....... +..#...#....#......#....#....... +..#....#..#............#....... +##...#..#........##..#......#.. +##........##........#.#.......# +#.......#........#.#..#....###. +.....#..#.#..........##...#.... +..##...#......#.#...#..#...#... +##.##...#......#....#....#...#. +#.......#..#.#..#....#..###.#.# +#.............#.#....#..#.#.... +...#.......###.#.##.##.#...#..# +.##.......##..##...#..###...... +.......#.#.#.#..####..#..#..#.. +...##......#.#.##.###....#.###. +###......###......#.#####..#... +..#........##..#..##.##..#...#. +.....##..#...#..#.##.....#.#... +#......#.##....#..##.#....#.#.. +##.#.##..#................#.... +......#.#....#......##.....#... +..#...##..#..#...#..#.#..#..... +........#.#.#.##...#.#.....#.#. +#.#......#.....##..#...#....... +..#.#......#...........###..... +......##....#....##..#..#.#.#.# +##....#.###...#......#..#...#.. +#.#.##....###...####.......#..# +##...........#.....#........#.# +.##.#..#.....#......#.......#.. +##..##..###....#.........##.... +..#..#..#.##...#.#...#........# +#.##.###...#.......#........... +.........#.................#... +#.##...#.....#..##........#.... +....#.#...##...#...........#... +.#.....#.#..#...##..##.....#... +.#.....####....#..##..#........ +...#..#......##.#..##.#.#.#..#. +.##.#.....#.....#...#.......##. +......#..#..#......#...####.... +.......#......##..#..##.....#.. +......#.#..#...#..#.#.......... +....##.........#............... +.#....#..##.....#....##.##..... +#.#.....#...#....####....#.#... +#.....#....#.#...#............. +...#..#.....#....##..#..#...... +...#.#............#...........# +###.#..#.#......#.....##.....#. +####....#....###.....#..#.##### +.###..#...#.#..#......##.#.#.#. +.....#.##.#....#..##....#..#..# +...#....#...##.....#......#.#.. +....#...#....#...#....#.....#.# +.#.....#.....#.#..#......#..#.. +..#..##....##.##....#.....##... +#..##...#.##...#..#.#.#.....#.. +...#..##.#..#....#.#....######. +..........#..#.....#....#...##. +#.#####.#.###..#.....#......... +#....#......#..#.#.##.##..###.. +..#...###.#.#....##.##...##.... +.......#....#..#...##......#... +...#.#...#..#.....#..##.#...... +###..##...........#............ +..#....#.##....#.#..##...#..... +##....#...#....#.....#.#..##... +..............#.##.#..#..##.### +......#..#..#..#.#....###...##. +.#...#..#.#.#....#..........#.. +..#.#.....#..#...........#.##.. +...#.#......#......#..#..#.#... +...#....#.#.#.....#...#.##..#.. +.#.#..#...#........##.......#.. +##..........#..#...#....###.#.. +#.....###......#..#.#.#....#.#. +..###.......#.#...............# +#....#.....#.#......#..##.##... +#.##.#.#....#..#.#...#.#...#..# +#....#..#...........#.......#.. +...#.####.....#.........###.##. +......#..#.....#..........#..#. +#...#.#..####...#...#.#.##...## +.##.........#......#.#.#....... +.......##...##.##....###...##.. +...#..#....#..#.#.#.....#.#.... +#....#.#.#.......##..###..##... +......#............#.#...#..#.. +#.#.....#......#...#.......###. +...#.#................#...#.... +.....#......#.#..#...##.#.#...# +#....#.#..#..#..##..#.##..#.... +#.................#..#....#.... +..#....#.......####....###..... +.#..#.#.#...###..#...#..###.... +..#..#.#......#.###..........#. +.....#......#.......##....##.#. +.#....#........#.#.##.#........ +#.#..##..#..#.#...####....##... +...#....#.#..#...#..........#.. +.#.....#.##....#...##.......... +....##....#.....#.....#...#.### +.#...##.#.#..##..#...#.#..#..#. +..#.......#.##.....#.#........# +...#...#.....##..#.#.#....#.... +...#.....#.......##.........#.# +.##.....#..#.#...#.#...#.#...#. +...........#...#.###..#...#..#. +#.##........#..###.##...####... +.#.....#.#...##...#..#..#...##. +..#....#..#...#.....#.....##... +..###..#.#.....##........#.##.. +.#.#..##........#.##....#..#.## +.####.#..##..#.#..#....##....#. +.##....##...#.#........#....... +....#..#..#...#..#..#..#.#..... +...#......................#.... +#.....#.#....#..#..#.#..#....#. +##.....#.....##..........###... +.#..#..............#...##.#.#.# +...#...#.#.............#.#..#.# +..#.....#.......#......#.#..... +.###.#..#..#..#.#..#.....#..... +.....##..##...##.......#....### +.#........###...##..#....##.... +#....#.#......##..#....#.##..#. +#....#.#...#........##...###... +.#.....#...#.###....#.##.#.#### +###......#....#...#....##..#..# +##....#..###......#...#.#.#.... +..........#......##.#..#....... +.#..#......###.........##...#.. +....#......#....#.........#.#.# +##.#.#...#.#...#...#..#......#. +....#.##.........#..#.....##.#. +........#...#..#.#.#.#.....##.. +..#......#.#.#..#.....##....... +..............#....#....##.#..# +....#.#.....#....#.#.###.#....# +..#..........#..#......#.##..#. +...#...#.#.............#..#.... +#.......#..#..##.........##..#. +..##..#............#.....#..... +....#.#..##...#.#..#.........#. +........#.......#.##....#....#. +...#.....#.#.....#.#....#...... +..#......##.#.............#.#.# +#.#.............#.#.....#...... +.##....##.#.....#....#...##.... +.#.#....##....#.....##......... +...#.....#.....#.....#..#.###.. +.......#....#...##.#...#...#..# +..#.#.......#...###.#...#....#. +.....###..##....###.#.##....... +....#..................##.#.##. +.#.......###.##...#.#.....#.... +....#....##...##.....#.#...#..# +#..#.....#......##...#....#.... +#..##.........#.....#...#...... +...#..##.......##......#..##### +.#..###.###.#.##........#...... +.#...#....#....#.#....#...##... +##........#....#.........##..#. +.#.....##............#.#....... +....#....#...........###.....## +.#......#.#.#..#....#.#.....##. +......#.##.#..##....#.#.#..#... +#....#......#...#..####........ +......#..#..##..#.......#.#..#. +##....##.###.#...#.##.#..#.###. +.#.........#...##...#.#......#. +..#.#...........####.#....##.## +.....#.#..##.#...###...#..#.#.. +...#..#..##.#...#.....#.##...## +..##......##..........#..###... +.#......##.....#.##....#.#.##.# +...#.......##..##.....#....#... +.##...#...#....#..#............ +#..#....#...........#.......... +......#...#.#.......#...#.##..# +..#.###..#.....#.....#..#.....# +....#....#..........##....#..#. +.......##.#.#.#......#....#...# +####..#.###........#..#......#. +#........##.#.#.#.............# +.#......#......#..#.##.....#... +.....##.##......##.#.....#.#.#. +.......##.#.....##.......#.#.#. +.#.#..#.#..#.##...#.#....#.#..# +.#..##....#..#...##.......#..#. +.#.#..#.......#................ +#........#.#.#......#.#.#.#.... +##......#...#..##.#...##.##.... +##.#..#...........##...#..###.. +......#.####...#........#.#.#.. +...#........##..###.#.#...#...# +.#.....##..#......##......###.. +..#.#...#......#..#..##.#.....# +#....#..#.#..........#...#..... +.#......#.##..###..#.#....#..## +.......#.......#..#..#......#.. +..##.....##.#..#..#.....##..... +........#.##...#.#.#..#..#..#.. +...#.######.........#.....#..## +.#.#............#....#......... +#...#....###.#......#.#........ +#.........#....#...##.......... +....#...........#.###.#...###.. +.........#........#.#.#..#...#. +.#.......#.#.....#.#.....#.##.. +.....#.......#.....#.#.#....... +#.##..#..##.......#...#......#. +.###.....##...##.#...##.##.#.#. +...#......##..##............#.# +...#......................#..## +#..#..#................#...#... +#..#....#.#.#...##.......#..#.. +....#.#..###.##...#..#.###..#.. +..#...#....####.#............#. +......#....#.#...#.#.#......... +#...#........#.....##..###.#..# +#....#...#...##...#..#....##... +#..#...#..#.......#.#..##.#..#. +#.#..........#...........##.... +.#...###...#......#.......#.#.# +.........#.........#...#...##.. +##.#..###......##..#.....#..#.. +....##...............#.....#... +.....#.....###.#.....#.#....... +....#..#......###..#..##..#.... +......................#.....#.. +..#..#...##....##....#........# +..#....#...#...#.......#.....#. +...##.#.#.##......#.#.#.#.####. +.###...#..#......#.#..#........ +#..#...##.#..#..##..##....#...# +...#...#..#..#..#........#..##. +.##....#.##.#....#...#.#.#....# +#..#....#..#....#.......##..#.# +...#.#....####...........#...#. +#...#####...#.#..#......#...#.# +.##....#.#.#..#......#..##..... +..........#..#.#.#.....##...... +.....#....#..................#. +.........#...#...#....#..###... +.#.#.#....#.................... +......##............##.###..#.. +#.#...#........####.##..#.#.##. +#........#.#.#.#..#.##.....#... +......####..#.##.......#....#.. +.........#...#...#.....#....... +..##.....#...#...#.....##.....# +....#...##....#.....#..#..##.## +..#.........##...##..###..#.... +#....#.#.........##.###.#...##. +.##...#....#..#..#.#....##..... +##..#..#..#...........#.##....# +....#..........#...#..#.....#.. +........###..#..#.#.#.....##... +#...#...#..###............###.. +..#.....#.#.#..#..#.#..#......# +..#...##..#....#...#......#.... +#....#........##.....#..##....# +#.....#.#.#..#.......##.#.#.##. +..##...#...#.....#..........#.. +##.....#....#......#..........# +......#..#..........#.#..####.. +......#...#............##...##. +..#.......##.......#...###.###. +.#..#.#.#...#..##.#......#.#... +.##.....##.#.#...#.##.........# +#.#.######...........#.#####.#. +........#.##...##....##.#.##.#. +....#......#.....#.....###...## +#..............#.#....#.#....#. +....#..###.#.........##.#.#.... +..#.#.#..##....####..........#. +...#..#.......#................ +...#....#..............#....#.. +.....#...#...#....#.#.#..#...#. +......##.............###.##..## +.#...#.#..#......#..#.##....... +##.....#.....#.##...#....#..... +..#..#.#.#.#.#..........#..###. +##..........#........#....#.#.. +.....#...#........#.#..###....# +.###.#........#.##......#.#...# +#...##....#....#....##.#.#..... +.....#.#............#.......... +..#.##....................#.... +.....#..#..#.#..#.##.......#... +.....###......#......##......## +#.....#.#.......##.......#...#. +.#.#...#......#..###...#.....#. +#.#..#...#..##.....#...#.#..#.. +.....#.#..........#..#......... +.###..##..##.....#...#...#..##. +#...#.#....#.......##..#....... +###...#.#.#..#.......#......#.. +....##........#..........##.... +............#....#...........#. +#..#.#....##..#.#..#......##... +.###....##...#....##..........# +.###........#........###.....#. +...#...#.#......#...#....#..... +.###.......#.........#......... +....##.#......#...###......##.# +.###...#..##.....##.......#.... +.#.#...#..#.##....#........#...