diff --git a/2024/day13_wip.exs b/2024/day13_wip.exs new file mode 100755 index 0000000..aa61de6 --- /dev/null +++ b/2024/day13_wip.exs @@ -0,0 +1,81 @@ +#!/usr/bin/env elixir +defmodule Day13 do + defmodule Machine do + defstruct [:a, :b, :prize] + end + + def part1(input) do + input + # these are equivalent linear equations + # know how to do it algebraically, but not algorithmically + # e.g. for first example + # + # 94a + 22b - 8400 = 34a + 67b - 5400 + # 94a + 22b - 3000 = 34a + 67b + # 60a + 22b - 3000 = 67b + # 60a - 3000 = 45b + # 4a - 200 = 3b + # a = 3/4b + 50 + # 34(3/4b + 50) + 67b - 5400 = 0 + # 25.5b + 1700 + 67b - 5400 = 0 + # 92.5b = 3700 + # b = 40 + # 34a + 67(40) = 5400 + # 34a + 2680 = 5400 + # 34a = 2720 + # a = 80 + end + + def part2(_input) do + :ok + end + + def input do + with [input_filename] <- System.argv(), + {:ok, input} <- File.read(input_filename) do + input + |> String.split("\n\n") + |> Enum.map(fn machine -> + machine + |> String.split(["+", ",", "=", "\n"], trim: true) + |> tl() + |> Enum.take_every(2) + |> Enum.map(&String.to_integer/1) + end) + else + _ -> :error + end + end + + ####################### + # HERE BE BOILERPLATE # + ####################### + + def run do + case input() do + :error -> print_usage() + input -> run_parts_with_timer(input) + end + end + + defp run_parts_with_timer(input) do + run_with_timer(1, fn -> part1(input) end) + run_with_timer(2, fn -> part2(input) end) + end + + defp run_with_timer(part, fun) do + {time, result} = :timer.tc(fun) + IO.puts("Part #{part} (completed in #{format_time(time)}):\n") + IO.puts("#{inspect(result)}\n") + end + + defp format_time(μsec) when μsec < 1_000, do: "#{μsec}μs" + defp format_time(μsec) when μsec < 1_000_000, do: "#{μsec / 1000}ms" + defp format_time(μsec), do: "#{μsec / 1_000_000}s" + + defp print_usage do + IO.puts("Usage: elixir day13.exs input_filename") + end +end + +# Day13.run()