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

Day 20

lib/day20.livemd

Day 20

Setup

sample = """
..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#....#..#..##..##
#..######.###...####..#..#####..##..#.#####...##.#.#..#.##..#.#......#.###
.######.###.####...#.##.##..#..#..#####.....#.#....###..#.##......#.....#.
.#..#..##..#...##.######.####.####.#.#...#.......#..#.#.#...####.##.#.....
.#..#...##.#.##..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#..
...####.#..#..#.##.#....##..#.####....##...##..#...#......#.#.......#.....
..##..####..#...#.#.#...##..#.#..###..#####........#..####......#..#

#..#.
#....
##..#
..#..
..###
"""

input = """
##.####..##...##..##.##.###....#..##..#.####.#####..##.#.##.#..#.#####.#.#.....#.####.##.......#.###.#.#..#.##.#.#....####.###..#.####.....##.##.#.#.#.###..#.#..##.####..####...####.#......#...####.#.....##.##.#.#####.#.##...###.#.##.#.#.###.#.####...#####..#.#....######.##.#.###....##...##.#####..##.#.###....#...#...#.#.###.#...##..##........###.#####.###.##....#.##.##..###..#.####....##...###.#####.##..#...###.######.#..#.#...##..#.#####.###.#...#....#.##.#.#.#..#...#####.#..###..#.#...#.#.###.#.#..###.#.

..#.####..###.#.###.##..#...##.#.#..###........#.##...##.#.#...########.#.##...#..##.#.##..##...#.##
#####....#..#.#.#...#.#.#..###.##...#.##.#.#...#.#.##.##.......#....#..###.##..#####..#.#.....###..#
.#.##..#.....#.#.#.#.##.#.#....###.####.#.##...#..##..#.#..#.#####....#.#...##.##...#..#.##...##...#
##...####....#...##..##.#..##..####.###.#....#.#.......##..#....#.##..##..##.#..##.#..##.###.#..###.
...##.#..#..##..#.#..#.#.####.....#..###......#..#.#.###..##.....#.##.#.#..##...##.......##...#..#.#
..#..##.....#.....#...######.#..#.#.#.#.###.##.#...####.#...#.#.#..###..##.#...##.#.#.#.#.#.......##
#.#....#.#.###.##.####..#..####.###...#..##.#.####.####..######......###...##.#.####..###..#.####..#
.##...##..##.#..#.#......##.#.#######.##.#.########.##..#.##.#.#.##...#..#.####.#....##..###..###...
##......##....#.##............#..###....#.###.###.###.#.###.###..##.#...#.##...######.##.##....#.#.#
..#.#.#.##.###.#...###.##.##.##.#.#..####.##...####.#.##.#.#..#.##.###..####..##..##.#.###.###.#.##.
.###....#########.##..###.##.#..####...###.###....#.#..#...######...##..#######.##.##....####.....#.
#.....##.##..##......####..#......#.###.#..#..##.##.#..#......##.##...##.#####.#.#..#.####.##.#.#.#.
...#.#..##..##.##....##....###.#.#####.#.#..##...#...#.#..#.#.#.#.##.#...####.###.#.####.###.....#..
.###.###.#####..####..#....#.####...##..##.......##....#...#..#.##.#.#...##.....##.##....####..#....
.#...#...##..##..#.##.....#######..#########..##....#.####....#.###...##.##..##..#...###..#.###.#...
..#.##..##.#.#.#...#..........##..#...#...##....#..###..##.....#.#..#.#..####.....####..##.#..###...
.##.#.###..###.#..##.#.####...#.#..#....#...#.####..##.#.#..#..##.#....###.#.##.#....##.##.##..#..##
#####..##.#####..#####..##.#.##.#.#..#...#.#.###...##....###..#.#.....#.......###..###..##.#..#.....
##....#..#....#.........####.##.###..#.#.###..####...###...#.#..####....###.#.#...#...#..######.##..
..##.###...#.##......#.##......#.......#..##.#.##..###..###.##.#.#.#.#.....#.#..#...#.##.......#.#.#
#.#..#..####..##.....##..##.....######.#...#....####...#..#..##......#...##.###..#...#...##..#.##..#
..###..#.###.....#.#.#.##....##...###..#.#.#..#.##..........#.#.###.#.##...#...#.#.####....#..###..#
..##....#....##.##..#.#.###..####.##.#..#..#.#...#.###.#.#..#.###...##.##.##...##.####..##..###.....
##..##.###.#.##..####....##.##.##.#....##.##...##.#....##.###.#.#..##.#.#.#####..##.###.#.#...#..#..
....#.#..####.....#...#.##....#####.##...#..#..#.#.##...#.##...##....#.##.###..###....#...#.##...#..
#.....##.#..#######.#.#.####.##.#..#####.##.....####..#..###.....#.#..#..##..########..#.#.#..##.#..
#....#####.#.#.#.####.#..##..##.#.#..#..#........##.####..#.###.#..####..##.....#.#.###.##.#.....#..
...##...######..####.#....#..#..###..#.#...##.##..#####.#.###.##..###.#.####.##.#.#.##...##....#.###
..###..#.##...#..#..#####.#..###...####...###.#.#.####....#####.#...#...#.#...#..#...##....#..##....
#..#....#.#.##..#..##...#####.#####.#.#.#...#.#####.#...#####.##.#..#...##..#....#.##.#.###...##.#.#
##.#........##.###..##.#.#.#..#.#...#....#...#..####.#####...#####..###.###.##.....#....##.###..#.##
#.##..###.###.###.##.....#.##.#...#.#.###....##...##.#..#.##.###.####.#####..##..#....#..#.####.#..#
#.###.#######.######.#.........#....###..#.#..#..#.####.###.##.#.#..#.##.#.#.######..#.##...##...#..
##...###.##.##.#####..#.###.#..####..#####....##.###..#.#..###...###..##.#....###.###.#.#####.###..#
###..###..##..##.##.##..####.#..###.##..#.##.######...##..#...#...####....###...#.#...#.#..#.#.#..##
##..##..###....##............###.....#...##.#..#.###.#...#...#######...#...##.#...#..#.######.#.#.#.
...###.##.###...#...#.....#..####.##..#...###.##..#...#.#.....##.####.....######..####...#.#.###.###
.###.#..#####...##...##...#..##.###.#......#.#####.#.#....#.##.##.#.#..##..#...#....#####..#.#..##.#
#..###....#..#.#.###.#...#.#.##.#..#..##.###.....##...##.###.##.....#.#.#..#..###.###.##..###.##.##.
###...##.###..#.#..#......##.#.###.###..###...###...##...#.#.#..#....##..##..#..#...##..##.........#
..#.#.......#.##..###...#...#.#.#.#####..###.######.......#....##.#######...#.######.....#.#...#..#.
.#..##.##..#.#...######....##.###.##....#...##.####.####...#..#.###..#####..##.#.#####...#####..##..
.#..##..#.#...#..###..####..#.####.##.#.##.###...###.....####.##.....#.#####.##..##..#.##.#.#.#...#.
....####.##.###.#...###.#..#...#####.##.##.##...#...#.....#..##..#.####.#..##..#...###..#.#.##.#....
.#..#...#.#..##....#..##.##..#.#..#...#...####.##.#...#..#####....#..#.#.###.....##...####.#..####.#
#.##.###....#.#.##.#...#.##########....######.#.#.##..##.#...#.#...#..####.####.####.........#.#..##
##..###.####.###.....##.###....##.#.#.....#.......####.#....##...##.#.....#...##....###..#....#.##.#
##...##.#.#.#.#..#..#.#.........##...##.####..###.#####.#..###.#..##.##...#..#..##.#...###.#....###.
...#.#.#.####.##.#.##...####.#.#####..##.#...#...#...##...#......##...####...####.#####.###.##.####.
##...#..#.#.####..#..####..######.##.#.##.#.######..#.#..#...#.##.##.##.......###...#.##.#...#.###.#
#.####..##..#.####..#####.##.###.##..######.###..##.#.##.#.##.#..##.#.##.#..###..##.##.####.##.###.#
###.#####.###...####.#.#..##.#..#.#.###.##.#.#.####..####.###......##.....#.###.#..#..#######..####.
.#.#.#..####....##.....##..#####.###.##..###.##.#..###...####.#......#.##.###.#.#.####....####.##...
##..#.##.###..##..######.#.#.#...######.#...#####....####......#..##..##..###.#####.###..#....#..#.#
#.###....#..###........#.....#.##.#.##.#..##....############....#.###...#.#....#.#..#.#.#########...
.....#.#.#.#.#.#..#.#.##..###..#.#..##.#.##.#.###...#..#..########.........###...#####.####..##..###
#..#...#####..#.#....###......#.##..#....#.#.#...##.##.######.......#####..#....##..##..#...#...#..#
..###.##.#..##..##.#.#....##.##.###.#...##....#..#.#.#..........#########...#.####...#.#####....###.
#.####.#####.#.....##.#...#......##...#.#...#.##..#####.#...##...###..#..##.##...##.#...#....#..##.#
..#..######..##.##.#..##.#.....######.##..#.##.#.#..#....###...###..###..#.#.....##.##...##.###....#
#..#.####..#.##.#.#.#.#.#....##....#...#...#.....#.#..#...####.########..#.##.#.##.##.##.##..#####.#
###......#..#....###......##.##..#...##...#.#.##.##..######..###.#.#....##..#####.#.#...##.#...###.#
#.######.##.#.#...#.#..#....######.....#..#####.##..####.##..###.#.#..####.###....#..#..#####..#..##
....#.###....##.#.###.#..#.##...#.##.##..##..###.#..##.######.##....#..##.#...####.#....#.###..###.#
.###...#..######...#..###.####..#...###.###..###.....#...##.#.###.##...###.##.#..#..#.##....#######.
.#..#..#..#######.#..#.#..#..#.#...#.##..#..##....#...#.#...####..#.#.####.....###...#..#..##...#...
.#..###.#.#..#.......#.####...#.####..##.#.#.###..#.##..#.##.##.#..####...#....#.....######.....#...
.#.#.###...#...##..##..##.##.#.....#.....####..##......##...#....#.###.######....#.#....#.#.##....#.
.##.#..#.######...###..##........###.#..#..#.##.#.##.#########.#...#####....#.###..#.#.#.....#....#.
#..##.###.##.#........##.#..##.....##....####.#..###.##.##..#.##.#.####..####..#.####.##.##.#.#..##.
#.#...##....#.#.#.......###...#.####..#.##..#####...###.#....####.....#....#...#..#####.##...####.#.
####.########..#.#......###.###.#.###.###..##.###...#.##.....###.###..#.....#..##.#.#....#..#.##.###
.###....##......##.#...##......##.#.#.#.#....#.######.##.####...##.#.##.####....##.#...###...#..#.##
.######.....##...#.##.#.###.....######.##.####..##.##.###.#...##.##.#.#..#######.#...#.#..#.#.#..###
...#...###...#..#.#..###......####...#.##.##..#.##..#..#....###.##....#....#.#..#.....##......#..#.#
#..#.#.##..###..####..#.#####.#...##..#.##.#.#...#.#.#.##.#####.#.#####..#.####.##..#.###.#.......##
##.#...#..#...##.#.#.##.##..##...###.##..###....####.##.##....##..#.#.##...##..#..#####.##.#......#.
.#####..#...#.....#.##...###...##.##..##.##.##.##...##.##.###..#..##.###.#..####.......#.#..###.....
###.###.#.#.###.##..####.##....#.##.#####.#..##...###..#..#.##..##..##.##..#.###..##.#..###......#..
#.##....#..###..##.#.##..#...#####.##.#..####..#.#.#....###.###....####............#.#..#..####.####
##..####.....#..###.#..##.##.####..##.#.#.#.###..##..#.##.###.#....###..###..#.....######..###.####.
##..####.#.#...#.#...#.#.##...#.#...#.##....#.#...#.#....###...#.#....###....##.#.#.###...##.####.##
#####...####.###..##.#.##.....###..####.#...##.####..#...##.##...#.###....###.#...###.##....##.####.
#..##..##...#....###.##.####........###.###..#.##.....#####..##...##.#.#.#....#.#..#....#.#.#.#..#..
##..##..###.########...#.#####......###.##...##.##..#......###...####..#.##.##....##....#...#.####.#
.##.####.#.##.##.##.##.###......##...#..##.#.#.##..##.#..##...#.#.#..##....#####.#.####...#.##.#.#.#
#.#..#..#......#######..##.####..#..#####...#....####.#..#.#.#.####.#.....########..#.#...####.##...
#.##..#####....#..#..###.###.##...##...#....#..#.##...#.#..#....#..#..##.....##......#....#..#.....#
.##..##.#.#.#.#.###.#..#...##.....#.##.#.#.....##.###......#..####...#####.#.###..##...#...#.#...#.#
#....##.##.#.#.###..#......#....#..#.#.#......#....##.####.##.#######.##..##.#..###.#.##.###...###.#
#.#...#.#.####..#...####..######.####...##...#.....###.##.####......#.##..#.##..#.##....##...###...#
.#####..#.###.###.###.##..##.###..##..#.....#...#..#..#####..##..##....###.##..######.##...##.##..##
#.##.....##.####.##.##.##..###.#..#..#.#...###.....#..#.##.######...#.##..#...#..#..#.#.###...#.....
#..#.#.#...###.#.####.#.#.#######.######...##.####.#..#..###.#.####.#...#.###.####.....####...#...##
##.....#.##......#......####....#.#.#.###...#....#.....####.#.#...#.##..#....#.#..##..#..##.##...#..
.#.##...####.#..#.#...#.#...#.#....#.#..#..#.#..##.#.#..##.##.#.####.#.###..#######....###..####....
####.##.#######.##.####..#.#...##..#.#...##.#.#...##.##....#.#...###.#.#..##...##..##...#..##...#.#.
#....#..#....#......#..#..##.#....##.#.#.#....###..####.##.#.#.#...#...#####....#..###.######.#..###
#.##...#.###.###.#.#.####..##..#....##..#.##.###..##.#...#.##.....###...###.#...#..#.##.##....######
#..#..####.##.###.....#.#.......##.....#.##.#######...###..#.###.#.####.####..#.#.#...#.##.##...####
"""

