Skip to content

Bulk import of in progress advent calendar

Jake edited this page Dec 28, 2023 · 5 revisions

If you've already started an advent calendar somewhere else and decide you want to start using advent-of-code runner you're faced with an issue. Your advent-of-code-runner project won't know about the puzzles you've already completed. If you try to submit those puzzles via advent-of-code-runner the advent of code website will return an error because you've already answered them. This leaves your repository in a weird state which you can't fix.

Luckily version 1.7.0 added the import command which lets you inform advent-of-code-runner about the correct answer to puzzles you solved outside of advent-of-code-runner.

It can be tedious to run the import command on multiple problems, using basic linux command line tools you can do a bulk import of many puzzles at once.

Step 1 - Create the Bulk Import File

Create a text file at the root of your repository, each line of the text file should have three items a day, a level and the correct answer. The lines correspond to the <day> <level> <answer> arguments of the import command. You can add as many lines as you have puzzles to import. This file can be deleted after you finish importing.

Here is an example where I add the answers to four puzzles:

  • Day 1 Level 1 answer: asdf
  • Day 1 Level 2 answer: 1234
  • Day 7 level 1 answer: -9983
  • Day 20 level 1 answer: hello world
touch bulkImport.txt
echo '1 1 asdf' >> bulkImport.txt
echo '1 2 1234' >> bulkImport.txt
echo '7 1 -9983' >> bulkImport.txt
echo "20 1 'hello world'" >> bulkImport.txt

To verify everything was added correctly you can view the newly created file

cat bulkImport.txt

Your file should look like this:

1 1 asdf
1 2 1234
7 1 -9983
20 1 'hello world'

Step 2 - Import

The following command will run the import command for each line of the file you created in step 1.

cat bulkImport.txt | xargs -n3 npm run -- import --no-confirm -- && npm run stats -- --save

The way this works:

  1. cat outputs each line of the input file to stdout
  2. The output of cat is redirected to xargs using the |
  3. The -n3 argument tells xargs that each line has at most 3 arguments.
  4. xargs executes npm run import for each line in stdout, passing the value to the import command.
  5. Once the importing is finished successfully, the stats command is executed with the --save option to update your README file with the latest completion progress.

Note: In the npm run -- import --no-confirm -- portion of the command you may wonder what the -- characters are for. The first -- separator is used to tell npm that the --no-confirm option is for advent-of-code-runner, not npm. The second -- separator is used to tell the import command to ignore any items which start with a -, this allows you to import negative numbers as answers.

When you run the command you should see output similar to this:

$ cat bulkImport.txt | xargs -n3 npm run -- import --no-confirm -- && npm run stats -- --save

> [email protected] import
> advent-of-code-runner import --no-confirm -- 1 1 asdf

🌟 Successfully imported puzzle answer. Run the 'solve' command to update runtime statistics. ❄️ 

> [email protected] import
> advent-of-code-runner import --no-confirm -- 1 2 1234

🛷 Successfully imported puzzle answer. Run the 'solve' command to update runtime statistics. 🌨️ 

> [email protected] import
> advent-of-code-runner import --no-confirm -- 7 1 -9983

☃️  Successfully imported puzzle answer. Run the 'solve' command to update runtime statistics. 🌨️ 

> [email protected] import
> advent-of-code-runner import --no-confirm -- 20 1 hello world

🛷 Successfully imported puzzle answer. Run the 'solve' command to update runtime statistics. 🥛

> [email protected] stats
> advent-of-code-runner stats --save

🎁 Saved progress to README file 🤶

If you don't want such verbose output you can redirect the output to /dev/null like so:

cat bulkImport.txt | xargs -n3 npm run -- import --no-confirm -- 1> /dev/null && npm run stats -- --save

Step 3 - Verify the Import

You can verify the import by running the stats command.

npm run stats

It should output a table which looks something like this:

image

Step 4 - Update the Runtimes

You will probably want to update the solution files for the imported puzzles with the code you wrote elsewhere. This allows you to run the puzzles using advent-of-code-runner. You might have noticed that imported puzzles have a fastest runtime of 999 seconds. That's just a placeholder, you can set it to a real value by running the solve command for each of your imported puzzles.