While I've attempted earlier Advent of Code problems with Elixir, I find it's easier to work in Livebook. This is especially true with the Advent of Code Smart Cell and the included template https://github.com/ljgago/kino_aoc/blob/main/priv/livebook/aoc_template.livemd.
Up to 2022 Day 04 uses an early template that included the description of each part in markdown.
I find including it somewhat eliminates the need to switch back and forth to the AoC tab but I have not always been consistent.
It also takes non-trivial effort to properly annotate the links, code blocks, and color scheme.
When up against others from the beta DockYard Academy cohort, speed is a factor to feel like my score (at least in these early days) can match my peers on our private leaderboard.
I thought I wanted to go for brute forcing and speed but I realized that's the quickest way to not really learn anything. While my attempts will always strive to solve a problem on my own first, I plan on adding knowledge dumps here or on my journal. My peers are often thinking multiple steps ahead of me, doing things like altering the input to make transformation or navigation easier. I'm often walking on eggshells around the input and considering this is meant to be fun, we don't have to play by arbitrary rules.
- Clone the project using
git@github.com:w0rd-driven/advent-of-code-livebook-22.git
. - If using the
asdf
version manager, install the local versions by runningasdf install
. This may take a while. - Install Livebook.
- It's preferred to use the CLI to launch via the command
livebook server index.livemd
.
- Navigate to an existing puzzle below.
- Marvel at the often naive approach to solving each puzzle.
- Click on the button:
- Click
Run notebook
if your Livebook location is correct. - This should open the Livebook with the title
Advent of Code - Day XX
- Change the title to
Advent of Code 2022 :: Day 05
, changing the day number accordingly. - Save the new Livebook by clicking on the disk icon.
- Navigate to the existing directory in the modal.
- Name the file
day05.livemd
. - Click Save.
- Navigate to the last day completed.
- Click the three vertical dots at the very top of the Livebook.
- Click
Fork
. - Change the title from ex.
Advent of Code 2022 :: Day 04 - fork
toAdvent of Code 2022 :: Day 05
. - Save the new Livebook by clicking on the disk icon.
- Navigate to the existing directory in the modal.
- Click on
day04.livemd
for instance to set the name but do not save it. This is a shortcut to use the same format. - Change
04
to05
and now click Save.
- Configure the introduction to work from only the Livebook.
- Configure the Parser AOC helper that downloads the input.
- Use TDD to implement the Parser tests and
parser
function implementation.
- Navigate to the page you wish to introduce, i.e. https://adventofcode.com/2022/day/5
- On our new Livebook, double click the
--> Content
section to edit the markdown. - Copy the contents from
--- Day 5: Supply Stacks ---
down toTo begin, get your puzzle input.
. - Paste the contents into the markdown section.
- Prepend
###
in front of the--- Day 5: Supply Stacks ---
line to make it a heading. - Add appropriate spacing, bolding (encase in
**
), code blocks (three back ticks), code highlights (one backtick), and color highlights with<span>
tags.
This is applicable from Day 05 onward as the template moved to this pattern. I'm following suit after noticing it is a common theme to configure a parsing routine that is shared between both parts. We are also not bound by rules, we can completely alter the input to use familiar modules or patterns.
- Evaluate a code cell to download the helper and show the UI.
- Choose the
Year
(2022),Day
(05), and click theSession
to set theSet the Session ID
. - Click back to the tab for the description page, i.e. https://adventofcode.com/2022/day/5.
- Open your browser's DevTools.
- Navigate to the
Network
tab and refresh the page. - Click the 1st response, it should be
5
or correspond to the day. - Click the
Headers
tab. - Scroll down to the
Request Headers
section. - Look for the
cookie:
withsession=
and copy everything after the equals sign. This starts with5361
for me but will likely be different for you. - Click back to the Livebook with the
Set Session ID
modal still open. - Paste the cookie in the
Value
. Verify thesession=
isn't included as you'll have errors if it's present. - Type in the name
SESSION
. - Click the
Add +
button. - Click
Evaluate
to evaluate the cell. You should see{:ok, input results...}
- Find your test case input in the description.
- Change
@input ""
to@input """
and enclose in"""
- Change
@expected = nil
to the expected output of theparse
operation. - Implement the
parse
function to produce the expected output.- This should be shared output between both parts. You may start with part one and realize this must change for part two as well.
- Make sure your test passes.
- You may need to work backwards from the
Part One
implementation.
- You may need to work backwards from the
- Implement the same
@input
from theParser
section. We're coupling this bad boy. - Change
@expected = nil
to the expected result, typically toward the end of the description. For Day 05, that should be@expected = "CMZ"
- In the
run
function, your first call should be toParser.parse(input)
to parse the input into an expected format. This may be decoupled later when I've done enough of these. - Implement the rest of the
run
function to produce the test output. For Day 05 that should beCMZ
. - Make sure your test passes.
- Repeat the steps for the main introduction.
- There is currently no Part Two Introduction section so we'll add it.
- Above the section marked
Code
click+ Block
,Section
to insert a new section. - Title this
Part Two Introduction
as it should be unique. - It may make more sense to append part two to the introduction at the top. I'll likely end up doing this even though scrolling could get annoying.
- Pro tip: use CMD+Up or CMD+Down outside of a cell to quickly scroll to the top or bottom of your browser tab. This works for all major browsers.
- Implement the same
@input
from theParser
section. We're coupling this bad boy. - Change
@expected = nil
to the expected result, typically toward the end of the description. For Day 05, that should be@expected = "CMZ"
- In the
run
function, your first call should be toParser.parse(input)
to parse the input into an expected format. This may be decoupled later when I've done enough of these. - Implement the rest of the
run
function to produce the test output. For Day 05 that should beCMZ
. - Make sure your test passes.
You aren't required to but ideally we should refactor what we've written to be concise. Normally, I don't do this until I've needed something 3 or more times but it's a good idea to be in this habit. It will be especially easy to notice better ways of producing the same end results. By having tests, we can also be confident in our changes. I highly recommend committing the Livebook to git before refactoring as I like to have as many ugly steps as possible. I think it's important for me to show that I'm not perfect out of the gate, often very far from it and that is okay. Refactoring is a luxury, not a requirement. Ugly code will become better the more you work on it.