Day 14
Input
sample = """
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....
"""
input = """
.O.O.#........#..#...#..#....#...........#O.#.##.O#....#.......O......#..OO.#.#O....#O..O#...OO.#O..
.#....O#.#O#....OO....O#OO...O...O#.O.O.#.....O..O#..O..O...O#..........O#....O..#O.....OO....OO..O.
..#.#.#....##.#..#....O..O...O.#O.#......OO..#.#.#...OOO.##......#O..O.#.O.....O#....#..#..O.#OO###.
....OO...O###.OO...#...O....##..#..O......O......#.....#...O..#..#O.#.#O.#..#..#O...O.O..#OO.....O.#
.O..O....#O.OOO..O...O..OO.OOO.#.OOO...O...OO....O..#.#.O.#.#.O#.O....O#..##.O.#.O.O....#.....O.....
.O..O...O.O......O....O..#.O.#.....#....O#...O..O..#....#.#O.....#.#...O.O..OO.#OO.....#...#..O.....
.O..O.OO.O...O.#OO..#O......O#......OO#OO..#...##....O.....O...O...O..O.O..#O..O.O...O..##O..O....#.
.O.......#.OO..O.O#...#O..#..#.O.#.O#.##O#......OO..##..O.#.....##..O...#.#.#.......O..OOO.O..OO.##.
O#OOO.O.O.......##.O..O...O.#O..O.O..O#.##..O.#...#....O#..#O..##....O..##O#O........#...##..#.....O
....#.O..O#...OO..O.O.........O#......##........#....O.#OOO.##O##..O...#...........##...#O........O.
OO#.....OO..#........#..O..O.O#......##.#.#O.#.O.#.O..O..#OO#.#..#...###..OO.O.#...#..#....O...#.OOO
O..#..#O##.#.O......O..#....O....#..#.O#.#.O.....O.#O.......#...OO#.##O.##...O.O.#O.O..#O..O.#O..O..
......#OO......O.##....#...#......OOOOOOO..#..#O..OO.....O...O.#...#O..O..O#O#.....O....#.......O#O.
..O##..O.#..#........#O.OO...O....#...O........OO...O.....##......#..###.O..O##....O.#...OO....#..O.
.O.OO...#..#O#...O#..O...O...O#....#.O.....#O.O.O........#....#.....O...#...O#..#.#O.#......OO.#O#O.
#.#....O..O........O.#........#..O....O##.....#..O#.##...OO...O#..#.....#..O..#...#.O.#...O.O...#..O
#..........O#......O#...O.....O........#..##....#.O.#........#.....#.O#O....OO.O..O..#...#...#.O.#O.
O..OO....O...O.#.#...O...............#O#........OOO.#....O#.O.....OO#OO#.O#.O.O#....O....#..#...O.##
.#...#..OO.O.O...O.##..OOO...OO.#.OOO..........#....##.##...O...OOO.##O.....#....O.O.O.O...#.##...#.
.O.#.O....##OOOO..#..O.........O..O#..#.O......O....#.....#....##.O.#.O...#O.OO........#.......OO#..
.#.#O...#O.O..O#O#..O.......O..OO...O##...OOO.#....OO.O##...O...........O#..#..#O.......#....#..OO.O
....#...OO....#.OO.#.O.#..O#..#O..........O....OO.O.#.O.....##OOO.#..#....O.OO.#O.O#.#...#.#.O....##
O#......O###O..O...O...#O.#...............O......#..O.O.............#.#.O.#.OO.##..OO###....#O.O...O
.#O...O##.#O.O..OO..#O.OO.........##.O.#.#.OO....O.O..#O#.O...O.........OO#.OOO...#..O..O#..O...O...
...#O.O.OO...#.......O...O...O.##.....O.....#..#OOO....#......OO....#.O..........#O.......#.#.O#O...
..O..O..OO.....O....O##.O#.......O#....O.O..OOO#......O#.....OOO..O.O#O#O..O.O..#.O#O..O.OO.#O.O....
.#..O..#..O.O.###.O...#....OO#..O.......O..#.O#O##..O..O#O...O..O....##......O#O.....O....O..##O....
...#..O.O#.#O..#..OOO...#.#O...O#...#.#....O....#.#.#...OO#....O###O#..O..#O..O..OO.O#.OOO.OO##..#..
..O.##.O..OO#....#..#..O#O...O.#..#.O.#.#O.......OOO.O#O...#O..#....##.O.O........#....#O..O.#.O....
#OO#.O.OO#..#O..OO#.#OO.O..O..O.#.O...O#.....O#....#.O......OO.#...O..........#.O...#OO.....#.O.O..#
..OO#.O#......O......#...###.....#OO#O#OO#.O#..#...##O.O.#...#O..##O.O.......#.#O#....O....#........
...OOO......O....O...OO...#..###...OOOO.O.......O...O.......OO.O.....O..O#..##....O.#O....#.O.......
.OO..O.O..O.OO.O.O.O#O#...O#.....O...OO..O#.O.#.O.O.#.....O##.#..#.O#..#....O...........O#.O..O.....
..O....O.....#..#.O.O......O#OO...O..OO.....#O.#.#O#..O#..O.O....#......#.OO...O...#O.O#..O.O.##....
.#.O....##..O.#.O.#....O.O....OOOO.O......#O.O.OOOO...........OO.OO..O.##.#....#O....O...O.O..O..#..
#OOOOO.##......O.OO..#O.O.O#O....O.O..#OO#O..OO...O.##..O.O.#.O..OO..O..#.........O......O...#......
....O...O...O...........#..##.O.........O..O#...O.OOO...O..#.O..O##.OO.......O..#O#O.#..O..O.O.O.#..
..#O.#...O..OOOO#..#....OO..##O....O.#.#..#..O.OO###...#...O.#.#.#O.O..#.O..#........#.O.O##O.O.#..#
.....#.O.....#..#..#..#.O...OO#.#.O....OO..O...#.#.O.O#......O#....#O.O.#O#.#..##....O.#O.O...O..O#O
....#..#..#O.....#.#O..OOOOO...O....#O.....#..#.#..#....O..O#.OOOO........O.....O...O.##..##.O#.....
...#.#..OO......O.O.#..###.#.#....#..O.O....OO..OO.......#..O...O#..#....O...O....O..#..#..O.O...#O.
...###.....#.O..##...O.#..#.O#.O#.O......O..##.....##O.O..O..O.#.##O#.##...OO....#O.O.....O.#.#OOOO.
.#..OOO....##...O#O..O....#.......#..#.#.O....#O#.#...O....O.#..O#....#.#......#O.O......OO#.OO..#.O
..O.#OO#........#.#OO.##O.....O#O......OO#....OO..OO.O...#.....#..O.OO...#.#..O..#..O..#O.#.........
O.#OOO.O.#.O.O.#..#O..O...O....OOO..OO.OOO..#OOO.#O#.....O#....#..O...#.O...O.O.....#.O..O##O.O#O#.O
.....O..O.#.OOO..O..#O.OO..#..#.#O...#............O..O..#.#.....O.#....#.........OO......O.#.O..#.##
.O..O.O#.O....OO...O..#O.O..........#OO#..O.O...##..OO......O#O.......O.#.#....#OO.....O......O...O.
..OOOO.O#..#...O..#.O............#....#..O.#..O.#......##..#..O......##..O.O#OO#....O.OO...#.#.#...#
.#O....#..O.#....#OO...##..#..O...O.......O....O.#....#...###.O#.....O.......O.#..O####...#.OO##O.#.
.O..O.#..O......#.O..O......O....#O....O.#....O#OOO#....O..O.....##O....#..OO..O...O#.......O..O#.O.
#...#....O.....#OO..#.#..O.O.##O#..#O.......#..OOO........#.#O.....#..O.O#O....O#....O.....OOO.....#
.O#O..O..O#.O..............O#...#.........#.#O....OOOO...##OO..OO.........OOO.......##.##.......O...
.....##..#O...#OO#......O.O..O.O#.O#.#..O......#...#.#O.....O......#O#........#O...O..OO..#.O....#O.
O....###..#.............O..O.O..#.O##...O.....#....O..#...O....##.#.O......#..#...O#.O#.##O.O.......
.#O.......O#OO.##.......#..#.#.....OO.#..#.#..O.....#...##.O#O..O........###....O..O.O.#....O#.#..O.
.O#.........O...O.....OOOO.....#.O..#.#O.O#.......O.#O.#..O.OO...O....#...O.......#O.#..OO.O#...O..O
..O..#..#........O.O.#O..O..#OO#.#.........#....O.O.....O..O..O..OO..OOO.O##......#.##O....#.#O.#...
.##.O....#.....O..#O#..#..O#...##.OO#..O..O..##.O......#.....O..O.#.O..#...#.O.#..#O.#....#..O#....O
....#.......#O...##O......O..#...O.O#O...OOO.#..OO.#.#.#....#..#..#O.O..#.O.O..O#......##...O#O.#...
O...O...##.O...#.O...#....O.....#.#OO.O...O#.##..#...OO.O....O..O...#.O....O#...O...OO..O..#.....O##
O.O.......O....O.OO.O....O..O..#...#.O#...O.OO.O.##.O..O........O.#.O#.O..##..OO.#.O..O#....#O....#.
#..O...O....###O...#O.#.........O.OOO.OO...O.##....OO.O...OO.O..#.#O..#.O.OO..O.#O#.#...#OO....OO...
.#..#...O...#.O.O....O..#.O......O.....#.....#.......OO.O.......#OO#.O..#.O..O..O......#OO...#O#....
.O.....OOO#..#.O.O..##.#....O.#.O.#.........O......O..O#......#...#.........#..O.....#.O.#...O..#...
...#O..O#..OO.O....#....#.......OO..O..OO..#.O...#..O...........O...O..O.OO..#....#.O...O......###..
...##.O...O.##.#..##.....##...##..#....O.#..##.OOO....O#..#.##OO..O............O..#O...O..O..O.#....
..O..O..O.O.OO.O.OOO...#..OOO.#..##.#..O.#..O#...O..O.O##O#.OOO...###....O#.....OO.O..........OOO#O#
O##.O..O.#....O...O......O#.......OO...#O#.O.O..##...............O.#O#.....OO.#..#..#O....###.#O#.O.
..O..#...#..OO...#O........###..O#...#..#O...O...O...##......O....O#..OOO#O.........#.#..OO....#....
...O..#.O.O...O..#O...O#..O...O#..##..##....O..#O.....#....#.#...#....#...#O.##..O.#O##.....#.......
.O..#OO.#..O...#OO....O...............O.#.##....###.#.....O..O...O..O...O...OOOO....O##..OO.O...#.OO
..#O..O..OO#..#.....O.#O.....#.#...O.#..#.....###.O....#..#.......O..#..O#.......O...#.OO.OO.OOO....
.......OO..#....O.#....#.O......#...OO##......OO...#O.#.#O#.##..#O.###....OOO...OO...O#.#O.O...#.O.#
.....#...###...#O..#.O.#OO##O.O#.....##.O......O.O..#.O...#.O.....#.#OO.O#.#O#.#.O.O#..O.O#.#.#..#.O
.O...#...##..O...#.#O...O...#..O..O....O.#...O....#O..OO..#.O##.......#O.#.OO#O.O..O.....OO.OO....O.
OO.....O.#...OO...O.O#OO#.O..#...OO......#..O..O.O....O#...O.....#..O.............O#O#.#....##.#....
#.....O#...#..#..OO.O...#...#..O.#..#.......#..O..#...#O.....O....O#O.O..O.#...#.OO##..#O#.O.#..O...
.OO.................#.#O....O...#........#.O#.#OOO.O.....#.#O.#....#.O...O.........O.O..#.......O...
..O.#....#.O.#....##O....#...O.O......#.#..O......OO.....O....##....O.##O....O.....O.##.O...........
O.....O....O#...O..#....#O...O.O......O#..##.O.O.O##....#..#.OO###..#.O#OO.O.#.O.O##.OO...O..OO..O..
#....O#...O...##O.O.O.....#.OO.O.O.#.....O#.OO.#.#..O.OO...O..#O.#..#....O.#O....#.#.#O...O..#O.O...
.O..#O#..O#................#O#.OO....O#..........O.......OOOO.....#..O..O.#....O##.O....O#...O...O..
O..O.O..#.....O...O...#....O..OO#.#.O#....O.O...#.......O.#....O....##..##O.O.O....O.#.OO.#..O...O..
....O..O...#..O#.###O...O#..#..........O##..O.#.#....OO..O..#..O......OO..#......O#.O...#..OO...#...
.#..#...#.....O..O..#...#O.O.....##......#.O.#...OO.O..........#O.O..#.#O.O...#OO.#O.O.#.....#......
..O..#..O...#......#.OOOOOO..OO..#O...O###...O.....O.#.O..O.OO#..O....#..OO.#O.#.O...#.#....#.O..O..
O...#........#..OOO...OO..#....O....#O#..#O.....#..O.#.#O..O.......#...OO.O.#....#.#O..#...........O
O.O..#.....O.O.....O.#.##O..OO..#OOO.#....O.O.###.....O.#..##..#.#.......#.........#O#...#...O....OO
...#O...O#...#.OO....#....#.OO#O#OO....O#.O#O.....#.#..O##..O.#OO.#O..O....O....O.OO.#O....O.......#
O....O.#.....O.#.#O...#O.O..##.#..O...#.#O.....#.#..O..#.....OO.#..OO#..O..OO#..#...OO.....#......#O
.......#.#.O..OO...#O....O.O#.#.....#.#..O....#.....#.O#..O#..#...O#.O.....##....#....#...O..O...#O#
.#O....O.#O......O..#..O#.....O.#.#....O.#...O.#...O.O........#O....OOO#...OOO#.OO.O#.O....#.....O..
O..O.OO..O....#O..#..#.#..OO....O#....O##O.....#......O.O.O.....O..O.......O...O..O#O...OOO..O#O....
#O.#..O##O....#.....#.#....OO.O##.O.....OO.O.....#...##.O......O#.#.##.O..O###.#..O...O...O.......#.
#.O.....O...O.#OO#.###....#O.O..O.....##...##..##.#.....O#.#.O#..O#..O.....O.......O.#O...#.O.#..#..
.#..O...O..O..OOO#...###.O#O....#O#..O.#.#O.OO..O.O.O.#...#.......#.#.O.OO...#OO..#O.....#.#..#..O.#
.O....O.......OO#.....OO..#O..OOO..#O....#O......O.O.O.O..OO..O.#..O.......O...O.O.#..O.....#O...O..
.#....##O#..#OO...O..#.O.O.O......#...#...O#.....#...O..#.O.#.O..#.O..##.O.#..#.#..#O...###O.#O.....
O..O...#...O...O...........#...OO#.##...O..OOO.##.....#..##O.....#....#......#O.#.O..#O.##.#....O.#.
O.##..#....OO.O....O#O....##.#O......O.....#.....O.....O#.#.#O.#.#..O.#..#.O#...O.OO.O...##.OO...##O
"""
Part 1
defmodule Aoc2023.Day13.Part1 do
def load(rows) do
height = rows |> Enum.count()
rows
|> Enum.zip()
|> Enum.map(fn row ->
row = row |> Tuple.to_list()
row
|> Enum.with_index()
|> Enum.reduce({0, []}, fn {c, i}, {pos, rocks} ->
case c do
"." -> {pos, rocks}
"#" -> {i + 1, rocks}
"O" -> {pos + 1, [height - pos | rocks]}
end
end)
|> then(fn {_, rocks} -> rocks end)
end)
end
def run(input) do
input
|> String.split("\n", trim: true)
|> Enum.map(fn row -> row |> String.split("", trim: true) end)
|> load()
|> Enum.map(&Enum.sum/1)
|> Enum.sum()
end
end
Aoc2023.Day13.Part1.run(sample)
Aoc2023.Day13.Part1.run(input)
Part 2
defmodule Aoc2023.Day13.Part2 do
def north_load(rows) do
height = rows |> Enum.count()
rows
|> Enum.zip()
|> Enum.map(fn row ->
row
|> Tuple.to_list()
|> Enum.with_index()
|> Enum.map(fn {e, i} ->
case e do
"O" -> height - i
_ -> 0
end
end)
|> Enum.sum()
end)
|> Enum.sum()
end
def tilt_north(rows) do
rows
|> Enum.zip()
|> Enum.map(&Tuple.to_list/1)
|> tilt_west()
|> Enum.zip()
|> Enum.map(&Tuple.to_list/1)
end
def tilt_west(rows) do
height = rows |> Enum.count()
rows
|> Enum.map(fn row ->
row
|> Enum.with_index()
|> Enum.reduce({0, [], []}, fn {c, i}, {rounded_pos, rounded, sharp} ->
case c do
"." -> {rounded_pos, rounded, sharp}
"#" -> {i + 1, rounded, [i | sharp]}
"O" -> {rounded_pos + 1, [rounded_pos | rounded], sharp}
end
end)
|> then(fn {_, rounded, sharp} ->
0..(height - 1)
|> Enum.map(fn i ->
cond do
i in rounded -> "O"
i in sharp -> "#"
true -> "."
end
end)
end)
end)
end
def tilt_south(rows) do
rows |> Enum.reverse() |> tilt_north() |> Enum.reverse()
end
def tilt_east(rows) do
rows
|> Enum.map(fn row -> row |> Enum.reverse() end)
|> tilt_west()
|> Enum.map(fn row -> row |> Enum.reverse() end)
end
def cycle(rows) do
rows |> tilt_north() |> tilt_west() |> tilt_south() |> tilt_east()
end
def run(input) do
input_map =
input
|> String.split("\n", trim: true)
|> Enum.map(fn row -> row |> String.split("", trim: true) end)
{start_map, loop} =
Stream.iterate(0, &(&1 + 1))
|> Enum.reduce_while([input_map], fn _i, [last_map | _] = maps ->
new_map = last_map |> cycle()
if Enum.find_index(maps, fn map -> map == new_map end) do
{:halt, {new_map, maps |> Enum.reverse()}}
else
{:cont, [new_map | maps]}
end
end)
start_index = loop |> Enum.find_index(fn m -> m == start_map end)
loop_size = Enum.count(loop) - start_index
loop |> Enum.at(rem(1_000_000_000 - start_index, loop_size) + start_index) |> north_load()
end
end
Aoc2023.Day13.Part2.run(sample)
Aoc2023.Day13.Part2.run(input)