Aetherus
Advent of Code 2020 - Day 3
This topic is about Day 3 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/leaderboard/private/view/39276
The join code is:
39276-eeb74f9a
Most Liked
adamu
I used:
Stream.cycle/1to handle the repeating horizontal valuesEnum.take_every/2to handle skipping the downward slope.- The input wasn’t very long, so I just brute forced the horizontal value with
Enum.at/2.
def slope(v_x, v_y) do
{_, trees} =
File.read!("input")
|> String.trim()
|> String.split("\n")
|> Enum.take_every(v_y)
|> Enum.map(&String.to_charlist/1)
|> Enum.reduce({0, 0}, fn row, {x, trees} ->
trees =
case Stream.cycle(row) |> Enum.at(x) do
?# -> trees + 1
?. -> trees
end
{x + v_x, trees}
end)
trees
end
5
kwando
I see I’m not the first one to find the Stream.cycle function ![]()
My version:
defmodule Aoc2020.Day03 do
def part1(input) do
count_trees(input, {3, 1})
end
def part2(input) do
slopes = [
{1, 1},
{3, 1},
{5, 1},
{7, 1},
{1, 2}
]
for slope <- slopes, reduce: 1 do
product -> product * count_trees(input, slope)
end
end
def count_trees(input, {dx, dy}) do
input
|> Stream.take_every(dy)
|> Stream.map(&Stream.cycle/1)
|> Enum.reduce({0, 0}, fn
row, {trees, shift} ->
row
|> Stream.drop(shift)
|> Enum.at(0)
|> case do
?. ->
{trees, shift + dx}
?# ->
{trees + 1, shift + dx}
end
end)
|> elem(0)
end
def input_stream(path) do
File.stream!(path)
|> Stream.map(&parse/1)
end
defp parse(line) do
line
|> String.trim()
|> String.to_charlist()
end
end
input = Aoc2020.Day03.input_stream("input.txt")
input
|> Aoc2020.Day03.part1()
|> IO.inspect(label: "part1")
input
|> Aoc2020.Day03.part2()
|> IO.inspect(label: "part2")
4
Damirados
My solution with streams keeping only 1 line of map in memory at any given time.
defmodule Event3 do
def run do
part1_ruleset = [{3, 1}]
part2_ruleset = [{1, 1}, {3, 1}, {5, 1}, {7, 1}, {1, 2}]
IO.puts("Test part1: #{solver("input/event3/test.txt", part1_ruleset)}")
IO.puts("Puzzle part1: #{solver("input/event3/puzzle.txt", part1_ruleset)}")
IO.puts("Test part2: #{solver("input/event3/test.txt", part2_ruleset)}")
IO.puts("Puzzle part2: #{solver("input/event3/puzzle.txt", part2_ruleset)}")
end
def solver(path, ruleset) do
accs = Enum.map(ruleset, &rule_to_acc/1)
input_stream(path)
|> Stream.drop(1)
|> Stream.transform(accs, &step_all/2)
|> Stream.take(-length(ruleset))
|> Stream.flat_map(& &1)
|> Enum.reduce(&(&1 * &2))
end
def input_stream(path), do: path |> File.stream!() |> Stream.map(&parse_input/1)
def parse_input(input), do: String.trim(input) |> String.graphemes() |> Enum.map(&(&1 == "#"))
def step_all(input, acc), do: Enum.map(acc, &step(input, &1)) |> Enum.unzip()
def step(input, {count, index, step, step_down, step_down}) do
width = length(input)
count = count + ((Enum.at(input, index) && 1) || 0)
{[count], {count, rem(index + step, width), step, step_down, 1}}
end
def step(_input, {count, index, step, step_down, down_counter}),
do: {[count], {count, index, step, step_down, down_counter + 1}}
def rule_to_acc({right, down}), do: {0, right, right, down, 1}
end
3
Popular in Challenges
This topic is about Day 15 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
New
This topic is about Day 15 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Today’s challenge for me was about using reduce:
defmodule Prob5 do
def move([[h1 | rest] = _list1, list2]) do
[rest, [h1 | list2]...
New
Note by the Moderators: This topic is to talk about Day 6 of the Advent of Code.
For general discussion about the Advent of Code 2018 an...
New
Note: This topic is to talk about Day 4 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
My solution finishes both parts in 5 seconds on my computer. That time should be possible to reduce by optimizing my rather naive tilt/2 ...
New
I’m having so much fun working on the “Protohackers” challenges, I never got into Advent of Code much but this has been amazing. The chal...
New
Anyone have a solution to Part 2 today? Part 1 was straight forward, but I can’t figure out a programatic way to do part 2. I understand ...
New
Note: This topic is to talk about Day 9 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
Fairly straightforward Dijkstra’s algorithm
import AOC
aoc 2023, 17 do
def compute(input, candidates) do
{{max_row, max_col}, ite...
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help.
Where are they similar?
Where do they differ the m...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
What learn first? Rust or Elixir
Hi Elixir community!
I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
Hi!
Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









