Skip to content

Instantly share code, notes, and snippets.

@Dzol
Last active December 9, 2022 15:36
Show Gist options
  • Save Dzol/1dda6dfc4430012b09b7ace784628a6d to your computer and use it in GitHub Desktop.
Save Dzol/1dda6dfc4430012b09b7ace784628a6d to your computer and use it in GitHub Desktop.

AOC2022

Mix.install([{:finch, "~> 0.14.0"}])
Finch.start_link(name: NBFinch)

AOC Input Helper

defmodule AOC do
  def input(day) do
    year = 2022
    link = "https://adventofcode.com/#{year}/day/#{day}/input"

    cookie = "session=#{System.fetch_env!("LB_AOC_SESSION_TOKEN")}"

    req = Finch.build(:get, link, [{"cookie", cookie}])
    {:ok, resp} = Finch.request(req, NBFinch)
    resp.body
  end
end

Day I Input

i01 = AOC.input(1)

Part I

i01
|> String.split("\n\n")
|> Enum.map(fn x -> x |> String.split() |> Enum.map(&String.to_integer/1) |> Enum.sum() end)
|> Enum.sort(:desc)
|> List.first()

Part II

i01
|> String.split("\n\n")
|> Enum.map(fn x -> x |> String.split() |> Enum.map(&String.to_integer/1) |> Enum.sum() end)
|> Enum.sort(:desc)
|> Enum.take(3)
|> Enum.sum()

Day II Common Set-Up & Input

you = %{
  "A" => :rock,
  "X" => :rock,
  "B" => :paper
}

me = %{
  "Y" => :paper,
  "C" => :scissors,
  "Z" => :scissors
}

code = Map.merge(you, me)

score = %{rock: 1, paper: 2, scissors: 3}

{lost, drew, won} = {0, 3, 6}

outcome = %{
  {:rock, :rock} => drew,
  {:rock, :paper} => won,
  {:rock, :scissors} => lost,
  {:paper, :rock} => lost,
  {:paper, :paper} => drew,
  {:paper, :scissors} => won,
  {:scissors, :rock} => won,
  {:scissors, :paper} => lost,
  {:scissors, :scissors} => drew
}
i02 = AOC.input(2)

Part I

i02
|> String.split("\n", trim: true)
|> Enum.map(&String.split/1)
|> List.flatten()
|> Enum.map(&Map.fetch!(code, &1))
|> Enum.chunk_every(2)
|> Enum.map(&List.to_tuple/1)
|> Enum.map(fn {_, me} = play ->
  score[me] + outcome[play]
end)
|> Enum.sum()

Part II

you = %{
  "A" => :rock,
  "B" => :paper,
  "C" => :scissors
}

me = %{
  "X" => :lose,
  "Y" => :draw,
  "Z" => :win
}

code = Map.merge(you, me)

play = %{
  {:rock, :lose} => :scissors,
  {:rock, :draw} => :rock,
  {:rock, :win} => :paper,
  {:paper, :lose} => :rock,
  {:paper, :draw} => :paper,
  {:paper, :win} => :scissors,
  {:scissors, :lose} => :paper,
  {:scissors, :draw} => :scissors,
  {:scissors, :win} => :rock
}
i02
|> String.split("\n", trim: true)
|> Enum.map(&String.split/1)
|> List.flatten()
|> Enum.map(&Map.fetch!(code, &1))
|> Enum.chunk_every(2)
|> Enum.map(&List.to_tuple/1)
|> Enum.map(fn {_, outcome} = strat ->
  score[play[strat]] + %{lose: 0, draw: 3, win: 6}[outcome]
end)
|> Enum.sum()

Day IX Part I

defmodule Day9Part1 do
  def adjecent?({x, y} = _h, t) do
    square = for dx <- [-1, 0, 1], dy <- [-1, 0, 1], do: {x + dx, y + dy}
    t in square
  end

  def same_row?({_x, y}, {_u, v}), do: y === v

  def same_col?({x, _y}, {u, _v}), do: x === u

  def h_trace(i) do
    parity = %{
      "L" => {-1, 0},
      "R" => {+1, 0},
      "U" => {0, +1},
      "D" => {0, -1}
    }

    i
    |> String.split()
    |> Enum.chunk_every(2)
    |> Enum.map(&List.to_tuple/1)
    |> Enum.map(fn {d, i} -> {d, String.to_integer(i)} end)
    |> Enum.map(fn {d, i} -> for _ <- 1..i, do: parity[d] end)
    |> List.flatten()
    |> Enum.scan({0, 0}, fn {dx, dy}, {x, y} -> {x + dx, y + dy} end)
  end

  def t_trace(h, {tx, ty} = t) do
    cond do
      adjecent?(h, t) ->
        t

      same_row?(h, t) ->
        opts = [
          {tx + 1, ty},
          {tx - 1, ty}
        ]

        Enum.find(opts, &adjecent?(h, &1))

      same_col?(h, t) ->
        opts = [
          {tx, ty + 1},
          {tx, ty - 1}
        ]

        Enum.find(opts, &adjecent?(h, &1))

      true ->
        opts = [
          {tx + 1, ty + 1},
          {tx + 1, ty - 1},
          {tx - 1, ty + 1},
          {tx - 1, ty - 1}
        ]

        Enum.find(opts, &adjecent?(h, &1))
    end
  end
end
import Day9Part1
i09 = AOC.input(09)
i09
|> h_trace()
|> Enum.scan({0, 0}, &t_trace/2)
|> Enum.uniq()
|> Enum.count()

Day IX Part II

i09
|> h_trace()
|> Enum.scan({0, 0}, &t_trace/2)
|> Enum.scan({0, 0}, &t_trace/2)
|> Enum.scan({0, 0}, &t_trace/2)
|> Enum.scan({0, 0}, &t_trace/2)
|> Enum.scan({0, 0}, &t_trace/2)
|> Enum.scan({0, 0}, &t_trace/2)
|> Enum.scan({0, 0}, &t_trace/2)
|> Enum.scan({0, 0}, &t_trace/2)
|> Enum.scan({0, 0}, &t_trace/2)
|> Enum.uniq()
|> Enum.count()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment