AOC day 3
Any elixir enjoyers tell me why this solution doesn't work for aoc day 3? I tried to use a parser combinator lib (NimbleParsec) cause my one true love haskell would've handled this in 1 loc lemon squeezy with parsec defmodule Aoc24.Day3 do import NimbleParsec def run do File.read!("inputs/day3.txt") |> String.replace(~r/\s+/, "") |> process() end mul_parser = ignore(string("mul(")) |> concat(integer(min: 1, max: 3)) |> ignore(string(",")) |> concat(integer(min: 1, max: 3)) |> ignore(string(")")) |> reduce({List, :to_tuple, []}) mem_parser = repeat( choice([ mul_parser, utf8_string([not: ?m], min: 1) |> ignore() ]) ) defparsec(:parse_memory, mem_parser) def process(input) do {:ok, parsed, _, _, _, _} = parse_memory(input) Enum.map(parsed, fn {x, y} -> x * y end) |> Enum.sum() |> IO.inspect() end end
23d
31
Is regex cheating?
```
defmodule Aoc3 do
def main() do
input = File.read!("day3.txt") |> String.trim()
regex = ~r"mul\((\d{1,3}),(\d{1,3})\)"
matches = Regex.scan(regex, input)
products = for [_, num1_str, num2_str] <- matches do
num1 = String.to_integer(num1_str)
num2 = String.to_integer(num2_str)
num1 * num2
end
total = Enum.sum(products)
IO.puts(total)
end
end
Aoc3.main()
```
We need markdown support on comments so we can have code blocks! (Or something similar)