Part a

defmodule Day20 do
  def enhance(image, algorithm, placeholder) do
    image
    |> expand(placeholder)
    |> expand(placeholder)
    |> decode(algorithm, placeholder)
    |> contract()
  end

  def flip_placeholder(algorithm, placeholder) do
    for _i <- 0..8 do
      placeholder
    end
    |> Enum.join()
    |> then(&amp;String.at(algorithm, Integer.parse(&amp;1, 2) |> elem(0)))
  end

  def decode(image, algorithm, placeholder) do
    height = Enum.count(image) - 1
    width = String.length(hd(image)) - 1

    for i <- 0..height do
      for j <- 0..width do
        if i == 0 or i == height or j == 0 or j == height do
          placeholder
        else
          (image |> Enum.at(i - 1) |> String.slice((j - 1)..(j + 1))) <>
            (image |> Enum.at(i) |> String.slice((j - 1)..(j + 1))) <>
            (image |> Enum.at(i + 1) |> String.slice((j - 1)..(j + 1)))
        end
        |> Integer.parse(2)
        |> elem(0)
        |> then(&amp;String.at(algorithm, &amp;1))
      end
      |> Enum.join()
    end
  end

  def expand(image, placeholder) do
    height = Enum.count(image)
    width = String.length(hd(image))

    [String.pad_leading("", width + 2, placeholder)] ++
      for i <- 0..(height - 1) do
        placeholder <> (image |> Enum.at(i)) <> placeholder
      end ++
      [String.pad_leading("", width + 2, placeholder)]
  end

  def contract(image) do
    height = Enum.count(image)

    for i <- 1..(height - 2) do
      image |> Enum.at(i) |> String.slice(1..-2)
    end
  end

  def display(image) do
    for row <- image do
      row
      |> String.replace("1", "#")
      |> String.replace("0", ".")
      |> IO.puts()
    end

    image
  end
