This commit is contained in:
Shadowfacts 2020-12-03 09:25:28 -05:00
parent 4458f8021d
commit 64261b54f5
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 386 additions and 0 deletions

63
lib/day3/day3.ex Normal file
View File

@ -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

323
lib/day3/input.txt Normal file
View File

@ -0,0 +1,323 @@
.#....#..##.#..####....#.......
......#..#....#....###......#.#
#..#.....#..............##.#.#.
#.#...#....#...#......##..#..#.
...#..#.##..#..#........###.#.#
...#.#..........#.........##...
...#.#....#.#....#..#......#...
..##.#.....#.......#.###..#..##
..#.......#.......#....##......
....##........##.##...#.###.##.
#.......#.......##..#......#...
..##.............##.#......#...
...#.####....#.....#...##......
.............##.#......#.......
..#...#....#......#....#.......
..#....#..#............#.......
##...#..#........##..#......#..
##........##........#.#.......#
#.......#........#.#..#....###.
.....#..#.#..........##...#....
..##...#......#.#...#..#...#...
##.##...#......#....#....#...#.
#.......#..#.#..#....#..###.#.#
#.............#.#....#..#.#....
...#.......###.#.##.##.#...#..#
.##.......##..##...#..###......
.......#.#.#.#..####..#..#..#..
...##......#.#.##.###....#.###.
###......###......#.#####..#...
..#........##..#..##.##..#...#.
.....##..#...#..#.##.....#.#...
#......#.##....#..##.#....#.#..
##.#.##..#................#....
......#.#....#......##.....#...
..#...##..#..#...#..#.#..#.....
........#.#.#.##...#.#.....#.#.
#.#......#.....##..#...#.......
..#.#......#...........###.....
......##....#....##..#..#.#.#.#
##....#.###...#......#..#...#..
#.#.##....###...####.......#..#
##...........#.....#........#.#
.##.#..#.....#......#.......#..
##..##..###....#.........##....
..#..#..#.##...#.#...#........#
#.##.###...#.......#...........
.........#.................#...
#.##...#.....#..##........#....
....#.#...##...#...........#...
.#.....#.#..#...##..##.....#...
.#.....####....#..##..#........
...#..#......##.#..##.#.#.#..#.
.##.#.....#.....#...#.......##.
......#..#..#......#...####....
.......#......##..#..##.....#..
......#.#..#...#..#.#..........
....##.........#...............
.#....#..##.....#....##.##.....
#.#.....#...#....####....#.#...
#.....#....#.#...#.............
...#..#.....#....##..#..#......
...#.#............#...........#
###.#..#.#......#.....##.....#.
####....#....###.....#..#.#####
.###..#...#.#..#......##.#.#.#.
.....#.##.#....#..##....#..#..#
...#....#...##.....#......#.#..
....#...#....#...#....#.....#.#
.#.....#.....#.#..#......#..#..
..#..##....##.##....#.....##...
#..##...#.##...#..#.#.#.....#..
...#..##.#..#....#.#....######.
..........#..#.....#....#...##.
#.#####.#.###..#.....#.........
#....#......#..#.#.##.##..###..
..#...###.#.#....##.##...##....
.......#....#..#...##......#...
...#.#...#..#.....#..##.#......
###..##...........#............
..#....#.##....#.#..##...#.....
##....#...#....#.....#.#..##...
..............#.##.#..#..##.###
......#..#..#..#.#....###...##.
.#...#..#.#.#....#..........#..
..#.#.....#..#...........#.##..
...#.#......#......#..#..#.#...
...#....#.#.#.....#...#.##..#..
.#.#..#...#........##.......#..
##..........#..#...#....###.#..
#.....###......#..#.#.#....#.#.
..###.......#.#...............#
#....#.....#.#......#..##.##...
#.##.#.#....#..#.#...#.#...#..#
#....#..#...........#.......#..
...#.####.....#.........###.##.
......#..#.....#..........#..#.
#...#.#..####...#...#.#.##...##
.##.........#......#.#.#.......
.......##...##.##....###...##..
...#..#....#..#.#.#.....#.#....
#....#.#.#.......##..###..##...
......#............#.#...#..#..
#.#.....#......#...#.......###.
...#.#................#...#....
.....#......#.#..#...##.#.#...#
#....#.#..#..#..##..#.##..#....
#.................#..#....#....
..#....#.......####....###.....
.#..#.#.#...###..#...#..###....
..#..#.#......#.###..........#.
.....#......#.......##....##.#.
.#....#........#.#.##.#........
#.#..##..#..#.#...####....##...
...#....#.#..#...#..........#..
.#.....#.##....#...##..........
....##....#.....#.....#...#.###
.#...##.#.#..##..#...#.#..#..#.
..#.......#.##.....#.#........#
...#...#.....##..#.#.#....#....
...#.....#.......##.........#.#
.##.....#..#.#...#.#...#.#...#.
...........#...#.###..#...#..#.
#.##........#..###.##...####...
.#.....#.#...##...#..#..#...##.
..#....#..#...#.....#.....##...
..###..#.#.....##........#.##..
.#.#..##........#.##....#..#.##
.####.#..##..#.#..#....##....#.
.##....##...#.#........#.......
....#..#..#...#..#..#..#.#.....
...#......................#....
#.....#.#....#..#..#.#..#....#.
##.....#.....##..........###...
.#..#..............#...##.#.#.#
...#...#.#.............#.#..#.#
..#.....#.......#......#.#.....
.###.#..#..#..#.#..#.....#.....
.....##..##...##.......#....###
.#........###...##..#....##....
#....#.#......##..#....#.##..#.
#....#.#...#........##...###...
.#.....#...#.###....#.##.#.####
###......#....#...#....##..#..#
##....#..###......#...#.#.#....
..........#......##.#..#.......
.#..#......###.........##...#..
....#......#....#.........#.#.#
##.#.#...#.#...#...#..#......#.
....#.##.........#..#.....##.#.
........#...#..#.#.#.#.....##..
..#......#.#.#..#.....##.......
..............#....#....##.#..#
....#.#.....#....#.#.###.#....#
..#..........#..#......#.##..#.
...#...#.#.............#..#....
#.......#..#..##.........##..#.
..##..#............#.....#.....
....#.#..##...#.#..#.........#.
........#.......#.##....#....#.
...#.....#.#.....#.#....#......
..#......##.#.............#.#.#
#.#.............#.#.....#......
.##....##.#.....#....#...##....
.#.#....##....#.....##.........
...#.....#.....#.....#..#.###..
.......#....#...##.#...#...#..#
..#.#.......#...###.#...#....#.
.....###..##....###.#.##.......
....#..................##.#.##.
.#.......###.##...#.#.....#....
....#....##...##.....#.#...#..#
#..#.....#......##...#....#....
#..##.........#.....#...#......
...#..##.......##......#..#####
.#..###.###.#.##........#......
.#...#....#....#.#....#...##...
##........#....#.........##..#.
.#.....##............#.#.......
....#....#...........###.....##
.#......#.#.#..#....#.#.....##.
......#.##.#..##....#.#.#..#...
#....#......#...#..####........
......#..#..##..#.......#.#..#.
##....##.###.#...#.##.#..#.###.
.#.........#...##...#.#......#.
..#.#...........####.#....##.##
.....#.#..##.#...###...#..#.#..
...#..#..##.#...#.....#.##...##
..##......##..........#..###...
.#......##.....#.##....#.#.##.#
...#.......##..##.....#....#...
.##...#...#....#..#............
#..#....#...........#..........
......#...#.#.......#...#.##..#
..#.###..#.....#.....#..#.....#
....#....#..........##....#..#.
.......##.#.#.#......#....#...#
####..#.###........#..#......#.
#........##.#.#.#.............#
.#......#......#..#.##.....#...
.....##.##......##.#.....#.#.#.
.......##.#.....##.......#.#.#.
.#.#..#.#..#.##...#.#....#.#..#
.#..##....#..#...##.......#..#.
.#.#..#.......#................
#........#.#.#......#.#.#.#....
##......#...#..##.#...##.##....
##.#..#...........##...#..###..
......#.####...#........#.#.#..
...#........##..###.#.#...#...#
.#.....##..#......##......###..
..#.#...#......#..#..##.#.....#
#....#..#.#..........#...#.....
.#......#.##..###..#.#....#..##
.......#.......#..#..#......#..
..##.....##.#..#..#.....##.....
........#.##...#.#.#..#..#..#..
...#.######.........#.....#..##
.#.#............#....#.........
#...#....###.#......#.#........
#.........#....#...##..........
....#...........#.###.#...###..
.........#........#.#.#..#...#.
.#.......#.#.....#.#.....#.##..
.....#.......#.....#.#.#.......
#.##..#..##.......#...#......#.
.###.....##...##.#...##.##.#.#.
...#......##..##............#.#
...#......................#..##
#..#..#................#...#...
#..#....#.#.#...##.......#..#..
....#.#..###.##...#..#.###..#..
..#...#....####.#............#.
......#....#.#...#.#.#.........
#...#........#.....##..###.#..#
#....#...#...##...#..#....##...
#..#...#..#.......#.#..##.#..#.
#.#..........#...........##....
.#...###...#......#.......#.#.#
.........#.........#...#...##..
##.#..###......##..#.....#..#..
....##...............#.....#...
.....#.....###.#.....#.#.......
....#..#......###..#..##..#....
......................#.....#..
..#..#...##....##....#........#
..#....#...#...#.......#.....#.
...##.#.#.##......#.#.#.#.####.
.###...#..#......#.#..#........
#..#...##.#..#..##..##....#...#
...#...#..#..#..#........#..##.
.##....#.##.#....#...#.#.#....#
#..#....#..#....#.......##..#.#
...#.#....####...........#...#.
#...#####...#.#..#......#...#.#
.##....#.#.#..#......#..##.....
..........#..#.#.#.....##......
.....#....#..................#.
.........#...#...#....#..###...
.#.#.#....#....................
......##............##.###..#..
#.#...#........####.##..#.#.##.
#........#.#.#.#..#.##.....#...
......####..#.##.......#....#..
.........#...#...#.....#.......
..##.....#...#...#.....##.....#
....#...##....#.....#..#..##.##
..#.........##...##..###..#....
#....#.#.........##.###.#...##.
.##...#....#..#..#.#....##.....
##..#..#..#...........#.##....#
....#..........#...#..#.....#..
........###..#..#.#.#.....##...
#...#...#..###............###..
..#.....#.#.#..#..#.#..#......#
..#...##..#....#...#......#....
#....#........##.....#..##....#
#.....#.#.#..#.......##.#.#.##.
..##...#...#.....#..........#..
##.....#....#......#..........#
......#..#..........#.#..####..
......#...#............##...##.
..#.......##.......#...###.###.
.#..#.#.#...#..##.#......#.#...
.##.....##.#.#...#.##.........#
#.#.######...........#.#####.#.
........#.##...##....##.#.##.#.
....#......#.....#.....###...##
#..............#.#....#.#....#.
....#..###.#.........##.#.#....
..#.#.#..##....####..........#.
...#..#.......#................
...#....#..............#....#..
.....#...#...#....#.#.#..#...#.
......##.............###.##..##
.#...#.#..#......#..#.##.......
##.....#.....#.##...#....#.....
..#..#.#.#.#.#..........#..###.
##..........#........#....#.#..
.....#...#........#.#..###....#
.###.#........#.##......#.#...#
#...##....#....#....##.#.#.....
.....#.#............#..........
..#.##....................#....
.....#..#..#.#..#.##.......#...
.....###......#......##......##
#.....#.#.......##.......#...#.
.#.#...#......#..###...#.....#.
#.#..#...#..##.....#...#.#..#..
.....#.#..........#..#.........
.###..##..##.....#...#...#..##.
#...#.#....#.......##..#.......
###...#.#.#..#.......#......#..
....##........#..........##....
............#....#...........#.
#..#.#....##..#.#..#......##...
.###....##...#....##..........#
.###........#........###.....#.
...#...#.#......#...#....#.....
.###.......#.........#.........
....##.#......#...###......##.#
.###...#..##.....##.......#....
.#.#...#..#.##....#........#...