Dec 1st 2015
Following the the tradition of "advent calendars", the Advent of code puzzles are published daily from the 1st of December up to the 25th.
They require you to write a small computer program to get to the solution. Advent of Code puzzles are usually given in this form: there is a blurb explaining the problem, a series of examples, and the question, which is the puzzle you need to solve, with your input.
Usually the puzzles increase of complexity day after day, and sometimes require to build upon programming concepts that you touched the days earlier.
I am going to try and tackle the Advent of Code puzzles for the past and current year, using for each year a different technology, and I am going to jot down my own personal notes about them. For tackling the 2015 puzzles I decided to use Go, and you will be able to find my code in my github project.
For the 1st December 2015 the puzzle states:
Note that this block is actually really useful for writing some small test cases. Test cases can be used to see if the function you are going to write is going to behave correctly with a smaller amount of data. If the code behaves correctly most of the time you can rule out errors while you are working on bigger amount of data. With these information we can now begin to think about the puzzle: it can be done by a function which takes a string, and returns a number, so I will first write the empty function:
Then I will write the tests
Note that in go it's possible to define a structure anonymously[1] and use it at the same time as we define the data [2]. In this case I encoded the examples that were defined in the example section. Now I can think about the code itself. The puzzle states:
And now I can run the tests the code...
The ok on the "github.com/limacat76/aoc2015/code" package means that the tests passed and I am ready to write complete the main function to run the code, which will read the complete input.
The various Advent of Code puzzles always have a second part which gets the same input but make you do a different calculation.
They require you to write a small computer program to get to the solution. Advent of Code puzzles are usually given in this form: there is a blurb explaining the problem, a series of examples, and the question, which is the puzzle you need to solve, with your input.
Usually the puzzles increase of complexity day after day, and sometimes require to build upon programming concepts that you touched the days earlier.
I am going to try and tackle the Advent of Code puzzles for the past and current year, using for each year a different technology, and I am going to jot down my own personal notes about them. For tackling the 2015 puzzles I decided to use Go, and you will be able to find my code in my github project.
For the 1st December 2015 the puzzle states:
Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time. An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor. The apartment building is very tall, and the basement is very deep; he will never find the top or bottom floors.This paragraph is the specification of the problem and we are given, including the limit of the problem. It tells us a lot: that we are going to manipulate a number, which is the result, according to the characters of the input. Then there is this block with some examples.
(()) and ()() both result in floor 0.
((( and (()(()( both result in floor 3.
))((((( also results in floor 3.
()) and ))( both result in floor -1 (the first basement level).
))) and )())()) both result in floor -3.
Note that this block is actually really useful for writing some small test cases. Test cases can be used to see if the function you are going to write is going to behave correctly with a smaller amount of data. If the code behaves correctly most of the time you can rule out errors while you are working on bigger amount of data. With these information we can now begin to think about the puzzle: it can be done by a function which takes a string, and returns a number, so I will first write the empty function:
// elevator.go
package code
// CalculateDestination calculates the destinations of elevator instructions
func CalculateDestination(instructions *string) int {
return 0
}
Then I will write the tests
// elevator_test.go
package code
import "testing"
func TestElevatorDestination(t *testing.T) {
// [1]
cases := []struct {
instructions string
destination int
}
// [2]
{
{"(())", 0},
{"()()", 0},
{"(((", 3},
{"(()(()(", 3},
{"))(((((", 3},
{"())", -1},
{"))(", -1},
{")))", -3},
{")())())", -3},
}
for _, c := range cases {
// [3]
got := CalculateDestination(&c.instructions)
if got != c.destination {
// [4]
t.Errorf("Dconf CalculateDestination (X) == %v, want %v", got, c.destination)
}
}
}
Note that in go it's possible to define a structure anonymously[1] and use it at the same time as we define the data [2]. In this case I encoded the examples that were defined in the example section. Now I can think about the code itself. The puzzle states:
Santa is trying to deliver presents in a large apartment building, but he can't find the right floor - the directions he got are a little confusing. He starts on the ground floor (floor 0) and then follows the instructions one character at a time. An opening parenthesis, (, means he should go up one floor, and a closing parenthesis, ), means he should go down one floor.This text maps directly to code in a direct fashion.
func CalculateDestination(instructions *string) int {
// He starts on the ground floor (floor 0)
floor := 0
// and then follows the instructions one character at a time.
for _, character := range *instructions {
// An opening parenthesis, (, means he should go up one floor
if '(' == character {
floor++
}
// , and a closing parenthesis, ), means he should go down one floor.
else if ')' == character {
floor--
}
}
// To what floor do the instructions take Santa?
return floor
}
And now I can run the tests the code...
cat@aoc2015:~/goAoc2015/src/github.com/limacat76/aoc2015$ go test ./... -cover
? github.com/limacat76/aoc2015/cmd/day1501A [no test files]
? github.com/limacat76/aoc2015/cmd/day1501B [no test files]
ok github.com/limacat76/aoc2015/code 0.001s coverage: 100.0% of statements
The ok on the "github.com/limacat76/aoc2015/code" package means that the tests passed and I am ready to write complete the main function to run the code, which will read the complete input.
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/limacat76/aoc2015/code"
)
func main() {
filename := os.Getenv("HOME") + "/2015/01/data.txt"
data, _ := ioutil.ReadFile(filename)
s := string(data)
fmt.Println(code.CalculateDestination(&s))
}
The various Advent of Code puzzles always have a second part which gets the same input but make you do a different calculation.
Comments
Post a Comment