bjorng
Erlang Core Team
Advent of Code 2020 - Day 22
This topic is about Day 22 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
code-shoily
This one was easy, took me awhile to understand the recursion rule for the recursive combat but once I got it, it was pretty straight-forward. I did think about using :queue but felt like trying without first.
defmodule AdventOfCode.Y2020.Day22 do
use AdventOfCode.Helpers.InputReader, year: 2020, day: 22
def run_1, do: input!() |> process() |> play() |> score()
def run_2, do: input!() |> process() |> recursive_combat() |> score()
def process(input) do
input
|> String.split("\n\n")
|> Enum.flat_map(&String.split(&1, ":"))
|> decks()
end
defp score(cards) do
cards
|> Enum.reverse()
|> Enum.with_index(1)
|> Enum.map(fn {val, idx} -> val * idx end)
|> Enum.sum()
end
defp decks([_, player_1, _, player_2]), do: {deck(player_1), deck(player_2)}
defp deck(player), do: Enum.map(String.split(player, "\n", trim: true), &String.to_integer/1)
defp play({[], player_2}), do: player_2
defp play({player_1, []}), do: player_1
defp play({[card_1 | rest_1], [card_2 | rest_2]}) when card_1 > card_2,
do: play({rest_1 ++ [card_1, card_2], rest_2})
defp play({[card_1 | rest_1], [card_2 | rest_2]}),
do: play({rest_1, rest_2 ++ [card_2, card_1]})
defp recursive_combat(decks), do: decks |> recursive_combat([], []) |> elem(1)
defp recursive_combat({[], player_2}, _, _), do: {2, player_2}
defp recursive_combat({player_1, []}, _, _), do: {1, player_1}
defp recursive_combat({[card_1 | rest_1] = a, [card_2 | rest_2] = b}, h1, h2) do
if a in h1 || b in h2 do
{1, a}
else
h1 = [a | h1]
h2 = [b | h2]
if card_1 <= length(rest_1) && card_2 <= length(rest_2) do
case recursive_combat({Enum.take(rest_1, card_1), Enum.take(rest_2, card_2)}, [], []) do
{1, _} -> recursive_combat({rest_1 ++ [card_1, card_2], rest_2}, h1, h2)
{2, _} -> recursive_combat({rest_1, rest_2 ++ [card_2, card_1]}, h1, h2)
end
else
case card_1 > card_2 do
true -> recursive_combat({rest_1 ++ [card_1, card_2], rest_2}, h1, h2)
_ -> recursive_combat({rest_1, rest_2 ++ [card_2, card_1]}, h1, h2)
end
end
end
end
end
1
camilleryr
I got mine down to ~225ms, but I dont see any other obvious ways to optimize from here - I tried to memoize decks between games, but that increased the run time.
1
code-shoily
That rule got me too. Chanting that == to < made big difference!
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
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
Continuation of Advent of Code 2022:christmas_tree:, Day 1:
Day 2!
Leaderboard:
New
Today’s problem is really tense. I don’t think I can do it without libgraph.
New
Since I started using Elixir, I have benefited greatly from being able to study various open-source projects. The codebase of LiveBook, i...
New
A frustrating one for me. I spent a long time trying to understand why some combinations resulted in fewer presses and struggled to keep ...
New
Hello all, hopefully I post this before someone else does and I don’t dupe.
IMO Day 4 was much easier than Day 3 (yay, I can sleep befor...
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 by the Moderators: This topic is for general discussion about the Advent of Code 2018.
To prevent people from being spoiled about s...
New
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby.
Anyway, here is my solution: ...
New
Other popular topics
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)?
Would
mix ecto.rollback -v 200809061...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
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
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
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








