adventofcode2019

package module
v0.0.0-...-535b83c Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2020 License: MIT Imports: 9 Imported by: 0

README

image:https://godoc.org/gitlab.com/jhinrichsen/adventofcode2019?status.svg["godoc",
link="https://godoc.org/gitlab.com/jhinrichsen/adventofcode2019"]
image:https://goreportcard.com/badge/gitlab.com/jhinrichsen/adventofcode2019["Go report
card", link="https://goreportcard.com/report/gitlab.com/jhinrichsen/adventofcode2019"]
image:https://gitlab.com/jhinrichsen/adventofcode2019/badges/master/coverage.svg?style=flat["coverage",
link="https://gitlab.com/jhinrichsen/adventofcode2019/-/jobs"]

= Advent of Code 2019

== Build from source

----
$ go get gitlab.com/jhinrichsen/adventofcode2019
----

This is an implementation of https://adventofcode.com/2019[Advent of Code 2019]
in Go.

Documentation

Index

Constants

View Source
const (
	X     = 0
	Y     = 1
	Z     = 2
	DIMS  = 3
	MOONS = 4
)

DIMS has three dimensions (x, y, z)

View Source
const (
	// OpcodeAdd adds parameter
	OpcodeAdd = 1
	// OpcodeMul multiplies parameter
	OpcodeMul = 2
	// OpcodeRet halts the program
	OpcodeRet = 99
)
View Source
const (
	// Lower range from puzzle input
	Lower = 136818
	// Upper range from puzzle input
	Upper = 685979
)
View Source
const (
	// Input takes a single integer as input and saves it to the position
	// given by its only parameter.
	Input = 3

	// Output outputs the value of its only parameter.
	Output = 4

	// JumpIfTrue sets the instruction pointer to the value from the second
	// parameter if the first parameter is non-zero.
	JumpIfTrue = 5

	// JumpIfFalse sets the instruction pointer to the value from the second
	// parameter if the first parameter is zero.
	JumpIfFalse = 6

	// LessThan stores 1 in the position given by the third parameter if
	// the first parameter is less than the second parameter, otherwise
	// it stores 0.
	LessThan = 7

	// Equals stores 1 in the position given by the third parameter if
	// the first parameter is equal to the second parameter, otherwise
	// it stores 0.
	Equals = 8

	// AdjustRelBase changes the relative base
	AdjustRelBase = 9
)
View Source
const COM = "COM"

COM is the Center of Mass

Variables

This section is empty.

Functions

func Abs

func Abs(n int) int

Abs returns absolute value for integer.

func Board

func Board(x, y int) [][]uint

Board creates a two dimensional arrray. The board created will have double width and double height, so that no negative indices are used when wiring.

func Boolean

func Boolean(b bool) int

Boolean is a C style Boolean: false -> 0, true -> 1.

func CompleteFuel

func CompleteFuel(mass int) int

CompleteFuel computes required fuel for given mass, including fuel for fuel. Both mass and fuel have no units.

func CritExactlyTwoAdjacent

func CritExactlyTwoAdjacent(n int, digits []byte) bool

CritExactlyTwoAdjacent two adjacent matching digits are not part of a larger group of matching digits.

func CritIncreasing

func CritIncreasing(n int, digits []byte) bool

CritIncreasing Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679)

func CritSixDigits

func CritSixDigits(n int, digits []byte) bool

CritSixDigits It is a six-digit number

func CritTwoOrMoreAdjacent

func CritTwoOrMoreAdjacent(n int, digits []byte) bool

CritTwoOrMoreAdjacent Two adjacent digits are the same (like 22 in 122345)

func CritWithinRange

func CritWithinRange(n int, digits []byte) bool

CritWithinRange The value is within the range given in your puzzle input

func Day10Part2

func Day10Part2(as []Asteroid, base Asteroid) int

Day10Part2 determines the 200th asteroid that gets vaporized.

func Day11Part1

func Day11Part1(prog IntCode) int

Day11Part1 returns the number of painted tiles.

func Day11Part2

func Day11Part2(prog IntCode) []byte

Day11Part2 returns a PBM encoded registration ID. The image is horizontally flipped so em well you can read it from the inside of the transparent hull?

func Day13Part1

func Day13Part1(cpu IntCodeProcessor, code IntCode) int

Day13Part1 returns number of blocks for arcade game.

func Day13Part2

func Day13Part2(cpu IntCodeProcessor, code IntCode) int

Day13Part2 returns number of blocks for arcade game.

func Day1Part1

func Day1Part1(lines []string) (int, error)

Day1Part1 returns sum of fuel for all modules

func Day1Part2

func Day1Part2(lines []string) (int, error)

Day1Part2 returns sum of fuel and transitively the required fuel for the fuel for all modules

func Day3Part1

func Day3Part1(wires []string) (int, error)

Day3Part1 computes the minimal manhattan distance of two crossing wires

func Day3Part2

func Day3Part2(wires []string) (int, error)

Day3Part2 computes the minimal combined steps for intersections

func Day4Part1

func Day4Part1() int

Day4Part1 returns number of passwords between Lower and Upper that meet all criteria

func Day4Part2

func Day4Part2() int

Day4Part2 returns number of passwords between Lower and Upper that meet all criteria

func Day5

func Day5(program IntCode, input <-chan int, output chan<- int)

Day5 supports running IntCode for day 2 (ADD, MUL, RET) and adds input, output, parameter mode and immediate mode.

func Day7Part1

func Day7Part1(prog IntCode, phases string) int

Day7Part1 returns max thrust for given program, using a permutation of all phases.

func Day7Part2

func Day7Part2(prog IntCode, phases string) int

Day7Part2 returns the maximum thrust for a feedback loop.

func Day8Part1

func Day8Part1(digits []byte) (int, error)

Day8Part1 returns number of 1s multiplied by number of 2s in the layer having minimal number of 0s.

func Day8Part2

func Day8Part2(digits []byte) ([]byte, error)

Day8Part2 returns rendered layer.

func Digits

func Digits(n int) []byte

Digits splits n into its digits

func DigitsInto

func DigitsInto(n int, buf []byte)

DigitsInto will write digits of n into buf

func False

func False(boolean int) bool

False returns false for 0, true otherwise.

func Fuel

func Fuel(mass int) int

Fuel computes required fuel for given mass. Both mass and fuel have no units.

func Len

func Len(opcodes []int) int

Len calculates them maximum index into an array, i.e. the len of an array that can hold opcodes.

func MaxSize

func MaxSize(wirings []string) (int, int, error)

MaxSize calculates the size of wirings

func MeetsCriteria

func MeetsCriteria(n int, digits []byte, crits []Criteria) bool

MeetsCriteria returns true if all criteria are fulfilled for n, using short circuit evaluation

func MinimalDistance

func MinimalDistance(b [][]uint) int

MinimalDistance returns minimal manhattan distance of all crossings

func Ndigits

func Ndigits(n int) int

Ndigits returns number of digits that n consists of. 0 has 1 digit, 42 has 2 digits, -4711 has 4 digits. Naming convention taken from Julia

func Run

func Run(opcodes []int) ([]int, error)

Run executes opcodes

func Runs

func Runs(s string) (string, error)

Runs executes a comma separated list of opcodes

func Size

func Size(wiring string) (int, int, error)

Size calculates width and height of a wiring

func Split

func Split(s string) ([]int, error)

Split converts a comma separated list into an array

func ToString

func ToString(opcodes []int) string

ToString returns a comma separated list of opcodes

func True

func True(boolean int) bool

True returns false for 0, true otherwise.

func Walk

func Walk(b [][]uint, wires []string) error

Walk dots all wires onto a board

Types

type Asteroid

type Asteroid = complex128

An Asteroid is identified by a dimensionless (x/y) position in a 2D space. Using a type _alias_ here to avoid casting

func Day10Part1

func Day10Part1(as []Asteroid) (Asteroid, int)

Day10Part1 returns the asteroid that can see most asteroids, and the number of visible asteroids.

func ParseAsteroidMap

func ParseAsteroidMap(asteroids []byte) []Asteroid

ParseAsteroidMap parses newline separated strings into asteroids.

type Criteria

type Criteria func(int, []byte) bool

Criteria is a function that tests an integer. For performance reasons, both numerical and digits are supplied.

func CriteriaPart1

func CriteriaPart1() []Criteria

CriteriaPart1 returns all required criteria for part 1

func CriteriaPart2

func CriteriaPart2() []Criteria

CriteriaPart2 returns all required criteria for part 1

type Day6

type Day6 struct {
	// contains filtered or unexported fields
}

Day6 holds an object and its orbit.

func NewDay6

func NewDay6(ss []string) (Day6, error)

NewDay6 creates a new Orbit from a list of 'a)b' lines.

func (Day6) CommonOrbit

func (a Day6) CommonOrbit(object1, object2 string) string

CommonOrbit returns the nearest orbit of two objects, at least COM.

func (Day6) Orbit

func (a Day6) Orbit(object string) string

Orbit returns the orbit of an object.

func (Day6) OrbitCount

func (a Day6) OrbitCount(object string) int

OrbitCount returns the number of orbits of a given object.

func (Day6) OrbitCountChecksum

func (a Day6) OrbitCountChecksum() int

OrbitCountChecksum returns the checksum for a complete orbit.

func (Day6) Transfers

func (a Day6) Transfers(object1, object2 string) int

Transfers counts the number of hops between an object up to the nearest common orbit, and then down to the the second object.

type Direction

type Direction int

Direction type

const (
	// Up represents 'U'
	Up Direction = iota
	// Right represents 'R'
	Right
	// Down represents 'D'
	Down
	// Left represents 'L'
	Left
)

func Parse

func Parse(path string) (Direction, int, error)

Parse splits a path such as U32 into a direction North and a length 32

type IntCode

type IntCode []int

IntCode is both a low-level interpreted language used in bootstrapping the BCPL compiler, and a virtual language in advent of code.

func MustSplit

func MustSplit(program string) (ic IntCode)

MustSplit converts IntCode in string representation (a comma separated list of token) into IntCode, and panics if conversion fails.

func (IntCode) Copy

func (master IntCode) Copy() IntCode

Copy provides a master copy because most programs are self modifying

type IntCodeProcessor

type IntCodeProcessor func(IntCode, <-chan int, chan<- int)

IntCodeProcessor can run IntCode

type ParameterMode

type ParameterMode int

ParameterMode is part of the opcode and controls parameter handling

const (
	// PositionMode reads memory via index
	PositionMode ParameterMode = iota

	// ImmediateMode uses values directly
	ImmediateMode

	// RelativeMode addresses via relative base
	RelativeMode
)

type Point

type Point struct {
	// contains filtered or unexported fields
}

Point holds a (x/y) position

type RegistrationID

type RegistrationID map[complex128]bool

RegistrationID is a 2D map of black (false) and white (true)

Jump to

Keyboard shortcuts

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