Aetherus
Advent of Code 2020 - Day 7
This topic is about Day 7 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
I become busy this week, so I may not be able to create such topics in time. Apologies in advance.
Most Liked
code-shoily
I used :digraph for the first one
still doing the second one (the way I found ended up counting leaves), your solution does give me a little hint
so thank you.
2
kwando
I tried a too clever solution first.. but I rolled back to this one.. took some tries to get right, so it is not that polished yet ![]()
defmodule Aoc2020.Day07 do
def part1(input) do
rules =
input
|> Enum.into(%{})
for {color, specs} <- rules, color != "shiny gold", reduce: 0 do
sum ->
if find(rules, specs, "shiny gold") do
sum + 1
else
sum
end
end
end
def part2(input) do
rules =
input
|> Enum.into(%{})
count_bags(rules, "shiny gold") - 1
end
def find(rules, [], _), do: false
def find(rules, [{_, color} | rest], color), do: true
def find(rules, [{_, child_color} | rest], color),
do: find(rules, rest, color) || find(rules, Map.get(rules, child_color, []), color)
def count_bags(rules, color) do
specs = Map.get(rules, color, [])
for {quantity, color} <- specs, reduce: 1 do
sum ->
sum + quantity * count_bags(rules, color)
end
end
def input_stream(path) do
File.stream!(path)
|> Stream.map(&parse/1)
end
def parse(line) do
line
|> String.replace(["bags", "contain", "bag"], "")
|> String.replace(~r/\s+|\./, " ")
|> String.trim()
|> String.split(",")
|> Enum.map(fn part -> String.trim(part) |> String.split(" ") end)
|> create_rule()
end
def create_rule([[mod, color | rule] | rules]) do
{"#{mod} #{color}", parse_spec([rule | rules])}
end
defp parse_spec([["no", "other"]]), do: []
defp parse_spec([]), do: []
defp parse_spec([[quantity, mod, color] | rest]) do
[{String.to_integer(quantity), "#{mod} #{color}"} | parse_spec(rest)]
end
end
input = Aoc2020.Day07.input_stream("input.txt")
Aoc2020.Day07.part1(input)
|> IO.inspect(label: "part1")
Aoc2020.Day07.part2(input)
|> IO.inspect(label: "part2")
1
LostKobrakai
Same here. But now it’s working:
https://github.com/LostKobrakai/aoc2020/commit/09be6db4a1ea87da26e01d67db746af2390710bc
1
Popular in Challenges
Note: This topic is to talk about Day 12 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can joi...
New
This topic is about Day 9 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
This topic is about Day 3 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
This topic is about Day 18 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Continuation of Advent of Code 2022:christmas_tree:, Day 1:
Day 2!
Leaderboard:
New
This is my second year doing AoC in Elixir and my first year doing it with Livebook.
When I was doing just plain Elixir I usually set up...
New
This topic is about Day 2 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adven...
New
part 1:
https://github.com/rugyoga/aoc2021/blob/main/day8.exs
part 2:
https://github.com/rugyoga/aoc2021/blob/main/day8b.exs
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
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
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
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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









