Advent of Code 2021 - Time to learn Go

aoc2021

Advent of Code 2021 - Time to learn Go Will contain my solutions for aoc2021, so avoid reading the files in .src/aoc2021/ unless you want spoilers. Also has 2020-day1-part1 in ./src/aoc2020 for demo/example purposes, as it's less relevant than the current year (personally used it to set up and test the environment, without having to break open this years advent-calendar).

Setup

Copy your session-cookie into .cookie_session or it wont work. You can retrieve this string by:

  • Logging into aoc and opening dev-console in your browser
  • Selecting the "Application" tab
  • Expanding "Cookies" in the left sidepanel ("Cookies" is found under header "Storage")
  • Selecting the aoc url
  • Then finally copy the Value from the "session"-row (will be a long lowercase hex-string)

aoc.py and day-template

This project comes with a handy (but simple) utility I have made called aoc-py. This can be used in the command-line to (more info in python oac-py -h):

  1. Create a new file for the day by copying/'instantiating' the day.template file, renaming, and replacing placeholder values. (python aoc.py [--year 2021] new [1-25])
    • 'year' - by default the current year. Mainly changes the folder used, and the %{year} placeholder when instantiating the template file.
    • [1-25] ('day') - by default finding the highest existing day/file (in ./src/aocYYYY/), incremented by 1. If no file exists its considered 0, giving 0+1. It overwrites the entire file if an existing day/file is specified. So beware losing data!
    • Normally used as python aoc.py new
    • The created file will be renamed using the format ./src/aoc{year}/day{day:02d}.go
  2. Test (run) a day (file) (python aoc.py [--year 2021] test [1-25])
    • 'year' - by default the current year. Changes the folder used.
    • 'day' - by default uses the highest existing day/file (in ./src/aocYYYY/).
    • Normally used as python aoc.py test
    • It does not autmatically submit your answer to aoc, that has to be done manually. But generally the template file does include assertions for hte test-inputs, and prints the answer to the real input.

The day.template file can be edited to change the initial state of new days (files). Generally should contain an easy way to add assertions for the tests aoc gives in each part (test-input with example-output). There are various useful placeholder variables that can be used:

  • %{starttime} - This is replaced with the timestamp (HH-MM-SS) of when the file was created. I usually put this in a comment to help me keep track of when I started a day for tracking-purposes (aoc's built-in timing is bugged, and assumes everyone starts at same time).
  • %{p1Done} & %{p2Done} - UNIMPLEMENTED. Meant to be able to be replaced retroactively when a part is completed. Possibly with some delta's too, to effortlessly know how long you took. For now, you would have to edit those values manually.
  • %{year} & %{day} - Self-explanatory. Replaced with the selected year (YYYY) and day (DD/day:02d) respectively.

Installation

So to summarize, 'installing' the project could be done as such:

?:/???> cd desired-project-directory

?:/desired-project-directory> git clone githubs-project-url .

# Get your session-cookie-token and write it into './cookie_session'

?:/desired-project-directory> python aoc.py new

# Edit the created file to complete part1

?:/desired-project-directory> python aoc.py --year 2020 test
argv: aoc.py
argv: --year
argv: 2020
argv: test
args  Namespace(day='', dir='./src/', func=<function test at 0x015F8F18>, year='2020')
Testing aoc: year=2020 - day=latest ...
go run ./src/aoc2020/day01.go
part1 minitest success: true!
part1: 63616

part2 minitest success: true!
part2:

# Edit the created file to complete part2

?:/desired-project-directory> python aoc.py -y 2020 test
argv: aoc.py
argv: --year
argv: 2020
argv: test
args  Namespace(day='', dir='./src/', func=<function test at 0x015F8F18>, year='2020')
Testing aoc: year=2020 - day=latest ...
go run ./src/aoc2020/day01.go
part1 minitest success: true!
part1: 63616

part2 minitest success: true!
part2: 67877784

?:/desired-project-directory> 

Note that I specified year 2020 in the block above. This is because I include a demo for the first day of the prior year in the project. Which allows you to test the environment without opening the actual/current year. Also serves to show how the project can contain/be used for multiple years at once.

Similar Resources

Solutions to Advent-of-Code 2021, in Go.

🎄 advent-of-code-2021 🎄 Solutions to 2021 Advent of code. Summary Advent of Code is an annual advent-calendar of programming puzzles. Here are my 20

Dec 14, 2022

Advent of code 2021 solutions by: me :)

