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
Hello, guys. I’m back again, but only for the weekends, maybe.
This topic is about Day 13 of the Advent of Code 2020 .
Thanks to @egze,...
New
Note: This topic is to talk about Day 10 of the Advent of Code 2019 .
There is a private leaderboard for elixirforum members. You can jo...
New
Phew, this one took a while to get right. My naive attempts was way to slow so I reached for Dijkstras shortest path algorithm.. and that...
New
This topic is about Day 18 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
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
This was way too easy after the last few days. Simple map, filter and count.
https://github.com/shritesh/advent/blob/main/2023/06.livemd
New
Note: This topic is to talk about Day 5 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
Nobody’s doing Advent of Code this year? :grinning_face_with_smiling_eyes:
I might do the first week or so.
For Day 1, first I solved i...
New
Note by the Moderators: This topic is for general discussion about the Advent of Code 2018.
To prevent people from being spoiled about s...
New
Ok, that was a rough one today.
I haven’t found a way to improve the algorithm further. Part 1 runs in .5 seconds, Part 2 in ~ 5 minutes...
New
Other popular topics
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
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
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
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









