Skip to content

Commit

Permalink
Solve day 3 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
petros committed Dec 6, 2024
1 parent 09fd75e commit c3acb83
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/day03/solution.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,29 @@ defmodule Aoc2024.Day03.Solution do
|> Enum.map(fn x -> Enum.product(x) end)
|> Enum.sum()
end

@doc """
# Examples
iex> Aoc2024.Day03.Solution.split_parts("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))")
["xmul(2,4)&mul[3,7]!^", "don't", "()_mul(5,5)+mul(32,64](mul(11,8)un", "do", "()?mul(8,5))"]
"""
def split_parts(memory) do
Regex.split(~r/don't|do/, memory, include_captures: true, trim: true)
end

@doc """
# Examples
iex> Aoc2024.Day03.Solution.solve_part2("xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))")
48
"""
def solve_part2(memory) do
"do#{memory}"
|> Aoc2024.Day03.Solution.split_parts()
|> Enum.chunk_every(2)
|> Enum.reject(fn x -> List.first(x) == "don't" end)
|> List.flatten()
|> Enum.reject(fn x -> x == "do" end)
|> List.to_string()
|> Aoc2024.Day03.Solution.solve_part1()
end
end

0 comments on commit c3acb83

Please sign in to comment.