diff options
author | Alexandre Jesus <adbjesus@gmail.com> | 2024-12-07 16:10:00 +0000 |
---|---|---|
committer | Alexandre Jesus <adbjesus@gmail.com> | 2024-12-07 16:10:00 +0000 |
commit | 1c1bc2ea6e5b45b7cb7377cd5e15645318bf1647 (patch) | |
tree | 4659de8dd0c252e20e7845c9b93b2cfc73c535d9 | |
parent | 8362e80cbdda472c2a92d9566e511cbce5a9ecf5 (diff) | |
download | aoc2024-1c1bc2ea6e5b45b7cb7377cd5e15645318bf1647.tar.gz aoc2024-1c1bc2ea6e5b45b7cb7377cd5e15645318bf1647.zip |
Day 7 (and small update to day 6)
-rw-r--r-- | src/day06.exs | 14 | ||||
-rw-r--r-- | src/day07.exs | 77 |
2 files changed, 83 insertions, 8 deletions
diff --git a/src/day06.exs b/src/day06.exs index 09a1604..8e339ff 100644 --- a/src/day06.exs +++ b/src/day06.exs @@ -32,15 +32,13 @@ defmodule Day06 do {v, j} <- Enum.with_index(String.codepoints(line)) do if v == element do {i, j} - else - nil end end |> Enum.filter(& &1 != nil) end - def part1({i, j}, grid) do - path({i, j}, :up, grid) + def part1({guard, grid, _, _}) do + path(guard, :up, grid) |> Enum.map(fn {i, j, _} -> {i, j} end) |> Enum.uniq() |> Enum.count() @@ -89,7 +87,7 @@ defmodule Day06 do end end - def part2(guard, grid, overt, ohori) do + def part2({guard, grid, overt, ohori}) do path(guard, :up, grid) |> Enum.map(fn {i, j, _} -> {i, j} end) |> Enum.uniq() @@ -160,12 +158,12 @@ defmodule Day06 do end end -{guard, grid, overt, ohori} = IO.read(:stdio, :eof) |> Day06.parse_data() +data = IO.read(:stdio, :eof) |> Day06.parse_data() -{time1 , ans1} = :timer.tc(fn -> Day06.part1(guard, grid) end) +{time1 , ans1} = :timer.tc(fn -> Day06.part1(data) end) IO.puts("Time : #{time1 / 1000000}") IO.puts("Answer: #{ans1}") -{time2 , ans2} = :timer.tc(fn -> Day06.part2(guard, grid, overt, ohori) end) +{time2 , ans2} = :timer.tc(fn -> Day06.part2(data) end) IO.puts("Time : #{time2 / 1000000}") IO.puts("Answer: #{ans2}") diff --git a/src/day07.exs b/src/day07.exs new file mode 100644 index 0000000..1adeb54 --- /dev/null +++ b/src/day07.exs @@ -0,0 +1,77 @@ +defmodule Utils do + def parse_integer_list(string, split, trim \\ true) do + string + |> String.split(split, trim: trim) + |> Enum.map(& String.to_integer(&1)) + end +end + +defmodule Equation do + @derive Inspect + defstruct [:result, :terms] + + def from_string(string) do + string + |> Utils.parse_integer_list([":", " "]) + |> then(& %Equation{result: hd(&1), terms: tl(&1)}) + end + + def has_lr_solution(equation, ops) do + has_lr_solution(hd(equation.terms), tl(equation.terms), equation.result, ops) + end + + defp has_lr_solution(acc, terms, result, ops) do + cond do + acc > result -> + false + terms == [] and acc == result -> + true + terms == [] -> + false + true -> + ops + |> Enum.any?(& has_lr_solution(apply_op(acc, hd(terms), &1), tl(terms), result, ops)) + end + end + + defp apply_op(l, r, op) do + case op do + :add -> + l + r + :mul -> + l * r + :concat -> + String.to_integer("#{l}#{r}") + end + end +end + +defmodule Day07 do + def part1(data) do + data + |> String.split("\n", trim: true) + |> Enum.map(& Equation.from_string(&1)) + |> Enum.filter(& Equation.has_lr_solution(&1, [:mul, :add])) + |> Enum.map(& &1.result) + |> Enum.sum() + end + + def part2(data) do + data + |> String.split("\n", trim: true) + |> Enum.map(& Equation.from_string(&1)) + |> Enum.filter(& Equation.has_lr_solution(&1, [:mul, :concat, :add])) + |> Enum.map(& &1.result) + |> Enum.sum() + end +end + +data = IO.read(:stdio, :eof) + +{time1 , ans1} = :timer.tc(fn -> Day07.part1(data) end) +IO.puts("Time : #{time1 / 1000000}") +IO.puts("Answer: #{ans1}") + +{time2 , ans2} = :timer.tc(fn -> Day07.part2(data) end) +IO.puts("Time : #{time2 / 1000000}") +IO.puts("Answer: #{ans2}") |