From ef0b86dbfb17bfdea9d37016c950b254619069ca Mon Sep 17 00:00:00 2001 From: sloane Date: Tue, 3 Dec 2024 07:50:45 -0500 Subject: [PATCH] solve 2024 day 3 --- 2024/README.md | 3 +- 2024/lib/2024/3.ex | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 2024/mix.exs | 1 + 2024/mix.lock | 1 + README.md | 4 +-- 5 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 2024/lib/2024/3.ex diff --git a/2024/README.md b/2024/README.md index 195afbe..88d41bf 100644 --- a/2024/README.md +++ b/2024/README.md @@ -2,10 +2,11 @@ | S | M | T | W | T | F | S | | :-: | :-: | :-: | :-: | :-: | :-: | :-: | -| [1] | [2] | 3 | 4 | 5 | 6 | 7 | +| [1] | [2] | [3] | 4 | 5 | 6 | 7 | | 8 | 9 | 10 | 11 | 12 | 13 | 14 | | 15 | 16 | 17 | 18 | 19 | 20 | 21 | | 22 | 23 | 24 | 25 | | | | [1]: ./lib/2024/1.ex [2]: ./lib/2024/2.ex +[3]: ./lib/2024/3.ex diff --git a/2024/lib/2024/3.ex b/2024/lib/2024/3.ex new file mode 100644 index 0000000..679f15d --- /dev/null +++ b/2024/lib/2024/3.ex @@ -0,0 +1,76 @@ +import AOC + +aoc 2024, 3 do + import NimbleParsec + + def p1(input) do + input + |> read_progam() + |> Enum.map(&eval/1) + |> Enum.sum() + end + + def p2(input) do + {answer, _} = + input + |> read_progam() + |> Enum.reduce({0, :do}, fn + {:dont, _}, {sum, _} -> {sum, :dont} + {:do, _}, {sum, _} -> {sum, :do} + _inst, {_sum, :dont} = acc -> acc + inst, {sum, :do} -> {sum + eval(inst), :do} + end) + + answer + end + + defcombinatorp( + :mul, + "mul(" + |> string() + |> ignore() + |> integer(min: 1) + |> ignore(string(",")) + |> integer(min: 1) + |> ignore(string(")")) + |> tag(:mul) + ) + + defcombinatorp( + :do, + "do()" + |> string() + |> ignore() + |> tag(:do) + ) + + defcombinatorp( + :dont, + "don't()" + |> string() + |> ignore() + |> tag(:dont) + ) + + defparsecp( + :program_parsec, + [ + parsec(:mul), + parsec(:do), + parsec(:dont) + ] + |> choice() + |> eventually() + |> repeat() + ) + + def read_progam(input) do + {:ok, program, _rest, _, _, _} = program_parsec(input) + + program + end + + defp eval({:mul, [a, b]}), do: a * b + defp eval({:do, _}), do: 0 + defp eval({:dont, _}), do: 0 +end diff --git a/2024/mix.exs b/2024/mix.exs index ff15d14..ddbeada 100644 --- a/2024/mix.exs +++ b/2024/mix.exs @@ -22,6 +22,7 @@ defmodule Aoc2024.MixProject do defp deps do [ {:advent_of_code_utils, "~> 4.0"}, + {:nimble_parsec, "~> 1.4"}, {:styler, "~> 1.2", only: [:dev, :test], runtime: false} ] end diff --git a/2024/mix.lock b/2024/mix.lock index d127457..410e89f 100644 --- a/2024/mix.lock +++ b/2024/mix.lock @@ -1,6 +1,7 @@ %{ "advent_of_code_utils": {:hex, :advent_of_code_utils, "4.0.1", "591073a49600cbceae6d92e0fb2d2c56b59f6d383851e2a7c00c3bf0e4f33f4f", [:mix], [{:floki, "~> 0.34", [hex: :floki, repo: "hexpm", optional: false]}, {:tz, "~> 0.26", [hex: :tz, repo: "hexpm", optional: false]}], "hexpm", "684d016883d7b5443d9d22abc34013fb6f1d5d9ff114859a40890545a570eec8"}, "floki": {:hex, :floki, "0.36.3", "1102f93b16a55bc5383b85ae3ec470f82dee056eaeff9195e8afdf0ef2a43c30", [:mix], [], "hexpm", "fe0158bff509e407735f6d40b3ee0d7deb47f3f3ee7c6c182ad28599f9f6b27a"}, + "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"}, "styler": {:hex, :styler, "1.2.1", "28f9e3d4b065c22575c56b8ae03d05188add1b21bec5ae664fc1551e2dfcc41b", [:mix], [], "hexpm", "71dc33980e530d21ca54db9c2075e646faa6e7b744a9d4a3dfb0ff01f56595f0"}, "tz": {:hex, :tz, "0.28.1", "717f5ffddfd1e475e2a233e221dc0b4b76c35c4b3650b060c8e3ba29dd6632e9", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:mint, "~> 1.6", [hex: :mint, repo: "hexpm", optional: true]}], "hexpm", "bfdca1aa1902643c6c43b77c1fb0cb3d744fd2f09a8a98405468afdee0848c8a"}, } diff --git a/README.md b/README.md index 3d25c55..7e678ac 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ | [2021] | **43/50** 🌟 | Elixir | | [2022] | **14/50** 🌟 | Elixir, Haskell | | [2023] | **19/50** 🌟 | Elixir, Haskell | -| [2024] | **4/50** 🌟 | Elixir | -| **Total** | **160** 🌟 | | +| [2024] | **6/50** 🌟 | Elixir | +| **Total** | **162** 🌟 | | [2015]: ./2015 [2017]: ./2017