lud
Advent of Code 2020 - Day 21
This topic is about Day 21 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
blue_quartz
After the slaughter that is day 20 (which I did solve, but with a sprawling 163 sloc solution
), this was a good detox, and I got to even reuse day 16’s logic a bit too…
I ‘accidentally’ created the relevant map for day 2 while solving for day 1, and the day 16 reuse is for the part where we have a map of values with increasing lengths, so we just need to sort by that and start picking out the correct value for the key.
defmodule AdventOfCode.Day21 do
@moduledoc "Day 21"
defmodule Food do
defstruct ingredients: MapSet.new(), allergens: MapSet.new()
end
defp parse(food) do
to_set = &(MapSet.new(String.split(&1, &2)))
[_, ingredients, allergens] = Regex.run(~r/^(.*) \(contains (.*)\)$/, food)
%Food{ingredients: to_set.(ingredients, " "), allergens: to_set.(allergens, ", ")}
end
defp process(input) do
all_food = Enum.map(input, &parse/1)
by_allergen = Map.new(
Enum.reduce(all_food, MapSet.new(), &(MapSet.union(&1.allergens, &2))),
fn allergen ->
case Enum.filter(all_food, &(allergen in &1.allergens)) do
[] -> {allergen, []}
x -> {allergen, Enum.reduce(tl(x), hd(x).ingredients, &(MapSet.intersection(&1.ingredients, &2)))}
end
end
)
{all_food, by_allergen}
end
def part1(input) do
{all_food, by_allergen} = process(input)
bad = MapSet.new(Enum.flat_map(by_allergen, &(elem(&1, 1))))
Enum.sum(Enum.map(all_food, fn food -> Enum.count(Enum.filter(food.ingredients, &(!(&1 in bad)))) end))
end
def part2(input) do
{_, by_allergen} = process(input)
Enum.reduce(
Enum.sort_by(by_allergen, &(Enum.count(elem(&1, 1)))),
Map.new(),
fn {allergen, ingredients}, acc -> Map.put(acc, hd(MapSet.to_list(ingredients) -- Map.keys(acc)), allergen) end
)
|> Enum.sort_by(&(elem(&1, 1)))
|> Enum.map(&(elem(&1, 0)))
|> Enum.join(",")
end
end
2
Papey
Yep, mine is a bit verbose maybe but it works so ![]()
https://github.com/papey/aoc/commit/a730fc00747f565bb7d622a5c54f1cc11b7a5701
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 18 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
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
Today’s challenge for me was about using reduce:
defmodule Prob5 do
def move([[h1 | rest] = _list1, list2]) do
[rest, [h1 | list2]...
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
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
This one was much easier for me than yesterday’s. Part 1 runs in 22ms and part 2 runs in ~3s.
defmodule BridgeRepair do
def parse_inpu...
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: This topic is to talk about Day 13 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can joi...
New
This topic is about Day 14 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Other popular topics
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
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
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
Got a question about when to concat vs. prepending items to list then reversing to achieve appending.
So i know lists boil down to [1 | ...
New
I would like to know what is the best IDE for elixir development?
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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








