aoc/2020/lib/days/day3.ex
2023-04-17 20:18:20 +02:00

33 lines
704 B
Elixir

defmodule AOC.Day3 do
@line_length 31
def count_trees(lines, jump), do: count_trees(lines, jump, 0, 0)
def count_trees([], _, _, acc), do: acc
def count_trees([hd | tl], jump, x, acc) do
acc =
acc +
if String.at(hd, x) == "#" do
1
else
0
end
x = rem(x + jump, @line_length)
count_trees(tl, jump, x, acc)
end
def part1 do
AOC.Util.input_lines(3, 1)
|> count_trees(3)
end
def part2 do
lines = AOC.Util.input_lines(3, 1)
count_down2 = Enum.take_every(lines, 2) |> count_trees(1)
tree_counts = Enum.map([1, 3, 5, 7], &count_trees(lines, &1))
Enum.reduce([count_down2 | tree_counts], &*/2)
end
end