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

Day 18

2023/day18.livemd

Day 18

Mix.install([:kino_aoc])

Section

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2023", "18", System.fetch_env!("LB_ADVENT_OF_CODE_SESSION"))
{:ok,
 "L 5 (#0760b2)\nU 4 (#058b53)\nL 3 (#d05a12)\nD 4 (#058b51)\nL 6 (#12ee82)\nU 6 (#63c403)\nL 4 (#065a12)\nU 8 (#a44373)\nL 7 (#7581c2)\nU 7 (#334ec3)\nL 3 (#35e792)\nU 5 (#079963)\nL 3 (#1737b0)\nU 4 (#29a7a3)\nL 2 (#705a02)\nU 5 (#7151e3)\nL 4 (#705a00)\nU 4 (#43f5a3)\nL 5 (#1737b2)\nU 8 (#61a8c3)\nL 5 (#6005d2)\nD 3 (#20f4c1)\nL 3 (#4aa812)\nD 3 (#8163b1)\nL 4 (#80ac22)\nD 4 (#5e64c1)\nL 7 (#cb5430)\nD 5 (#7ac2d1)\nR 7 (#920132)\nD 6 (#a44371)\nL 6 (#944582)\nU 3 (#bc3423)\nL 5 (#71c1b2)\nU 4 (#88e6f3)\nL 4 (#0e35f2)\nU 5 (#11fa31)\nL 8 (#6ab312)\nU 5 (#5c3df1)\nR 8 (#40be22)\nU 3 (#5c3df3)\nR 9 (#42e412)\nU 6 (#11fa33)\nR 5 (#178f02)\nU 10 (#88e6f1)\nL 2 (#cff5f2)\nU 6 (#41fb73)\nL 5 (#247782)\nU 4 (#8d6d73)\nL 5 (#139492)\nU 8 (#6360d3)\nR 4 (#1688c2)\nU 2 (#28eb43)\nR 5 (#7d6230)\nU 6 (#769093)\nR 3 (#7d6232)\nU 5 (#487383)\nL 5 (#4fe4c2)\nU 4 (#784971)\nL 7 (#649a42)\nU 3 (#81c8d1)\nL 6 (#3e8422)\nU 5 (#2e26f3)\nL 5 (#880bd2)\nU 4 (#cbeb53)\nL 6 (#3c3a82)\nU 3 (#990203)\nL 8 (#18ce92)\nU 6 (#3bd723)\nL 10 (#8b17c2)\nU 5 (#931f03)\nR 7 (#56b4b2)\nU 3 (#1c3d03)\nR 2 (#693490)\nU 7 (#a5d813)\nL 4 (#693492)\nU 7 (#454523)\nR 8 (#749e20)\nU 3 (#b6ecd3)\nL 8 (#1e69b0)\nU 6 (#0cd813)\nR 4 (#a286d0)\nU 8 (#8249a3)\nR 6 (#c431e0)\nU 10 (#64b5d3)\nR 3 (#26fee0)\nU 7 (#3a6403)\nR 7 (#44c0a2)\nU 8 (#574291)\nR 2 (#9bf502)\nU 5 (#574293)\nR 5 (#0a7b22)\nU 8 (#12e553)\nR 2 (#67c0e0)\nU 3 (#ab6f63)\nR 7 (#922b60)\nD 6 (#680283)\nR 5 (#637c90)\nD 5 (#b10343)\nR 3 (#954ed0)\nD 8 (#254333)\nL 8 (#3f16a0)\nD 3 (#55f2c3)\nR 4 (#2465e0)\nD 9 (#6b7461)\nR 6 (#8b2b00)\nD 7 (#6b7463)\nR 5 (#7ae9f0)\nD 3 (#584e33)\nR 3 (#4fd2d2)\nD 4 (#30e451)\nR 3 (#682c22)\nD 3 (#30e453)\nR 4 (#727be2)\nD 8 (#85b173)\nL 4 (#3a8c50)\nD 3 (#004a23)\nR 2 (#6eef60)\nD 3 (#92b8d3)\nR 5 (#bdb140)\nU 8 (#8500b3)\nR 5 (#2e42f2)\nD 8 (#531953)\nR 3 (#9358a2)\nD 5 (#14d4b3)\nR 6 (#b19aa2)\nD 8 (#14d4b1)\nR 8 (#3e0d32)\nU 8 (#274023)\nR 3 (#b2d1a2)\nU 3 (#274021)\nR 8 (#206d02)\nU 6 (#2d02d3)\nR 4 (#0bd782)\nU 3 (#483403)\nR 9 (#7d6fc0)\nU 5 (#717513)\nR 9 (#5e63a0)\nU 6 (#614071)\nR 6 (#6df500)\nU 9 (#6f14e1)\nR 5 (#9229d0)\nU 5 (#6f14e3)\nL 5 (#0993b0)\nU 4 (#614073)\nL 3 (#aad3a0)\nU 7 (#2aed13)\nL 7 (#117f30)\nU 8 (#b185d3)\nL 3 (#a39640)\nU 10 (#a408b1)\nL 5 (#1e31b0)\nU 4 (#5af491)\nL 3 (#43ff50)\nU 4 (#125121)\nR 5 (#2321b0)\nU 4 (#4a6de1)\nR 6 (#80c430)\nU 5 (#4a6de3)\nL 5 (#8fe3b0)\nU 4 (#483d91)\nR 8 (#012050)\nU 3 (#3730e1)\nR 5 (#d36d12)\nU 8 (#7d5761)\nR 7 (#d36d10)\nU 7 (#21b1c1)\nR 9 (#8ea5e2)\nU 7 (#602a01)\nR 8 (#aae3f2)\nU 5 (#8d2981)\nR 5 (#263f72)\nU 5 (#2a3471)\nR 7 (#263f70)\nU 2 (#88ea21)\nR 3 (#3f5f62)\nU 6 (#5e1cc1)\nR 5 (#adffb0)\nU 6 (#104941)\nR 5 (#073230)\nU 4 (#25be03)\nL 9 (#1a2fd0)\nU 5 (#3d1233)\nR 9 (#879540)\nU 4 (#5c9671)\nL 2 (#6ad9e0)\nU 8 (#5c9673)\nL 7 (#3bb750)\nU 3 (#3d1231)\nR 11 (#080ca0)\nU 4 (#25be01)\nR 4 (#50a100)\nU 5 (#163541)\nL 5 (#27ff80)\nU 3 (#ab8481)\nR 4 (#549890)\nU 7 (#010511)\nL 4 (#749c72)\nU 4 (#46cfe1)\nL 5 (#a03822)\nD 5 (#594361)\nL 2 (#12a9f0)\nD 9 (#509f91)\nL 3 (#c051f0)\nU 3 (#509f93)\nL 7 (#41d8b0)\nD 3 (#80af51)\nL 3 (#549892)\nD 9 (#29c701)\nL 5 (#58fe00)\nD 5 (#4ef833)\nR 5 (#4ad860)\nD 7 (#aa4b13)\nL 7 (#6d6810)\nD 8 (#774e21)\nL 6 (#631140)\nU 8 (#4c9b31)\nL 4 (#4a8a50)\nD 3 (#61b9d1)\nL 6 (#665fd0)\nD 5 (#4edd51)\nR 6 (#46f5d0)\nD 3 (#109bd1)\nL 6 (#896f50)\nD 5 (#3ab8e1)\nL 3 (#6f6ce0)\nD 4 (#5acd11)\nL 5 (#1d15b2)\nU 4 (#032e23)\nL 10 (#8d6c92)\nU 3 (#032e21)\nR 10 (#4e59f2)\nU 5 (#70a541)\nL 5 (#1102a0)\nU 8 (#8b66f1)\nL 5 (#871710)\nU 10 (#be4e51)\nR 3 (#871712)\nU 11 (#1182c1)\nR 3 (#1102a2)\nD 9 (#493441)\nR 8 (#7a6b80)\nD 4 (#c3daa1)\nR 3 (#8a38d0)\nU 3 (#07ac71)\nR 4 (#131b90)\nU 5 (#34c6e1)\nR 4 (#914cf2)\nU 5 (#0e9af1)\nR 3 (#6c62d2)\nU 3 (#3af801)\nL 6 (#0018c0)\nU 7 (#2b7361)\nL 4 (#55fb20)\nU 5 (#8a33c1)\nR 4 (#a79be0)\nU 4 (#59acf1)\nR 7 (#bb8240)\nD 5 (#77ca71)\nR 7 (#894bd0)\nU 5 (#a12ff1)\nR 3 (#437630)\nD 4 (#1dbba1)\nR 10 (#4c87b0)\nU 6 (#9ea131)\nR 2 (#4c87b2)\nU 4 (#19f7e1)\nL 11 (#85c770)\nU 4 (#220631)\nL 9 (#224540)\nU 5 (#d4ac83)\nL 4 (#421610)\nU 3 (#23ae63)\nL 9 (#8259c0)\nD 4 (#1b4b51)\nR 6 (#b84e80)\nD 11 (#9b8e41)\nL 6 (#29c310)\nD 4 (#9cba93)\nL 6 (#2e33e0)\nU 10 (#1d8b03)\nL 5 (#2578e2)\nU 7 (#0c7911)\nL 2 (#6112e2)\nU 2 (#0c7913)\nL 7 (#621cb2)\nD 8 (#4164f3)\nL 4 (#080dd2)\nU 11 (#574773)\nR 7 (#6ab252)\nU 3 (#4224b3)\nR 3 (#898930)\nU 2 (#c8" <> ...}
# puzzle_input =
"""
R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)
"""
"R 6 (#70c710)\nD 5 (#0dc571)\nL 2 (#5713f0)\nD 2 (#d2c081)\nR 2 (#59c680)\nD 2 (#411b91)\nL 5 (#8ceee2)\nU 2 (#caa173)\nL 1 (#1b58a2)\nU 2 (#caa171)\nR 2 (#7807d2)\nU 3 (#a77fa3)\nL 2 (#015232)\nU 2 (#7a21e3)\n"
commands =
  puzzle_input
  |> String.split("\n", trim: true)
  |> Enum.map(fn <> <> " " <> rest ->
    {len, " (#" <> <> <> ")"} = Integer.parse(rest)
    {dir, len, hex}
  end)
[
  {76, 5, "0760b2"},
  {85, 4, "058b53"},
  {76, 3, "d05a12"},
  {68, 4, "058b51"},
  {76, 6, "12ee82"},
  {85, 6, "63c403"},
  {76, 4, "065a12"},
  {85, 8, "a44373"},
  {76, 7, "7581c2"},
  {85, 7, "334ec3"},
  {76, 3, "35e792"},
  {85, 5, "079963"},
  {76, 3, "1737b0"},
  {85, 4, "29a7a3"},
  {76, 2, "705a02"},
  {85, 5, "7151e3"},
  {76, 4, "705a00"},
  {85, 4, "43f5a3"},
  {76, 5, "1737b2"},
  {85, 8, "61a8c3"},
  {76, 5, "6005d2"},
  {68, 3, "20f4c1"},
  {76, 3, "4aa812"},
  {68, 3, "8163b1"},
  {76, 4, "80ac22"},
  {68, 4, "5e64c1"},
  {76, 7, "cb5430"},
  {68, 5, "7ac2d1"},
  {82, 7, "920132"},
  {68, 6, "a44371"},
  {76, 6, "944582"},
  {85, 3, "bc3423"},
  {76, 5, "71c1b2"},
  {85, 4, "88e6f3"},
  {76, 4, "0e35f2"},
  {85, 5, "11fa31"},
  {76, 8, "6ab312"},
  {85, 5, "5c3df1"},
  {82, 8, "40be22"},
  {85, 3, "5c3df3"},
  {82, 9, "42e412"},
  {85, 6, "11fa33"},
  {82, 5, "178f02"},
  {85, 10, "88e6f1"},
  {76, 2, "cff5f2"},
  {85, 6, "41fb73"},
  {76, 5, "247782"},
  {85, 4, ...},
  {76, ...},
  {...},
  ...
]

Part 1

dirs = %{
  ?U => {0, -1},
  ?D => {0, 1},
  ?L => {-1, 0},
  ?R => {1, 0}
}

{points, {_, circ}} =
  Enum.map_reduce(commands, {{0, 0}, 0}, fn
    {dir, d, _}, {{x, y}, l} ->
      {dx, dy} = dirs[dir]
      {{x, y}, {{x + dx * d, y + dy * d}, l + d}}
  end)
{[
   {0, 0},
   {-5, 0},
   {-5, -4},
   {-8, -4},
   {-8, 0},
   {-14, 0},
   {-14, -6},
   {-18, -6},
   {-18, -14},
   {-25, -14},
   {-25, -21},
   {-28, -21},
   {-28, -26},
   {-31, -26},
   {-31, -30},
   {-33, -30},
   {-33, -35},
   {-37, -35},
   {-37, -39},
   {-42, -39},
   {-42, -47},
   {-47, -47},
   {-47, -44},
   {-50, -44},
   {-50, -41},
   {-54, -41},
   {-54, -37},
   {-61, -37},
   {-61, -32},
   {-54, -32},
   {-54, -26},
   {-60, -26},
   {-60, -29},
   {-65, -29},
   {-65, -33},
   {-69, -33},
   {-69, -38},
   {-77, -38},
   {-77, -43},
   {-69, -43},
   {-69, -46},
   {-60, -46},
   {-60, -52},
   {-55, -52},
   {-55, -62},
   {-57, -62},
   {-57, -68},
   {-62, ...},
   {...},
   ...
 ], {{0, 0}, 3518}}
area =
  points
  |> Enum.chunk_every(2, 1, [{0, 0}])
  |> Enum.map(fn [{x1, y1}, {x2, y2}] ->
    x1 * y2 - x2 * y1
  end)
  |> Enum.sum()
  |> abs()
  |> div(2)

area + div(circ, 2) + 1
49897

Part 2

dirs = %{
  ?3 => {0, -1},
  ?1 => {0, 1},
  ?2 => {-1, 0},
  ?0 => {1, 0}
}

{points, {_, circ}} =
  Enum.map_reduce(commands, {{0, 0}, 0}, fn
    {_, _, <>}, {{x, y}, l} ->
      {dx, dy} = dirs[dir]
      d = String.to_integer(d, 16)
      {{x, y}, {{x + dx * d, y + dy * d}, l + d}}
  end)
{[
   {0, 0},
   {-30219, 0},
   {-30219, -22709},
   {-883628, -22709},
   {-883628, 0},
   {-961172, 0},
   {-961172, -408640},
   {-987189, -408640},
   {-987189, -1081463},
   {-1468497, -1081463},
   {-1468497, -1291619},
   {-1689290, -1291619},
   {-1689290, -1322745},
   {-1594191, -1322745},
   {-1594191, -1493363},
   {-2054383, -1493363},
   {-2054383, -1957521},
   {-1594191, -1957521},
   {-1594191, -2235883},
   {-1689290, -2235883},
   {-1689290, -2635895},
   {-2082599, -2635895},
   {-2082599, -2500907},
   {-2388392, -2500907},
   {-2388392, -1970928},
   {-2915434, -1970928},
   {-2915434, -1584292},
   {-2082599, -1584292},
   {-2082599, -1081463},
   {-2680634, -1081463},
   {-2680634, -408640},
   {-3287954, -408640},
   {-3287954, -1179522},
   {-3753901, -1179522},
   {-3753901, -1740273},
   {-3812108, -1740273},
   {-3812108, -1666638},
   {-4249149, -1666638},
   {-4249149, -1288815},
   {-4514335, -1288815},
   {-4514335, -1666638},
   {-4788320, -1666638},
   {-4788320, -1740273},
   {-4884816, -1740273},
   {-4884816, -1179522},
   {-5736623, -1179522},
   {-5736623, -1449785},
   {-5885991, ...},
   {...},
   ...
 ], {{0, 0}, 237488828}}
area =
  points
  |> Enum.chunk_every(2, 1, [{0, 0}])
  |> Enum.map(fn [{x1, y1}, {x2, y2}] ->
    x1 * y2 - x2 * y1
  end)
  |> Enum.sum()
  |> abs()
  |> div(2)

area + div(circ, 2) + 1
194033958221830