Advent of Code Golang Complete Walkthrough

Author

Reads 1.3K

Layout of calendar paper with notes on bright multicoloured notebooks on pink background
Credit: pexels.com, Layout of calendar paper with notes on bright multicoloured notebooks on pink background

The Advent of Code challenge is a 25-day coding event where participants solve puzzles using a programming language of their choice, and Golang is one of the popular choices.

Each day's puzzle comes with a set of input data and a description of the problem to be solved. The puzzles range from simple to complex, with varying levels of difficulty.

The Golang solution for the first day's puzzle involves reading the input data, parsing it, and then finding the solution to the problem. The solution is then printed to the console.

One of the key takeaways from the Advent of Code challenge is the importance of reading and understanding the problem description before starting to code.

Curious to learn more? Check out: Nokia 1800

Getting Started

First, you'll need to install Go on your machine. This is a straightforward process that can be completed in just a few minutes.

You can download the Go installer from the official Go website. Make sure to select the correct version for your operating system.

Once installed, you'll need to set up your Go environment by creating a new directory for your project and running the command `go mod init` to initialize a new module.

For your interest: Golang vs Go

Learning Go

Credit: youtube.com, Learn GO Fast: Full Tutorial

Learning Go is a great way to improve your programming skills, and one way to do it is by using Advent of Code's daily challenges.

Deciding to learn Go was a great decision, as it allowed the author to focus on a new language without having to worry about implementing a working solution at the same time.

Rewriting Python solutions in Go was a good starting point, as it helped the author get familiar with the language without feeling overwhelmed.

Go's blend of control and usability makes it a favorite language of the author, who finds it easier to write in than other languages.

The author has found Go solutions to be several times faster than Python solutions, which is no surprise given that Go gets compiled while Python is interpreted.

Importing external modules in Go is straightforward, as evidenced by the author's use of a hashset implementation in a couple of their solutions.

Suggestion: Golang Go

Make Skeleton Files

Credit: youtube.com, Using Skeleton Files, Creating Project and Packaging Files to Submit to Mimir Platform

To make skeleton files, you'll need to use the embed feature.

Skeleton files won't compile without an input.txt file in the same folder.

You can create an input file with the command "make input".

Requirements

To get started, you'll need a specific version of Go installed on your computer. Go 1.16+ is required for this project, as it uses the embed feature for input files.

Having the latest version of Go will also unlock some extra features, like the ability to range over integers and use built-in min() and max() functions.

Additional reading: Golang Version Manager

Running Locally

To run the code locally, use the command go run main.go with the -part flag. This will allow you to run the actual inputs for the day you're working on.

You can choose to run either part 1 or part 2 by specifying the corresponding number. For example, go run main.go -part 1 will run part 1 of the code.

To run examples and unit tests, use the command go test -run RegExpToMatchFunctionNames. This will execute the tests found in the main_test.go file.

You'll need to authenticate with the AOC by passing your cookie using either the -cookie flag or the AOC_SESSION_COOKIE env variable.

Understanding the Problem

Credit: youtube.com, LIVE Solving The Advent Of Code Puzzles In Golang

The Advent of Code challenge presents a series of puzzles to solve using Golang, requiring a deep understanding of the language and its ecosystem.

Each puzzle has a unique input format, which can range from simple strings to complex data structures, such as arrays and maps.

The puzzles often involve parsing and processing this input data to produce the desired output.

The problem is further complicated by the limited time available to solve each puzzle, which can be as short as 24 or 48 hours.

The puzzles also require a good understanding of algorithms and data structures, as well as the ability to write efficient and readable code.

The challenge is designed to be completed in a specific time frame, which can be a source of stress and pressure for participants.

The puzzles are designed to be fun and engaging, but also challenging and thought-provoking.

The Advent of Code challenge is a great way to improve your coding skills, learn new concepts, and stay up-to-date with the latest developments in the Golang ecosystem.

The puzzles cover a wide range of topics, including data structures, algorithms, and software design patterns.

The challenge is open to participants of all skill levels, from beginners to experienced developers.

Solving the Puzzle

Credit: youtube.com, LIVE Solving The Advent Of Code Puzzles In Golang

To solve the Advent of Code puzzles in Golang, we first need to understand the problem. The puzzles are designed to be solved using a specific programming language, in this case, Golang.

The puzzles are presented as a series of challenges that require us to write code to solve a specific problem, such as finding the sum of all numbers in a list or decoding a message. The puzzles are designed to be fun and engaging, while also providing a challenge for programmers.

By breaking down the problem into smaller, manageable parts, we can use Golang's built-in features, such as slices and maps, to solve the puzzle efficiently.

Solving Part 1

Passing a closure with a readFunc that returns the phase value and then the output stored in an 'output buffer' is key to solving this puzzle.

This approach allows you to range over all the amps and modify the ampInput each time the closure gets executed.

Close-up view of colorful code on a laptop screen, showcasing programming concepts.
Credit: pexels.com, Close-up view of colorful code on a laptop screen, showcasing programming concepts.

The closure first returns the phase value, which is the first item in ampInput, and then shrinks the ampInput to only contain the output value of the previous function.

Populating the ampInput in the outer function and passing it to the closure is crucial for this process to work.

This technique helps you alter the ampInput each time the closure gets executed, which is essential for solving the puzzle.

Solving Part 2

Solving Part 2 was a breeze with Go, thanks to its ability to create a chain of channels. This allowed each channel to run on its own goroutine, using the first channel as a readFunc and the last channel as a writeFunc.

The key to success was adapting the IntCode computer to use the writer channel to push the result, making it easy to listen to all amps and make sure they'd finished before calculating the maximum output signal achieved.

Laptop displaying code in a dark setting, highlighting programming concepts and digital work.
Credit: pexels.com, Laptop displaying code in a dark setting, highlighting programming concepts and digital work.

Chaining the amps together in a feedback loop was the way to go, with the last amp (E) and first amp (A) being chained together until all amps reached the exit function (opcode 99).

This approach made it easy to communicate the final results on the 'total' channel, allowing for a natural and headache-free solution to the 7th challenge.

Tiffany Kozey

Junior Writer

Tiffany Kozey is a versatile writer with a passion for exploring the intersection of technology and everyday life. With a keen eye for detail and a knack for simplifying complex concepts, she has established herself as a go-to expert on topics like Microsoft Cloud Syncing. Her articles have been widely read and appreciated for their clarity, insight, and practical advice.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.