LoginSignup
7
1

闘魂Elixir ── Advent of code 2023 Day 11 Part 2 を Livebook で楽しむ

Posted at

$\huge{元氣ですかーーーーッ!!!}$
$\huge{元氣があればなんでもできる!}$

$\huge{闘魂とは己に打ち克つこと。}$
$\huge{そして闘いを通じて己の魂を磨いていく}$
$\huge{ことだと思います}$

はじめに

@torifukukaiou さんの パク リスペクト記事です

Elixir Livebook で Advent of Code 2023 の問題を解いてみます

実装したノートブックはこちら

問題はこちら

Part 1 はこちら

セットアップ

Kino AOC をインストールします

Mix.install([
  {:kino_aoc, "~> 0.1.5"}
])

Kino AOC の使い方はこちらを参照

入力の取得

Day 11 の入力を取得します

スクリーンショット 2023-12-29 12.04.04.png

私の答え

私の答えです。
折りたたんでおきます。
▶を押して開いてください。

details

回答用のモジュールです

入力を扱いやすい形にするための parse と、回答を作るための resolve 関数を持っています

defmodule Resolver do
  def parse(input) do
    orig_space =
      input
      |> String.split("\n")
      |> Enum.with_index()
      |> Enum.map(fn {line, row} ->
        line
        |> String.codepoints()
        |> Enum.with_index()
        |> Enum.filter(fn {cell, _} -> cell == "#" end)
        |> Enum.map(fn {_, col} -> {row, col} end)
      end)
      |> Enum.concat()

    max_row =
      orig_space
      |> Enum.map(fn {row, _} -> row end)
      |> Enum.max()

    max_col =
      orig_space
      |> Enum.map(fn {col, _} -> col end)
      |> Enum.max()

    empty_rows =
      0..max_row
      |> Enum.filter(fn target_row ->
        orig_space
        |> Enum.map(fn {row, _} -> row end)
        |> Enum.member?(target_row)
        |> Kernel.!()
      end)

    empty_cols =
      0..max_col
      |> Enum.filter(fn target_col ->
        orig_space
        |> Enum.map(fn {_, col} -> col end)
        |> Enum.member?(target_col)
        |> Kernel.!()
      end)

    orig_space
    |> Enum.map(fn {row, col} ->
      ex_rows = Enum.count(empty_rows, fn empty_row -> row > empty_row end) * 999999
      ex_cols = Enum.count(empty_cols, fn empty_col -> col > empty_col end) * 999999

      {row + ex_rows, col + ex_cols}
    end)
  end

  def resolve(space) do
    space
    |> Enum.with_index()
    |> Enum.map(fn {{src_row, src_col}, src_index} ->
      space
      |> Enum.with_index()
      |> Enum.map(fn {{dst_row, dst_col}, dst_index} ->
        if src_index >= dst_index do
          0
        else
          abs(dst_row - src_row) + abs(dst_col - src_col)
        end
      end)
      |> Enum.sum()
    end)
    |> Enum.sum()
  end
end

Part 1 と 99.9% 同じ答えです

単純に追加する行を  * 999999 しただけです

まとめ

最短経路が出てくるので、てっきり巡回セールスマン問題でも解かされるのかと思っていたら肩透かしでした

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1