Skip to content

chrislwade/aoc-2021

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

2021 Advent of Code (Ruby)

This repository implements solutions to the puzzles in the 2021 Advent of Code using Ruby.

Preface

Ruby used to be my go-to language but my day job has me doing the polyglot thing most of the time. I wanted to revitalize my Ruby knowledge which led me to use it for AoC 2021.

I'm certain some practices in this code are dated and frowned upon my modern Ruby developers. I'm open to suggestions and improvements!

Credits

  • John Dugan: I "stole" their README layout. Because I am horrible at documentation!
  • aucinc123: I borrowed their solution for Day 8, Part 2. Because my brain just wasn't finding a solution.
  • GeeksforGeeks: Because I haven't used DFS since college.
  • Matt Rix: I borrowed their solution for Day 12, Part 2. Because I haven't used DFS since college and was hitting a wall with adapting a single algorithm to allow revisiting.
  • William Y. Feng: I borrowed their solution for Day 13. Because, despite getting part 1 and 2 right, using the sample data, the algorithm I used (matrix flipping) didn't work because the second flip is a horizontal one that has more rows in the upper chunk. I could have worked through this but I decided to look for another approach and found this one. Strangely enough, this one does not produce the correct visual output for the sample data, but does for the real puzzle input.
  • William Y. Feng: I borrowed their solution for Day 18. Because, once again, DFS and tree traversal aren't something I do in my day job and while I mostly still follow the concepts, I'm a bit rusty on specifics.
  • msschmitt: I borrowed their solution for Day 21, Part 2. Because I worked out the frequencies stuff and the fact that this was really just another [Lanternfish][aoc-day6] problem, but hitting a wall realizing tracking the player 2 losses was important.

Getting Started

Prerequisites

The project requires ruby 3.0.2, but any reasonably current version of Ruby will likely work.

If you use a Ruby version manager that responds to .tool-versions, you should be switched to correct version automatically. I recommend ASDF for those on platforms that support it.

If you want to download the daily puzzle input programmatically, obtain your session cooking from the AoC website. See installation.

Installation

This project uses Bundler to manage dependencies. To install them simply run the following:

$ bundle install

The project provides a utility script to fetch the daily puzzle input programmatically. To enable this script, run the following:

$ echo 'session_cookie_value' > .env

File Structure

  • bin: Wrapper and utility scripts.
  • input: Puzzle input organized by day.
  • lib: Daily solutions and other bad programming.
  • spec: A meta-programmed spec to make testing easier.

Daily Solutions

Scaffolding Daily Solution

The project provides a scaffold script that will generate the boilerplate required to implement a new day.

It will attempt to download the puzzle input for the specified day if an ADVENT_SESSION environment variable is set and is a valid session cookie from the AoC website. This environment variable can also be provided in a .env thanks to the dotenv gem.

Below is an example usage and output:

$ ./bin/scaffold 11
Downloaded puzzle input and wrote .../input/day11.txt.
Wrote sample .../spec/fixtures/day11.txt.
**NOTE**: You must still copy the sample data into this file to enable testing of this day's puzzles.
Wrote scaffold .../lib/day11.rb.

Running Daily Solutions

The project presents a wrapper command (aoc) that loads the implementation for the specified day, processes the puzzle input, and generates output for both puzzle parts.

Below is an example usage and output:

$ ./bin/aoc 1
--- Day 01: Sonar Sweep ---
How many measurements are larger than the previous measurement?
- Puzzle 1: 1655
How many sums are larger than the previous sum?
- Puzzle 2: 1683

Running Tests

The tests for each day are automatically generated using meta-programming to simplify the task of implementing a new day.

The scaffolding implementation includes a class method (sample) to provide the expected sample answers from the puzzle page. These values must be fetched from the puzzle and provided as the value for the appropriate hash key: puzzle1 or puzzle2.

The scaffolding also includes a class method (expected) to provide the expected puzzle answers. This is provided as a method to be able to verify outputs after potential refactoring. This may be redundant given the sample testing, but it is provided for completeness. These values must be plugged in after validating the solution with the AoC website.

All tests are setup to be skipped until expected values are provided. Additionally, the new day's tests are focused until the values for sample and expected are all filled in.

To execute the tests run the following:

$ rspec
....................................................................................

Finished in 1 minute 51.64 seconds (files took 0.34171 seconds to load)
84 examples, 0 failures

About

Advent of Code 2021 - Ruby (https://adventofcode.com/2021)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages