cblavier

cblavier

Advent of Code 2020 - Day 10

Hi, there :wave:

Today, I felt it was way more challenging! I went through part2 thanks to Agent based memoization (without memoization the execution time was :infinity: , after it was 3ms :sunglasses:)

My code:
Part1 / Part2

Most Liked

bossek

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

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

Rainer

Rainer

Yes, morge challenging today, so I didn’t come up with a solution for part 2 yet :stuck_out_tongue:
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)].
LostKobrakai

LostKobrakai

Thanks for mentioning that. I had part 2 working for the examples, but the full input timed out. Then I rebuild it using :digraph and recursively weighted the edges from the end. This again timed out for the puzzle input but not the examples. Turns out keeping track of the already checked vertexes made the test resolve in 0.1 sec.

adamu

adamu

That is pretty elegant, kind of wish I’d thought of going backwards. I don’t think the direction is relevant for whether memoization is free or not though. We both just store the accumulated values in a map, and I go forwards. Anyway, the other solutions are faster :sunglasses:. And @camilleryr’s is faster than @Damirados’ Reddit “optimal” solution, although I don’t pretend to understand it! Very clever.

Name                 ips        average  deviation         median         99th %
camilleryr      105.88 K        9.44 μs    ±75.62%           9 μs          30 μs
reddit           32.58 K       30.69 μs    ±34.14%          28 μs          81 μs
adam             25.09 K       39.86 μs    ±24.96%          37 μs          91 μs
hallski          19.81 K       50.49 μs    ±27.17%          48 μs         126 μs
bossek           19.45 K       51.42 μs    ±27.12%          48 μs         121 μs
kwando           16.00 K       62.51 μs    ±30.45%          58 μs         158 μs

Comparison:
camilleryr      105.88 K
reddit           32.58 K - 3.25x slower +21.25 μs
adam             25.09 K - 4.22x slower +30.41 μs
hallski          19.81 K - 5.35x slower +41.05 μs
bossek           19.45 K - 5.44x slower +41.98 μs
kwando           16.00 K - 6.62x slower +53.07 μs

It would be interesting to see how the Agent answers do, but I’ve looked at this problem for long enough already!

Where Next?

Popular in Challenges Top

Aetherus
This topic is about Day 16 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
bjorng
Note: This topic is to talk about Day 25 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
sb8244
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
bjorng
My solution finishes both parts in 5 seconds on my computer. That time should be possible to reduce by optimizing my rather naive tilt/2 ...
New
stevensonmt
Trying to get more facility with dynamic programming concepts on Leetcode and having an issue I can’t find a way around. It’s a chutes an...
New
shritesh
This was way too easy after the last few days. Simple map, filter and count.
New
New
bjorng
This topic is about Day 9 of the Advent of Code 2021 . We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
cblavier
Hey there :wave: No magic or algorithmic finesse today, I just finished the challenge and I my code is quite slow (1sec for part1, 3se...
New
Aetherus
Today’s problem is really tense. I don’t think I can do it without libgraph.
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
bsollish-terakeet
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
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement