Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Day 15 - Advent of Code 2023

lib/advent_of_code/2023/day-15.livemd

Day 15 - Advent of Code 2023

Mix.install([:kino, :benchee])

Links

Input

input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"fzl-,tcjp=8,vkjr=9,xs-,jktcpk=3,gzp-,kfsxsd-,zxkv=7,fxz-,pj=7,mhbdch=7,xlss-,smk-,ppz-,kqggd-,dqh=7,gmv=6,tjjfm=2,gbv=5,gn-,ld=7,jdr-,phq=3,rd-,qz=3,sh-,gzsb=2,glrt=7,vjkrjg-,gjqmpc=7,qnx=7,mf=1,tnrm-,lppg-,gnvx=1,rv-,vnt-,bst-,gkb-,tl=1,bggff-,lm=8,mqq=7,hr-,gkb-,fspkn=2,tjjfm-,shshb=3,qfczq-,zk-,zcsc-,lnrjh=2,fz-,bkb-,rg-,bcfzn=6,xsbb=5,sm=3,rqs-,rvlv=9,pvh=9,rl-,zrb=9,lftk=1,cdn-,zp=8,hc-,mf-,hp-,zk-,dbj=5,tm=9,zx-,gmp=4,gcq-,mxq-,lvh-,lkhb-,vn=9,gzsb=6,xlss=9,drkhc=8,rgqg=1,hkr-,fxz=7,xsm-,fvmf=4,hsm=7,dlrss=9,rv-,kj-,ndtc-,zjf-,zj=9,kv=5,sxvv-,hj-,mltm-,qggz-,mntr-,td=5,lppg=1,xqb-,brt-,nm-,vlp=7,zcjnv=6,chh-,dd=2,fh-,hxfkx=6,hj=6,rzk=7,skpq-,rxb=1,fn=1,nc=1,vqq-,ndq-,xtgd=6,qlgv-,dl-,xln-,rqs=5,vpmmf=1,llvv-,hzp=6,dnbql=3,rqz=5,nfhb=7,vdb-,gmp-,hskv-,xs-,vk-,xll-,ng=2,xb-,mjjph-,tss-,lsd-,qsj-,tbdb-,tdnm=6,bv-,qz=5,zp=8,jn-,fh-,dqh=1,rgrd=5,kqggd-,ddccc-,gdx=3,br-,cnb=4,tfx-,scp=5,rgch-,xltvn=4,xdblm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jttq" <> ...

Solution

defmodule Day15 do
  defdelegate parse(input), to: __MODULE__.Input

  def part1(input) do
    input
    |> parse()
    |> Stream.map(&amp;hash/1)
    |> Enum.sum()
  end

  defp hash(list) do
    Enum.reduce(list, 0, fn char, acc ->
      rem((acc + char) * 17, 256)
    end)
  end

  def part2(input) do
    input
    |> parse()
    |> hashmap()
    |> calc_focusing_powers()
    |> List.flatten()
    |> Enum.sum()
  end

  defp hashmap(steps) do
    Enum.reduce(steps, %{}, fn step, boxes ->
      case Enum.chunk_by(step, &amp;(&amp;1 in ~c"=-")) do
        [label, [?=], focal] ->
          add_label(boxes, label, focal)

        [label, [?-]] ->
          remove_label(boxes, label)
      end
    end)
  end

  defp add_label(boxes, label, focal) do
    entry = {label, focal}

    Map.update(boxes, hash(label), [entry], fn acc ->
      List.keystore(acc, label, 0, entry)
    end)
  end

  defp remove_label(boxes, label) do
    Map.update(boxes, hash(label), [], &amp;List.keydelete(&amp;1, label, 0))
  end

  defp calc_focusing_powers(boxes) do
    Enum.map(boxes, fn
      {_, []} ->
        0

      {box_id, box} ->
        box
        |> Enum.with_index(1)
        |> Enum.map(fn {{_label, focal_length}, index} ->
          focal_length = String.to_integer(to_string(focal_length))

          (box_id + 1) * index * focal_length
        end)
    end)
  end

  defmodule Input do
    def parse(input) do
      input
      |> String.replace("\n", "")
      |> String.splitter(",", trim: true)
      |> Stream.map(&amp;String.to_charlist/1)
    end
  end
end
{:module, Day15, <<70, 79, 82, 49, 0, 0, 18, ...>>,
 {:module, Day15.Input, <<70, 79, 82, ...>>, {:parse, 1}}}

Run the HASH algorithm on each step in the initialization sequence. What is the sum of the results?

Your puzzle answer was 511257.

Day15.part1(input)
511257

With the help of an over-enthusiastic reindeer in a hard hat, follow the initialization sequence. What is the focusing power of the resulting lens configuration?

Your puzzle answer was 239484.

Day15.part2(input)
239484

Both parts of this puzzle are complete! They provide two gold stars: **

At this point, you should return to your Advent calendar and try another puzzle.

If you still want to see it, you can get your puzzle input.

Tests

ExUnit.start(auto_run: false)

defmodule Day15Test do
  use ExUnit.Case, async: false

  setup_all do
    [
      input: "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7"
    ]
  end

  describe "part1/1" do
    test "returns expected value", %{input: input} do
      assert Day15.part1(input) == 1320
    end
  end

  describe "part2/1" do
    test "returns expected value", %{input: input} do
      assert Day15.part2(input) == 145
    end
  end
end

ExUnit.run()
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
2 tests, 0 failures

Randomized with seed 922499
%{total: 2, failures: 0, excluded: 0, skipped: 0}

Benchmarking

defmodule Benchmarking do
  # https://github.com/bencheeorg/benchee
  def run(input) do
    Benchee.run(
      %{
        "Part 1" => fn -> Day15.part1(input) end,
        "Part 2" => fn -> Day15.part2(input) end
      },
      memory_time: 2,
      reduction_time: 2
    )

    nil
  end
end
{:module, Benchmarking, <<70, 79, 82, 49, 0, 0, 7, ...>>, {:run, 1}}
Benchmarking.run(input)
Operating System: macOS
CPU Information: Apple M1 Pro
Number of Available Cores: 10
Available memory: 32 GB
Elixir 1.15.6
Erlang 26.1

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 2 s
reduction time: 2 s
parallel: 1
inputs: none specified
Estimated total run time: 22 s

Benchmarking Part 1 ...
Benchmarking Part 2 ...

Name             ips        average  deviation         median         99th %
Part 1        1.95 K        0.51 ms     ±5.72%        0.51 ms        0.57 ms
Part 2        0.43 K        2.35 ms    ±10.70%        2.23 ms        2.91 ms

Comparison: 
Part 1        1.95 K
Part 2        0.43 K - 4.59x slower +1.84 ms

Memory usage statistics:

Name           average  deviation         median         99th %
Part 1         1.29 MB     ±0.00%        1.29 MB        1.29 MB
Part 2         5.89 MB     ±0.01%        5.89 MB        5.89 MB

Comparison: 
Part 1         1.29 MB
Part 2         5.89 MB - 4.57x memory usage +4.60 MB

Reduction count statistics:

Name   Reduction count
Part 1        137.41 K
Part 2        431.04 K - 3.14x reduction count +293.62 K

**All measurements for reduction count were the same**
nil