aoc2020

package module
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2020 License: GPL-3.0 Imports: 7 Imported by: 0

README

= 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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Day1

func Day1(ns map[uint]bool, part1 bool) uint

Day1 returns product of two numbers that add up to 2020.

func Day2

func Day2(lines []string, part1 bool) (uint, error)

Day2 return number of valid passwords.

func Day3

func Day3(lines []string, part1 bool) uint

Day3 returns number of trees encountered for part1, otherwise product of slopes (1,1), (3,1), (5,1), (7,1) and (1,2).

func Day4

func Day4(lines []string, part1 bool) uint

Day4 returns number of valid passports.

func Day5

func Day5(seats []string, part1 bool) uint

Day5 returns max seat ID for part1 or missing seat for binary space partitioned seats.

func Day5Part1

func Day5Part1(seats []string) uint

Day5Part1 returns max seat ID for binary space partitioned seats.

func Day6

func Day6(lines []string, part1 bool) uint

Day6 returns number of distict answers by group.

func Day8

func Day8(lines []string, part1 bool) (int, bool)

Day8 returns accumulator, and , and true if the program terminated normally, or false if any one line is executed second time (which indicates a loop).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL