You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've generated a separate directory in the repository for each advent calendar day: from the 1st until the 25th. Additionally, the day_0
directory contains a solution for the first challenge from last year's competition.
To solve a daily challenge, navigate to the corresponding day and implement the solution. I've prepared two methods for each day, Part1
andPart2, indicating that every challenge has two parts. The return value of the methods are always strings. The methods receive the input
as a string slice, so you don't have to worry about reading anything from a file.
The solution for the challenge of December 1, 2022, looks like this:
// getElfCalories reads the input lines and returns a slice of calories for each elffuncgetElfCalories(input []string) []int {
varcalories []intcurrentElf:=0for_, i:=rangeinput {
ifi=="" {
calories=append(calories, currentElf)
currentElf=0
} else {
cal, _:=strconv.Atoi(i)
currentElf+=cal
}
}
calories=append(calories, currentElf)
sort.Slice(calories, func (i, jint) bool {
returncalories[i] >calories[j]
})
returncalories
}
// Part1 solves the first part of the exercisefuncPart1(input []string) string {
calories:=getElfCalories(input)
maxCalories:=calories[0]
returnstrconv.Itoa(maxCalories)
}
// Part2 solves the second part of the exercisefuncPart2(input []string) string {
calories:=getElfCalories(input)
topCalories:=0for_, i:=rangecalories[:3] {
topCalories+=i
}
returnstrconv.Itoa(topCalories)
}
Additionally, I've prepared unit tests for each day to try the solutions with a template input. The text files in the directories contain
the template input and output for both parts of the exercise. Paste the input to input_test_x.txt file and the expected solution
tosolution_x.txt.
Test your solution
Before submitting a solution, you can test your algorithm with the template input. Navigate to the day you want to check and run the
following command:
go test
Alternatively, you can also run the command from the project root.
go test ./days/day_xx/
Compile and run
You can compile code and run with the actual input if all the tests pass. For this, first, run this command:
go build .
To run the solution, you need to provide a few extra arguments.
the --day flag must be set to specify which day's solution should run
the --input flag specifies the path of the file containing the actual input
optionally, you can add the --mode flag to run one part of the daily challenge, accepted values are 1 and 2
And now the complete command:
./advent_of_code_go_template --day x --input path_to_input --mode 1
# or
go run . --day x --input path_to_input --mode 1
Contribution
If you'd like to contribute to the project, open an issue or a pull request!