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

Advent of Code 2015 Day 11 Part 1

2015_day11_part1.livemd

Advent of Code 2015 Day 11 Part 1

Mix.install([
  {:kino_aoc, "~> 0.1"}
])

Get Inputs

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2015", "11", System.fetch_env!("LB_SESSION"))

My answer

has_straight_str = fn str ->
  Regex.match?(
    ~r/(?=abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz)/,
    str
  )
end
has_straight_str.("abcd")
has_straight_str.("abdc")
has_no_iol = fn str ->
  Regex.match?(~r/^[^iol]*$/, str)
end
has_no_iol.("abcd")
has_no_iol.("abid")
has_double_double = fn str ->
  Regex.match?(~r/(.)\1.*(.)\2/, str)
end
has_double_double.("abcc")
has_double_double.("aabcc")
has_double_double.("ababbcc")
valid_password? = fn password ->
  has_straight_str.(password) && has_no_iol.(password) && has_double_double.(password)
end
valid_password?.("hijklmmn")
valid_password?.("abbceffg")
valid_password?.("abbcegjk")
valid_password?.("abcdffaa")
valid_password?.("ghjaabcc")
<> = "a"
a_code
to_alphabet_num = fn str ->
  <<code>> = str
  code - 97
end
Enum.map(?a..?z, &amp;<<&amp;1>>)
|> Enum.map(&amp;to_alphabet_num.(&amp;1))
from_alphabet_num = fn num ->
  <<(num + 97)>>
end
0..25
|> Enum.map(&amp;from_alphabet_num.(&amp;1))
:binary.match("abc", "a")
:binary.match("abc", "x")
skip_iol = fn password ->
  index_map =
    ["i", "o", "l"]
    |> Enum.reduce(%{}, fn char, index_map ->
      case :binary.match(password, char) do
        {index, _} -> Map.put(index_map, char, index)
        _ -> index_map
      end
    end)

  if index_map == %{} do
    password
  else
    index_map
    |> Enum.min_by(fn {_, index} -> index end)
    |> then(fn {char, index} ->
      password
      |> String.codepoints()
      |> Enum.with_index()
      |> Enum.map(fn {code, code_index} ->
        cond do
          code_index == index ->
            case char do
              "i" -> "j"
              "o" -> "p"
              "l" -> "m"
              _ -> code
            end
          code_index > index ->
            "a"
          true ->
            code
        end
      end)
      |> Enum.join()
    end)
  end
end
skip_iol.("ghijklmn")
next_password = fn current ->
  current
  |> String.codepoints()
  |> Enum.reverse()
  |> Enum.with_index()
  |> Enum.reduce(0, fn {str, digit}, num ->
    num + to_alphabet_num.(str) * (26**digit)
  end)
  |> Kernel.+(1)
  |> Integer.digits(26)
  |> Enum.map(&amp;from_alphabet_num.(&amp;1))
  |> Enum.join()
  |> skip_iol.()
end
next_password.("cba")
next_password.("ghijklmn")
find_new_password = fn current_password ->
  0..10000000
  |> Enum.reduce_while(current_password, fn _, password ->
    new_password = next_password.(password)
    if valid_password?.(new_password) do
      {:halt, new_password}
    else
      {:cont, new_password}
    end
  end)
end
find_new_password.("ghijklmn")
find_new_password.(puzzle_input)