Aetherus
Advent of Code 2020 - Day 16
This topic is about Day 16 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
Was a bit worried that my answer was quite complex, with multiple maps, transposing, tracking seen values, and nlogn filtering, but it seems that’s what everyone else did too. 
I need to focus on work for the next couple of weeks, so I’ll only be looking at the remaining problems on days off from now on.
adamu
Enum.zip to transpose. Wish I’d thought of that.
adamu
Also: I thought -- was much nicer than MapSet.difference. I was willing to accept a performance hit for the increased simplicity, but apparent it actually performs better since OTP 22:
As of Erlang/OTP 22, this operation is significantly faster even if both lists are very long, and using
--/2is usually faster and uses less memory than using theMapSet-based alternative mentioned above.
blue_quartz
I’m kind of surprised that I can just ‘chain’ my valid tickets eventually into a nice map of keys to column indices (e.g. "row" => 1)…
Explanation:
- Get valid tickets
- Pivot them so that you get a map of column indices to all the values of that column
- Map that to a tuple
{col, matches}wherematchesis a list of possible keys - Sort by the count of
matches(almost there) - Reduce to another map, this time keyed by the key with the column index as the value
- Filter for map entries where the key starts with
"departure" - Perform the final reduction by multiplication
# snippet only
{rules, own, nearby} = process(input)
Enum.filter(nearby, &(elem(&1, 1) == []))
|> Enum.map(fn {ticket, _} -> Map.new(Enum.with_index(ticket), fn {n, index} -> {index, [n]} end) end)
|> Enum.reduce(&(Map.merge(&1, &2, fn _, v1, v2 -> v1 ++ v2 end)))
|> Enum.map(
fn {col, values} ->
{col, Enum.map(Enum.filter(rules, fn {_, valid?} -> Enum.all?(values, valid?) end), &(elem(&1, 0)))}
end
)
|> Enum.sort_by(&(length(elem(&1, 1))))
|> Enum.reduce(Map.new(), fn {col, matches}, acc -> Map.put(acc, hd(matches -- Map.keys(acc)), col) end)
|> Enum.filter(fn {key, _} -> String.starts_with?(key, "departure") end)
|> Enum.reduce(1, fn {_, col}, acc -> Enum.at(own, col) * acc end)
LostKobrakai
I did something different today by using nimble_parsec to parse the input data. But otherwise especially part 2 feels like awkwardly procedural. I could make it work, but it’s just an unnerving amount of nested iterations over lists.







