1defmodule Aoc2025.Utils do
2 @moduledoc """
3 Common functions we can use for all exercises
4 """
5
6 @doc """
7 Check if input exists in the /priv folder, else download it from
8 adventofcode.com
9
10 The cookie `AOC_COOKIE` is expected to be set in the environment.
11 """
12 def get_input(day) do
13 day_str = String.pad_leading("#{day}", 2, "0")
14 path = "priv/day#{day_str}.txt"
15
16 case File.read(path) do
17 {:ok, input} ->
18 input
19
20 {:error, _} ->
21 {:ok, %{body: input}} =
22 Req.get("https://adventofcode.com/2025/day/#{day}/input",
23 headers: [{"cookie", "session=#{System.fetch_env!("AOC_COOKIE")}"}]
24 )
25
26 File.write!(path, input)
27 input
28 end
29 end
30end