Advent of code 2021 These are my solutions for the advent of code 2021 event. My idea is to solve all the problems in go, but I may use another langua

Dec 27, 2021

A little repository for my Advent of Code 2021 solutions in Go

Advent of Code 2021 What's this repo all about? Well, this is a collection of code written in Golang to solve the daily problems presented in Advent o

Dec 12, 2021

Advent of Code 2021 solutions using Go 1.18 Generics

advent-of-code-2021 Here are my solutions for Advent of Code 2021. This year, I chose to write my solutions using Go 1.18 with generics (by building t

Dec 18, 2022

My solutions for Advent of Code 2021.

My solutions for Advent of Code 2021 in go. The solution and input data (i.e. my custom input data) for day XX is contained in the subdirectory dayXX.

Dec 22, 2021

🎅 A programming language for Advent of Code.

🎅 Adventlang My blog post: Designing a Programming Language for Advent of Code A strongly typed but highly dynamic programming language interpreter w

Dec 28, 2022

Advent of code solutions in Golang

Advent of Code go solutions This repo contains my solutions in Golang for advent of code (P.S: I am using this opportunity to learn new language most

Dec 21, 2021

A non-go engineer tries to write Go to solve Advent of Code

Wherein an engineer (who primarily uses Kotlin, Java, Scala and C#) tries to teach themselves Go by solving Advent of Code challenges. It's... not pre

Dec 9, 2021

Advent of code — Programming Christmas Puzzles

Advent-of-code-2021 Advent of code — Programming Christmas Puzzles ★ - both the first and the second parts are solved ☆ - only the first part is solve

Dec 11, 2021
Advent of Code 2021, this time in Go

Go Lang Notes Advent of Code Day 3 The distinction between chars and bytes is a bit annoying. I got tripped up by doing: int(str[pos]) which gives yo

Dec 10, 2022
🎄 My code for the Advent of Code of year 2021 in Go.

Advent of Code 2021 This repository contains all code that I wrote for the Advent of Code 2021. This year I chose to try and learn Go. Enjoy! Built wi

Dec 9, 2021
Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Advent of Code 2021 Advent of Code is an Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved

Dec 2, 2021
Jan 4, 2022
Personal Advent of Code challenges to force me to learn Go.

Advent of Code 2021 A list of 31 challenges I am completing using Golang for the month of December. Day 1: Quicksort Variables Constants Lists Comment

Dec 16, 2021
Advent of Code 2021 Solutions

Advent of Code 2021 Solutions for the 2021 Advent of Code Building This project makes use of Go 1.17. go mod download go test ./... Running the Soluti

Dec 1, 2022
Finished snippets I did for Advent of Code 2021.

Advent of Code 2021 This repository includes my Go code for the Advent of Code 2021. If you want to solve the puzzles yourself first, do so, then look

Dec 25, 2021
Advent of Code 2021, experiment in learning Go

aoc_2021 Advent of Code 2021, experiment in learning Go To run, open a terminal in the root folder of the project and run with go run . <day to run> <

Mar 1, 2022
My solutions for 2021's Advent of Code

Advent of Code 2021 These are my solutions to this year's Advent of Code. I used it as an excuse to practice with a language which I'm not yet very fa

Dec 27, 2021
Advent of Code 2021 (Go)

Advent of Code 2021 (Go) This project includes an implementation of the Advent of Code 2021 problems, implemented in Go. These are ports from my C# so

Dec 18, 2021