= Advent Of Code (AOC) 2020
My take on the AOC, documenting my errors.
Usually, i don't go for implementation speed, because that does not resonate well with me.
My second highest priority is runtime performance, and priority number one is getting it right the first time.
Therefore, i always provide complete coverage via unit tests.
Go just makes this _so_ easy.
This document does not include solutions so no worries to read it at any point in time.
== Environment
- Go 1.15.5
- vim, vim-go, gopls, fed by a HHKB
- VisualStudio Code for debugging, fed by a MX Master 3
- Fedora 33
- AMD Ryzen 5 3400G on a Gigabyte B450
The package name is `aoc2020`.
Each day lives separately in a `day{{.n}}.go` and `day{{.n}}_test.go` file.
Unit test data, both examples and puzzle input, is in
`testdata/day{{.n}}.txt`, and `testdata/day{{.n}}_example.txt`.
== Day 1
Warmup, nice and smooth.
Blattschuss, Blattschuss (answer correct on first try).
== Day 2
Warmup, nice and smooth.
Blattschuss, Blattschuss.
== Day 3
The example's unit test succeeded, but part 1 failed, 284 being too low.
I refrained from implementing a too-complex implementation based on complex
numbers (x/y), and operated directly on the indexable lines[][] area.
Fail, Blattschuss.
== Day 4
First try 234 again too low.
Needed an extra blank line at the end of the file to make sure last pass was
validated.
For part 2, unit testing found an error before submitting the correct result.
Fail, Blattschuss.
`Hair color` is defined as `a # followed by exactly six characters 0-9 or a-f`.
So check for a leading '#', and then iterate the remaining slice:
====
----
if s[0] != '#' {
return false
}
for i := range s[1:] {
if '0' <= s[i] && s[i] <= '9' { // WRONG
...
}
}
----
====
`s[1:]` is the correct slice, `i` is `0` for the first iteration, indexing
`s[i]` does _not_ index the slice but the original string, so it will point
to `#`.
====
----
num := s[1:]
for i := range num {
if '0' <= num[i] && num[i] <= '9' { // CORRECT
...
}
}
----
====
Benchmark for part 2:
====
----
go test -benchmem -run=^$ -bench ^(BenchmarkDay4Part2|BenchmarkDay4Part2IncludingInput)$
goos: linux
goarch: amd64
BenchmarkDay4Part2-8 1064 1220554 ns/op 199106 B/op 3504 allocs/op
BenchmarkDay4Part2IncludingInput-8 757 1547982 ns/op 281552 B/op 4327 allocs/op
PASS
----
====
A seven digit ns/op is a two digit ms/op, so 12 resp. 15 ms/op.
== Day 5
Blattschuss, Blattschuss.
== Day 6
Blattschuss, Blattschuss.
== Day 7
Blattschuss
== Day 8
Blattschuss.
Part 2 ran like a champ, but the answer was not accepted, too low. After
re-reading the instructions, the program must terminate at the last line, not
somewhere.
== Day 9
Blattschuss, Blattschuss.
== Day 10
Blattschuss, Blattschuss.
I feel proud because i figured out the O(n) solution all by myself.