Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

<!-- livebook:{"persist_outputs":true} --> # Advent of Code 2023 - Day 16 ## Sample ```elixir sample = ~S( .|...\.... |.-.\..... .....|-... ........|. .......... .........\ ..../.\\.. .-.-/..|.. .|....-|.\ ..//.|.... ) ``` <!-- livebook:{"output":true} --> ``` "\n.|...\\....\n|.-.\\.....\n.....|-...\n........|.\n..........\n.........\\\n..../.\\\\..\n.-.-/..|..\n.|....-|.\\\n..//.|....\n" ``` ## Input ```elixir input = ~S( \-....|\......|...............................\......-../......./...\........|......|......................... .........-.....-............|.................-......|.\\......-...................-..|...\................... ......./.|.............-.....-......................................./................\........\.............. ....|.-.........\..........|.......--.........../....|\....-.........|................./..-.................|. ...-......./...../....\...................-......../\.../...............\.............-....................... ...|............\.-......./...........|......-....................-..-.../....../......\...\.../.............. ..|..|..\.....................\..........|...\.|......................\......|....|....|.\.-......-...|....-.. ......../........................-......-.............\\.-............................/|........|../.......... -..........................|.......-.../..............|...................\................|........-...../... .|............\.//.....-..........-....\......./...........................|........................|......... .............................................-................................\..-.\................/........\ ........|........................-....\..........-.........-..../....\../....\.\........\.....\.|............. .................\..|...........-...|......../................|.-..........-./...........|.--................. .......................-................\............\..|.....\....\....|\.....\.....-...\.....-.|............ ..........................\....|.../........../..........//........\\......................................... ........................\.|..\........./.........../...||.............../................../.........-........ /...........................|.....-..../../........./.\.....................|....../.|.....|.................. ....................\.../-|..../..................../.....|..............\./.....-.........../.--......../|.|. ...-........\.....|......|....|.-............................-.....|./.......-.....-.....|-.....|............. ....../............................................./..|....-..................\.....-.................//..... \.........................|....................................................../...../...\.........-..-..... ....-../..\\.......-......../...\.................................\....-.............\.........\.....-........ ............/.........../...........\....................\......../.....\.................................|../ .......\.........-....|..-|..|..............\............/.................................\......./.......... ................./..........................................\...........-|......................./.........\.. |.......................-..../-/.......-........\.........................................../..../.......-.... .\.............|.........|...--....-..|..-..............................................................|\.-|. .\....\...-\.....|./..........................\..............-......................\.....|................... .....|......./.|......................|.................................\........./..../..........\........... /..-...................|.......|.....-..........|...........\...-............-......|.|...................../. ....................\..../.............|............|...............................-....|.-.........\....|... ...|\.\.......\......\.....|..|.......|...\........................|.................-..-...........-......... ............../\-.|.......-...\..........-............\.../...../..|......\................./............|.... ..........|............\..../........-.............\./....../.........................../.-................... .-.\/............................./......|....\........|....../............\......-....../.........\.......... ........./......../../.................\...-.\.........\............./../.....-............\.................. \.\.......-..................-........-\.....|....\-............/...\............../................./........ .........|.................-......\....\......./....|/................-.....\...........|\......|............. .......|........\...\......................-...--........../......-...........\|...............|..\......|.... ........\.................\...../....|.|..................\.....................\...........\.....-.-......... .....\/................/...-.-...............\...................................\....|.|.-|................/. .......\............\............\-............/......\....../.........|......-......\..-....-...-.........\.. .............../......................................|....\.............../..|................../...-...|.... .........\.../../.........................../..-........./.-...\.......-..........|.-...../..../........../... ...|.......................|./..................-................\...|.....\.-................\./......\...... |..../........|...../...................-./.....-...........|.....................-.......-..-................ .../..-.......-...........|..-...............--..............||.../........................................... ....|../...\.................../..../-..-...|.-.............\......................-....\.......-.....-....... .\.........-....-.....|........-..............................|.........................................\....| ..|......................./.........\.......................\...............-.....././...\..|....-....././.... ...--.|.......................\..|......\|........-....||../....-........|.........\.......-..../..\....-..... -.\.........\...................-../....|............-.............../.............\......-....\.............. ............\....-.....\.|...............-.......-.|..............\...........................|..-.../........ ................................................\.........-....................-.......\...................... .\.....\........................|....|......-...........-\...|/...................../......................... ///.-..\.........\......./....|..........................\.|/.....................-...........-\....|..|...... -...........|.....-/...............................|.....|....................................\......-......\/ ................\/............................................/.......................-........./............. ........\.....-..\..............|....................|.....\........-../....../...........\-.........-........ ...../......../.........|.\........../.............\.............../......-.........................-\.../.-.. .......................\..\......../|...|.....-..........................-.|.......\....................-..... ..\......-/........./....|..\.................|...-..../|.................................-................... .............-..........-....../............................................../..........|.................... .././.../.........\..\....|....||...............|./....-...-...............|/.......................-......... ...............-...../.\.....\............\...............................-.-...........\.............-|...... ....\..............-............/..\................................\-.....-...........................\...... .....-.....|.-.........../.........|...................../..................\..../.......\.-./.............../ ..\-..-....|................\...../................................|....|.|........./...........|....|........ .............|..............................-.\........../....../......\.|.......-.......-.................... ........................./.............|.|........|..-............-../........-../....|.......|............-.. ......./.........\.\..\..../.-.-..|./........-.|.............../......./......|....-.-...../...........\...... ................./.....-....|........-\..\....\...................../....................................../.. .|.......|./.../.......|.-..........-................-...........\.......-./......./.......................... .......-..............................\.....\..-\/.|.-....................../.............|..........-...-.... ............/..-..........\..\/.\.../...........-................-..........\.|....|../-....................-. .....\......|.......|................|..\.|......\..-|-..../...........................................\...\.. ........../-.|........-..-........\......|...\.......\...|................................|......-............ ..|.......\....../......................||......\........-....\......|............-............/.....\.../.... ..................../...\.-............\......-........\...............-..\.....|............................. ..........|.-....|....|./-.....|................../.......\......-...........././...|-...........|....-....... ....../...\................./.....-...................|-................/.-...................|.........|.../. ...........................-.................|../..|....../..........-....................../................. ......../..\...../.\...\|...../.........\..........................-...\.......\...........\................./ ......-......|........./.../..|.....................|.......|...-..............-.\.....-./...\.......-..|.\... ....|...-....................|....|....|..............\..|-......|...|...........................-............ .......................|........-......\-...........-../..................../................./............... .......|....-\.\...|...\..............--...-.......-..|...\........|\.........................-......../....\. ...\..........................-.................-......................-......\........-...................... ..|.....................|.-../-....\....-................|....................../...............|.|.........|. .\/.........-.....\............................|.............|....................\........................... ....-....................\....-....-..\.......\....................././......|.......................\........ ./.......-...............-..............-...-...........................-........|...............\-.......|..| .....-................................/.|....-/...\..-.......-..........|.....-...|..........|................ .....-..........................|...-.......|...../.\...-.........-........-\............-....-...../.../..... ......./|\...........-...............\......./.........\...................\.....\............../.|.\........\ .\/.............|...........................\-|........|..-.....................|.......................|..... /....|..................................|......................\..-\.....|..|......................|....|..... ..................................-....../-..............\.........................................-../....\.. .......................||......./....................../.../....\.................................\.../....... ...............-..........--........\............................../...-/............/..........-............. ......../...........|....................-................|..../.............../......./..................\.\. ....\.../|..\..........|...........-.......................................................................\.. ......\...|/..\...............|......\.................../................../....\........../......-.......... ..............-....\............/.....|../...\....|.|..........\..........|................/..........|....... ..../\.......................-................\......-............\./......................|....|..........|.. ..............|................|\.......\..-.../................/............................|................ ...................-..................................................-.......|.\.........|................... .................\....|.....\/......-........................\..................|............................. ......................................|...........\...-.....|.....|.............../.....|..\.../.............. .........../............................/...............|.....................-....\..................-....... ) ``` <!-- livebook:{"output":true} --> ``` "\n\\-....|\\......|...............................\\......-../......./...\\........|......|.........................\n.........-.....-............|.................-......|.\\\\......-...................-..|...\\...................\n......./.|.............-.....-......................................./................\\........\\..............\n....|.-.........\\..........|.......--.........../....|\\....-.........|................./..-.................|.\n...-......./...../....\\...................-......../\\.../...............\\.............-.......................\n...|............\\.-......./...........|......-....................-..-.../....../......\\...\\.../..............\n..|..|..\\.....................\\..........|...\\.|......................\\......|....|....|.\\.-......-...|....-..\n......../........................-......-.............\\\\.-............................/|........|../..........\n-..........................|.......-.../..............|...................\\................|........-...../...\n.|............\\.//.....-..........-....\\......./...........................|........................|.........\n.............................................-................................\\..-.\\................/........\\\n........|........................-....\\..........-.........-..../....\\../....\\.\\........\\.....\\.|.............\n.................\\..|...........-...|......../................|.-..........-./...........|.--.................\n.......................-................\\............\\..|.....\\....\\....|\\.....\\.....-...\\.....-.|............\n..........................\\....|.../........../..........//........\\\\.........................................\n........................\\.|..\\........./.........../...||.............../................../.........-........\n/...........................|.....-..../../........./.\\.....................|....../.|.....|..................\n....................\\.../-|..../..................../.....|..............\\./.....-.........../.--......../|.|.\n...-........\\.....|......|....|.-............................-.....|./.......-.....-.....|-.....|.............\n....../............................................./..|....-..................\\.....-.................//.....\n\\.........................|....................................................../...../...\\.........-..-.....\n....-../..\\\\.......-......../...\\.................................\\....-.............\\.........\\.....-........\n............/.........../...........\\....................\\......../.....\\.................................|../\n.......\\.........-....|..-|..|..............\\............/.................................\\......./..........\n................./..........................................\\...........-|......................./.........\\..\n|.......................-..../-/.......-........\\.........................................../..../.......-....\n.\\.............|.........|...--....-..|..-..............................................................|\\.-|.\n.\\....\\...-\\.....|./..........................\\..............-......................\\.....|...................\n.....|......./.|......................|.................................\\........./..../..........\\...........\n/..-...................|.......|.....-..........|...........\\...-............-......|.|...................../.\n....................\\..../.............|............|...............................-....|.-.........\\....|...\n...|\\.\\.......\\......\\.....|..|.......|...\\........................|.................-..-...........-.........\n............../\\-.|.......-...\\..........-............\\.../...../..|......\\................./............|....\n..........|............\\..../........-.............\\./....../.........................../.-...................\n.-.\\/............................./......|....\\........|....../............\\......-....../.........\\..........\n........./......../../.................\\...-.\\.........\\............./../.....-............\\..................\n\\.\\.......-..................-........-\\.....|....\\-............/...\\............../..............." <> ... ``` ## Part 1 ```elixir defmodule Part1 do def parse(input) do input |> String.split("\n", trim: true) |> Enum.with_index() |> Enum.reduce( {%{}, 0, 0}, fn {row, y}, acc -> row |> String.graphemes() |> Enum.with_index() |> Enum.reduce( acc, fn {char, x}, {map, max_x, max_y} -> { Map.put(map, {x, y}, char), max(max_x, x), max(max_y, y) } end ) end ) end def move({x, y, dir}, ".", max_x, max_y) do {nx, ny} = case dir do :east -> {x + 1, y} :west -> {x - 1, y} :north -> {x, y - 1} :south -> {x, y + 1} end if nx >= 0 && nx <= max_x && ny >= 0 && ny <= max_y do [{nx, ny, dir}] else [] end end def move({x, y, dir}, "\\", max_x, max_y) do new_dir = case dir do :east -> :south :west -> :north :north -> :west :south -> :east end move({x, y, new_dir}, ".", max_x, max_y) end def move({x, y, dir}, "/", max_x, max_y) do new_dir = case dir do :east -> :north :west -> :south :north -> :east :south -> :west end move({x, y, new_dir}, ".", max_x, max_y) end def move({x, y, dir}, "-", max_x, max_y) do if dir == :east || dir == :west do move({x, y, dir}, ".", max_x, max_y) else move({x, y, :west}, ".", max_x, max_y) ++ move({x, y, :east}, ".", max_x, max_y) end end def move({x, y, dir}, "|", max_x, max_y) do if dir == :north || dir == :south do move({x, y, dir}, ".", max_x, max_y) else move({x, y, :north}, ".", max_x, max_y) ++ move({x, y, :south}, ".", max_x, max_y) end end def energize([], visited, _grid, _max_x, _max_y) do visited |> Enum.map(fn {x, y, _} -> {x, y} end) |> MapSet.new() |> MapSet.size() end def energize(q, visited, grid, max_x, max_y) do [head | tail] = q if MapSet.member?(visited, head) do energize(tail, visited, grid, max_x, max_y) else {x, y, _} = head energize( tail ++ move(head, grid[{x, y}], max_x, max_y), MapSet.put(visited, head), grid, max_x, max_y ) end end end {grid, max_x, max_y} = Part1.parse(input) Part1.energize([{0, 0, :east}], MapSet.new(), grid, max_x, max_y) ``` <!-- livebook:{"output":true} --> ``` 7199 ``` ## Part 2 ```elixir {grid, max_x, max_y} = Part1.parse(input) starts = (0..max_x |> Enum.flat_map(fn x -> [{x, 0, :south}, {x, max_y, :north}] end)) ++ (0..max_y |> Enum.flat_map(fn y -> [{0, y, :east}, {max_x, y, :west}] end)) starts |> Enum.map(fn start -> Task.async(fn -> Part1.energize([start], MapSet.new(), grid, max_x, max_y) end) end) |> Enum.map(&Task.await/1) |> Enum.max() ``` <!-- livebook:{"output":true} --> ``` 7438 ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More