bjorng

bjorng

Erlang Core Team

Advent of Code 2021 - Day 6

This topic is about Day 6 of the Advent of Code 2021.

We have a private leaderboard (shared with users of Erlang Forums ):

https://adventofcode.com/2021/leaderboard/private/view/370884

The entry code is:
370884-a6a71927

Most Liked

ruslandoga

ruslandoga

I went with representing the frequencies as a tuple and a simulation step was:

defp simulate({s0, s1, s2, s3, s4, s5, s6, s7, s8}, days) when days > 0 do
  simulate({s1, s2, s3, s4, s5, s6, s7 + s0, s8, s0}, days - 1)
end

Full solution

It’d be nice to see an Nx solution here, it fits this problem very nicely.

wasi0013

wasi0013

I enjoyed this one! :smiley:

defmodule Aoc.Y2021.Day06 do
  @moduledoc """
  Solved https://adventofcode.com/2021/day/6
  """
  import Aoc.Helper.IO

  def run_part1(), do: get_input() |> solve_part1()
  def run_part2(), do: get_input() |> solve_part2()

  def solve_part1(data), do: solve(data, 80)
  def solve_part2(data), do: solve(data, 256)

  def solve(data, days), do: data |> fish_count() |> simulate(days) |> Enum.sum()
  def fish_count(data), do: Enum.map(0..8, fn n -> Map.get(Enum.frequencies(data), n, 0) end)

  def simulate(fish_count, 0), do: fish_count

  def simulate([zeroth, first, second, third, fourth, fifth, sixth, seventh, eighth], days) do
    simulate([first, second, third, fourth, fifth, sixth, seventh + zeroth, eighth, zeroth], days - 1)
  end

  defp get_input(), do: get_integer_input("2021", "06", ",")
end


jarimatti

jarimatti

I think there is an elegant mathematical way, using linear algebra: the amount of fish that have specific day can be modeled as a vector, where element 0 is the amount of fish that have 0 days left. Total number of fish at any given day is the sum of elements of that vector.

We can multiply that vector with a matrix K, that produces the next day’s fish counts. Here’s what K could look like (apologies for Octave/Matlab syntax, I used that to verify the approach):

K0 = [
  0 0 0 0 0 0 1 0 1;
  1 0 0 0 0 0 0 0 0;
  0 1 0 0 0 0 0 0 0;
  0 0 1 0 0 0 0 0 0;
  0 0 0 1 0 0 0 0 0;
  0 0 0 0 1 0 0 0 0;
  0 0 0 0 0 1 0 0 0;
  0 0 0 0 0 0 1 0 0;
  0 0 0 0 0 0 0 1 0
];

# Fun fact: matrix for computing any initial condition to day 80 is:
K80 = K0 ^ 80;

# Get day 1 from day 0 counts per day:
v1 = v0 * K;

# Get day 80 from day 0 counts per day:
v80 = v0 * K80;

Full code here for my input: aoc2021_day6.m

I’ve yet to figure out how to do this in Elixir fluently and it seems it’s time to learn some Nx. :grin:

jarimatti

jarimatti

OK, that was more fun than expected. (pun not intended)

Nx solution module over here: Aoc2021.Day6.LinAlg

Nx multiply and especially Nx.power are done by element, so I had to resort to reduce when multiplying the tensors. Otherwise it pretty much follows the Octave style. I like Nx so far, this was easy to install and play around with.

qhwa

qhwa

Thank you for sharing this! This matrix is awesome!

I watched @josevalim 's video on day 6, and oh man, it was so amazing and blew my mind. In the end, he tried the same matrix idea with Nx. It was so delightful to watch him thinking, solving, and explaining.

Where Next?

Popular in Challenges Top

ehayun
I have 2 arrays: a1 can be any combination of value or nil like that a1 = [1,nil,3] and array 2 the same a2 = [4,2, nil] How do I com...
New
bjorng
This topic is about Day 5 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums ): https://adve...
New
sukhmeetsd
All in all, from what I understand, it is better not to use GenServer.cast when we want some concurrent operations to happen for sure, be...
New
bjorng
Note: This topic is to talk about Day 9 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
bjorng
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
christhekeele
Setting this down for the night, as after a quick naive solve for quick part 1 I realize that part 2 is by design computationally expensi...
New
bjorng
Note: This topic is to talk about Day 18 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can joi...
New
bjorng
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
seeplusplus
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
rvnash
Anyone have a solution to Part 2 today? Part 1 was straight forward, but I can’t figure out a programatic way to do part 2. I understand ...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
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

We're in Beta

About us Mission Statement