gangstead

gangstead

Advent of Code livebook setup

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?

Marked As Solved

stevensonmt

stevensonmt

maybe try using "#{__DIR__}/file1.txt" as the file path?

Also Liked

gangstead

gangstead

That works!

I’m still interested if there’s any way to do a file upload into a livebook. Maybe that’s a smart cell or something I could develop?

bmitc

bmitc

I am also using Livebook, and you can find my 2022 notebook here: advent-of-code/advent_of_code_2022.livemd at main · bmitc/advent-of-code · GitHub

Warning: Solutions are there for days 1 and 2.

In particular, I am using a Utilities module to provide the data input file reading:

defmodule Utilities do
  @moduledoc """
  Provides utility functions to be used across days
  """

  @doc """
  Reads the given day's data file of "day_<zero padded day number>_input.txt" as
  a stream
  """
  @spec readDataStream(integer()) :: Stream.t()
  def readDataStream(day) do
    day_as_string =
      day
      |> Integer.to_string()
      |> String.pad_leading(2, "0")

    Path.join(__DIR__, "../data/day_#{day_as_string}_input.txt")
    |> Path.expand()
    |> File.stream!()
    |> Stream.map(&String.trim/1)
  end
end

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.

ExUnit.start(autorun: false)

defmodule AdventOfCode.Tests do
  use ExUnit.Case, async: true

  test "Day 1" do
    assert Day1.part_one() == # redacted
    assert Day1.part_two() == # redacted
  end

  test "Day 2" do
    assert Day2.PartOne.solution() == # redacted
    assert Day2.PartTwo.solution() == # redacted
  end
end

ExUnit.run()
mruoss

mruoss

I’m using kino_aoc. A very handy tool imo.

Where Next?

Popular in Challenges Top

Aetherus
This topic is about Day 3 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
Aetherus
This topic is about the Advent of Code 2021 - Day 4. Thanks to @bjorng , we now have a new Private Leaderboard. The entry code is: 370...
New
kwando
Phew, this one took a while to get right. My naive attempts was way to slow so I reached for Dijkstras shortest path algorithm… and that ...
New
bjorng
This topic is about Day 18 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
maennchen
Ok, that was a rough one today. I haven’t found a way to improve the algorithm further. Part 1 runs in .5 seconds, Part 2 in ~ 5 minutes...
New
Aetherus
This topic is about Day 4 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
stevensonmt
Anyone else think the prompt for this challenge is contradictory? The rules for comparing packets include If both values are lists, c...
New
rugyoga
Fairly straightforward Dijkstra’s algorithm import AOC aoc 2023, 17 do def compute(input, candidates) do {{max_row, max_col}, ite...
New
kwando
Took a while, but another use case for “move vectors” today and pattern matching. :slight_smile: The trick was to first generate a list...
New
seeplusplus
Hello all, hopefully I post this before someone else does and I don’t dupe. IMO Day 4 was much easier than Day 3 (yay, I can sleep befor...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement