Powered by AppSignal & Oban Pro

Advent of code - Day 3

day_3.livemd

Advent of code - Day 3

Partie 1

inputs =
  "inputs/day3.txt"
  |> File.read!()
  |> String.trim()
  |> String.split("\n")
defmodule Score do
  def score(input) do
    {a, b} = String.split_at(input, trunc(String.length(input) / 2))

    case String.myers_difference(a, b)[:eq] do
      <> <> _ when c <= ?Z -> c - ?A + 27
      <> <> _ -> c - ?a + 1
    end
  end
end
inputs
|> Enum.map(&amp;Score.score/1)
|> Enum.sum()

Partie 2

inputs2 =
  """
  vJrwpWtwJgWrhcsFMMfFFhFp
  jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
  PmmdzqPrVvPwwTWBwg
  wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
  ttgJtRGJQctTZtZT
  CrZsJsPPZsGzwwsLwLmpwMDw
  """
  |> String.trim()
  |> String.split("\n")
defmodule Score2 do
  def score([i1, i2, i3]) do
    i1
    |> String.graphemes()
    |> MapSet.new()
    |> MapSet.intersection(MapSet.new(String.graphemes(i2)))
    |> MapSet.intersection(MapSet.new(String.graphemes(i3)))
    |> MapSet.to_list()
    |> Enum.at(0)
    |> case do
      <> <> _ when c <= ?Z -> c - ?A + 27
      <> <> _ -> c - ?a + 1
    end
  end
end

inputs
|> Enum.chunk_every(3)
|> Enum.map(&amp;Score2.score/1)
|> Enum.sum()