cblavier
Advent of Code 2020 - Day 10
Hi, there ![]()
Today, I felt it was way more challenging! I went through part2 thanks to Agent based memoization (without memoization the execution time was
, after it was 3ms
)
Most Liked
bossek
My solution of part 2 is to calculate result backwards, memoization is then “for free”:
data = "data/10" |> File.read!() |> String.split() |> Enum.map(&String.to_integer/1)
data = Enum.sort([0 | data], :desc)
IO.puts(
Enum.reduce(data, %{(hd(data) + 3) => 1}, fn i, memo ->
Map.put(memo, i, Enum.sum(Enum.map(1..3, &Map.get(memo, i + &1, 0))))
end)[0]
)
camilleryr
You can actually calculate the answer for part two without the need to run any of the possibilities - if you sort your input and reduce that to a list of the number of consecutive digits in a row ( [1, 2, 3, 6] → [3, 1] or [1, 3, 4, 5, 8] → [1, 3, 1]), you can then calculate the number of permutations each ‘block’ will cause and then just find the product of the list
https://github.com/camilleryr/advent20/blob/main/lib/day_10.ex
Rainer
Yes, morge challenging today, so I didn’t come up with a solution for part 2 yet ![]()
Couldn’t decide how I wanna solve it, and then run out of time before work…
Anyway: Heres my part 1 in Erlang:
-module(day10).
-export([run/0]).
run()->
Input = lists:sort(load_file("day10input.txt")),
{part1(Input), part2(Input)}.
part1(Input)->
calc([0|Input], 0, 0).
calc([X,Y|T], Ones, Threes) ->
case Y - X of
1 -> calc([Y|T], Ones + 1, Threes);
3 -> calc([Y|T], Ones, Threes + 1);
_ -> calc([Y|T], Ones, Threes)
end;
calc(_, Ones, Threes)->
Ones * (Threes + 1).
part2(_)-> notimplemented.
load_file(Filename)->
{ok, Binary} = file:read_file(Filename),
StringContent = unicode:characters_to_list(Binary),
[ element(1, string:to_integer(Line)) || Line <- string:split(StringContent, "\n", all)].
Popular in Challenges
Other popular topics
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









