This is my second year doing AoC in Elixir and my first year doing it with Livebook.
When I was doing just plain Elixir I usually set up each day as an exs script and ran it with elixir 2021-day1.exs 2021-1input.txt That way I could save the example input from the problem setup, and the user-specific input and switch back and forth.
@josevalim did it in livebook last year and I was looking at his code where he used Kino inputs (example) and copy/pasted the AoC input and ran it. This works great, but your input is not persisted across sessions.
I’ve tried reading a file from the Livebook, putting it in the same directory as my .livemd file and reading the file as file1.txt and ./file1.txt, but that’s not right. The file is not found. I’m guessing the path is relative to the livebook process, which might be different depending on how you’re running livebook (escript, livebook.app, or hosting it on a webserver).
Are there any other tips for reading a file from within a livebook? Whether it’s on the livebook server or from the browser with Kino, a smart cell, something else?
In particular, I am using a Utilities module to provide the data input file reading:
defmodule Utilities do
Provides utility functions to be used across days
Reads the given day's data file of "day_<zero padded day number>_input.txt" as
@spec readDataStream(integer()) :: Stream.t()
def readDataStream(day) do
|> String.pad_leading(2, "0")
I download all of the input files and name them as day_<number>_input.txt. For example, day_01_input.txt. I put them in a data folder up a level from the notebook because the intention is to potentially support several languages but keep the input files in a single location for a given year.
I also have tests setup to help in refactoring after I have solved a puzzle.
defmodule AdventOfCode.Tests do
use ExUnit.Case, async: true
test "Day 1" do
assert Day1.part_one() == # redacted
assert Day1.part_two() == # redacted
test "Day 2" do
assert Day2.PartOne.solution() == # redacted
assert Day2.PartTwo.solution() == # redacted