My solutions for the Advent of Code in NodeJS
Each day consists of two puzzles.
I added a README.md file to each Day, which contains the instructions exactly as they were displayed on https://adventofcode.com/
- Open up your favourite terminal (and navigate somewhere you want to download the repository to).
- Make sure you have NodeJS installed. Test by entering
$node -v
If this returns a version number, NodeJS is installed. If not, get NodeJS here. - Clone the repository and navigate to it.
$git clone https://github.com/NullDev/Advent-of-Code.git && cd Advent-of-Code
- Check out the template branch in your fork from my remote. (IF YOU SEE THIS: YOU ARE ON THE WRONG BRANCH)
$git checkout -b template origin/template
- Push your newly created branch and make sure to set it as your default branch on GitHub (
REPO/settings/branches
). - Install all dependencies by typing
$npm install
- Remove all years that weren't made by you (if all:
rm -r "./20*"
) and maybe alter the README - Copy
config.template.json
and paste it asconfig.json
- Go to https://adventofcode.com/ and login with your account. Then copy the value from the
session
cookie and paste it into theconfig.json
file (See here if you don't know how). - (OPTIONAL) If you wish to use the GitHub Action for the "Prepare Day automatically at 0:10 workflow" you need to go to your repository settings -> secrets -> actions and add
SESSION_COOKIE
with your session string.
- To automatically setup and prepare the current day, simply type
npm run prepare-today
- If you finished part one and want to get the readme updated, simple run the command again.
- Optionally, to prepare a previous day, type
node prepare_day.js YEAR-DAY
- Example:node prepare_day.js 2020-12
Both of those commands will create a folder for the day/year, fetch the task from the website & convert it to a README.md, fetch the input and create template files for the solution.
Each script can be run stand-alone / separatly but I've also created a start_all.js
script to launch all days in order, and display the solutions along with an approximated benchmark (the benchmark uses performance.now()
to measure the execution time and does not include the actual reading of the file except when the file is read line-by-line).
- Run all years:
npm run start:all
- Run a specific year:
npm run start:YEAR
- Example:npm run start:2020
- Run a specific year and day:
npm run start:YEAR DAY
- Example:npm run start:2020 3
- Run todays parts:
npm run start:today
- Run a specific day & part:
node YEAR/DAY/part_[1 OR 2].js
- Example:node 2020/01/part_2.js
- All Puzzles & Inputs (
Day_XX/README.md
's andDay_XX/input.txt
's) Copyright (c) Eric Wastl (@ericwastl) - All Puzzle Solutions1 (
Day_XX/part_1.js
's andDay_XX/part_2.js
's) Copyright (c) NullDev (nulldev.org)
I attempted to solve every problem as functional as possible and with as little code as possible while still being performant.
Some solutions could be one-liners but I left them splitted up for the sake of readability.
I also tried to experiment with a couple of different things:
- Single-Liner in Day_01/part_1.js
- Single-Liner on Day 04 in both part_1 and part_2
- Solution with logical XOR-ing in Day_02/part_2.js
- Theoretical single-liner (single chain function) in Day_04/part_1.js
- Completely branchless approach in Day_04/part_2.js
- Another theoretical single-liner in Day_06/part_1.js and Day_06/part_2.js
- Recursive curried function in Day_07/part_1.js
- Another theoretical one-liner in Day_10/part_1.js
- More branchless solutions in Day_12/part_1.js and Day_12/part_2.js
- Yet another single-liner in Day_13/part_1.js
- Experiment with bit-shift, bitwise operations and bigint numbers in Day_14/part_2.js
- Another theoretical one-liner in Day_16/part_1.js
- Recursive IIFE arrow function to minimize code in Day_17/part_1.js
- Theoretical one-liner by utilizing the Lambda calculus Y-combinator with an recursive anonymous arrow function in Day_18/part_1.js
- Theoretical (almost) one-liner in Day_20/part_1.js
- The final one-liner in Day_25/part_1.js
- Single-Liner on Day 01 in both part_1 and part_2
- Solution in
O(n)
on Day 06 in both part_1 and part_2 (See the Note) - Single-Liner on Day 07 in both part_1 and part_2 by using a formula transformation
- Single-Liner in Day_08/part_1.js
- Another Single-Liner in Day_09/part_1.js
- Theoretical (almost) SIngle-Liner in Day_10/part_2.js
- Single-Liner in Day_13/part_1.js
- Single-Liner in Day_17/part_1.js
- Singe-Liner in Day_21/part_1.js
- Recursive IIFE in Day_21/part_2.js
- Single-Liners & IIFE's on Day 22 in both part_1 and part_2
- Pretty straight forward one-liners in both Day_01/part_1.js and Day_01/part_2.js
- Another one-liner in both Day_04/part_1.js and Day_04/part_2.js
- Yet another one-line solution in both Day_06/part_1.js and Day_06/part_2.js
- One-Liner in Day_01/part_1.js
- One-Liner in both Day_02/part_1.js and Day_02/part_2.js
- One-Liner in both Day_04/part_1.js and Day_04/part_2.js
- One-Liner in Day_06/part_1.js
- One-Liner in both Day_11/part_1.js and Day_11/part_2.js
- One-Liner in both Day_12/part_1.js and Day_12/part_2.js
- One-Liner in both Day_13/part_1.js and Day_13/part_2.js - both using Binary Matrices
- One-Liner in Day_14/part_1.js
- One-Liner in both Day_15/part_1.js and Day_15/part_2.js
- One-Liner in Day_24/part_1.js
Footnotes
-
Unless otherwise noted in the Code ↩