end

[algorithm | image] =
  input
  |> String.split("\n\n", trim: true)

algorithm =
  algorithm
  |> String.replace("#", "1")
  |> String.replace(".", "0")
  |> String.replace("\n", "")

placeholder = String.at(algorithm, Integer.parse("000000000", 2) |> elem(0))

image
|> hd()
|> String.replace("#", "1")
|> String.replace(".", "0")
|> String.split("\n", trim: true)
|> Day20.enhance(algorithm, Day20.flip_placeholder(algorithm, placeholder))
|> Day20.enhance(algorithm, placeholder)
|> Day20.display()
|> Enum.join()
|> String.replace("0", "")
|> String.length()

Part b

[algorithm | image] =
  input
  |> String.split("\n\n", trim: true)

algorithm =
  algorithm
  |> String.replace("#", "1")
  |> String.replace(".", "0")
  |> String.replace("\n", "")

placeholder = String.at(algorithm, Integer.parse("000000000", 2) |> elem(0))

starting_image =
  image
  |> hd()
  |> String.replace("#", "1")
  |> String.replace(".", "0")
  |> String.split("\n", trim: true)

Enum.reduce(0..24, starting_image, fn _x, acc ->
  acc
  |> Day20.enhance(algorithm, Day20.flip_placeholder(algorithm, placeholder))
  |> Day20.enhance(algorithm, placeholder)
end)
|> Day20.display()
|> Enum.join()
|> String.replace("0", "")
|> String.length()