16 Commits

Author SHA1 Message Date
ce6e63b5b6 Day 15 Part 2
Well...that turned out easier than I thought. I suspected this solution would work, but wasn't completely confident. It can only work for the type of maze used by this problem (where there are no loops of open areas).
2022-06-13 08:31:43 -05:00
d41aa6cfa5 Day 15 part 1
I wanted to use something like a right-hand wall solver, but the fact that you don't know the maze ahead of time and you can't see what something is without trying to move into it made that difficult. This semi-brute-force approach works well enough. I originally stopped as soon as I found the oxygen system and figured out the shortest path, but once I submitted that answer and saw that part 2 wanted the full map explored, I figured I might as well just do it all at once.

Part 2 might be able to use the right-hand exploration rule since it has the full map, maybe...possibly a pathfinding/A* type solution, but the problem is finding the "goal" location (furthest point from the oxygen system) itself, so I'm not sure if those will work. My current plan is to either try right-hand wall walking or some sort of breadth-first tree system to plot all distances from the oxygen system, then take the furthest one as the answer.

I think I would have been stuck on part 1 longer if my input set didn't happen to find the goal system fairly easily (or maybe my debug drawing helped me work through it with that input set specifically, I'm not sure) since a different input set required some tweaking to the max-visited threshold in order to find things that my first input set found with a lower setting.

Regardless, I'm pretty excited that I came to Trémaux's algorithm, more or less, on my own. I went to Wikipedia to see if I was on the right track and lo and behold, I came to a version of it myself.
2022-06-13 00:01:50 -05:00
b5202b28c5 Allow intcode programs to be halted externally
This is simplest to do during an input or output callback, but could potentially also be done to a program running on a goroutine.
2022-06-12 23:49:13 -05:00
0a249b85fc Use varargs for GetPermutations 2022-06-12 23:48:14 -05:00
f03184d4c4 Day 14 solution
This one's part 1 destroyed me. I had a very difficult time, trying 3 separate approaches, each one of which worked for most cases but eventually fell apart on the 5th sample or my actual puzzle input. I ended up reading a bunch of hints from the subreddit which eventually led me to a blog post describing this solution, which wasn't far off from what I had, but I was overcomplicating things.

Part 2 surprised me in that I expected a simple "ore available divided by ore needed for 1 fuel" would solve it, but of course the excess chemicals produced in any given reaction meant that it wasn't that simple. So this approach uses that estimate as a lower bound, since it always underestimates, and then bisects its way to the solution (starting at the lower bound and adding 1 each time took too long). I'm sure a smarter upper bound choice could lower the runtime of this by a bit, but runtime isn't bad enough right now for me to try any additional optimizations.
2022-06-12 13:37:38 -05:00
c15d206b8b Day 13 solution
This was incredibly cool and I had a really fun time with it. Uncomment everything to see the game play itself! Note that I'm not seeking around in the terminal window to make the drawing smooth, I'm just outputting each new frame as it happens, so there's some jitter, but it still looks great!

I messed around a bit with control codes to move the cursor around instead of the "draw the buffer over and over again" approach, and they work, mostly, but I'm sticking with this for now.
2022-06-12 13:37:38 -05:00
43d9a77d62 Day 12 solution
Okay, I had to seek help on this one. The orbital period + least-common-multiple solution was not coming to me.
2022-06-12 13:37:38 -05:00
b903417c06 Day 11 solution 2022-06-12 13:37:37 -05:00
9dd39aa193 Day 10 solution
This one was an absolute beating for me. I am so bad at these sorts of problems. Ultimately I settled on a probably-not-ideal solution that crawls the graph with offsets of each variant of (+/-x,+/-y), marking nodes visited as we come across them so that we end up with a list of asteroids that we can see. Given that this is day 10, and knowing how bad I am at math, I'm assuming this is very far from the intended solution, but it works reasonably quickly and I managed to come up with it myself, so I'm not going to stress too much about it.

For asteroid destruction, the best method I could come up with for finding the correct order was to implement an entire Vector class and sort by angle, which worked, but again, I can't decide if it was the intended solution or not. I should start reusing past years' codebases so I don't have to keep building a utility library from scratch.
2022-06-12 13:37:37 -05:00
3b3c805997 Move Pair to a more reusable location
I originally used this in my day 10 solution, but ended up removing it. Either way, it's a general utility so it belongs here.
2022-06-12 13:37:37 -05:00
3ca7312e2c Day 9 solution
This day showed me that when the input instruction was introduced and said "write addresses will never be in immediate mode", that didn't mean "so don't bother handling modes for input addresses", it meant "handle the mode, but assert if it's immediate mode". It was super helpful that this program contained a bootstrap sequence to validate each instruction.

Memory expansion came with a few caveats: obviously reads and writes needed to handle expanding the memory space, but a Reset also can no longer get away with simply copying the program into memory again because we need to ensure that any additional memory is cut off (or at least zeroed), so the quickest way to handle that in Go is to simply allocate a new buffer; I'd rather manipulate the existing buffer, but I'm having a hard time finding the best way to do that.

And finally, make sure you reset your relativeBase when resetting the program...that one was ugly to track down.
2022-06-12 13:37:37 -05:00
bb00808f99 Day 8 solution
I had fun with this one. I liked how straightforward it was, and it's always satisfying to see the code print a message visually when you're done.
2022-06-12 13:37:37 -05:00
11b73ba5bc Day 7 solution
I will probably end up regretting this since I assume the "wait to be given an input from some other process before continuing execution" paradigm is going to come up again, but this part 2 goroutine+channel solution felt good (taking advantage of Go features) and made me happy, so I rolled with it.
2022-06-12 13:37:37 -05:00
d7db069031 Day 6 solution
I'm reasonably happy with this. I started with a bi-directional linked list, but realized that a flat list of all nodes came in handy for one use case while the linked list came in handy for another, so I settled on that.
2022-06-12 13:37:36 -05:00
870abdf579 Day 5 solution
This required an overhaul of the intcode machine to actually be its own type that could operate on its own memory and stuff. So I had to touch day 2 to make it adhere to the new API.

Feeling good about this foundation now. Until I get gobsmacked at some point later, which I expect to happen.
2022-06-12 13:37:36 -05:00
bdd007bb4d Reduce changes needed to the template
I always end up making these changes each time I copy from the template.
2022-06-12 13:37:24 -05:00
11 changed files with 529 additions and 100 deletions

View File

@ -28,7 +28,7 @@ func (d *Day07) Part1() string {
var highestVal int64
var highestSequence []int64
allSequences := utilities.GetPermutations([]int64{0, 1, 2, 3, 4})
allSequences := utilities.GetPermutations([]int64{0, 1, 2, 3, 4}...)
for _, sequence := range allSequences {
if len(sequence) != len(d.amps) {
panic("input sequence does not match up to number of amplifiers")
@ -68,7 +68,7 @@ func (d *Day07) Part2() string {
var highestVal int64
var highestSequence []int64
allSequences := utilities.GetPermutations([]int64{5, 6, 7, 8, 9})
allSequences := utilities.GetPermutations([]int64{5, 6, 7, 8, 9}...)
for _, sequence := range allSequences {
if len(sequence) != len(d.amps) {
panic("input sequence does not match up to number of amplifiers")

View File

@ -10,18 +10,15 @@ import (
)
type reaction struct {
inputs map[string]int
inputs map[string]int64
output u.Pair[string, int]
}
type Day14 struct {
reactions []reaction
leftovers map[string]int
}
func (d *Day14) Parse() {
d.leftovers = make(map[string]int)
lines := u.GetStringLines("14p")
d.reactions = make([]reaction, len(lines))
for i, line := range lines {
@ -32,10 +29,10 @@ func (d *Day14) Parse() {
outPair := strings.Split(output, " ")
outAmt, _ := strconv.Atoi(outPair[0])
d.reactions[i].output = u.Pair[string, int]{First: outPair[1], Second: outAmt}
d.reactions[i].inputs = make(map[string]int)
d.reactions[i].inputs = make(map[string]int64)
for _, input := range inputs {
pair := strings.Split(input, " ")
d.reactions[i].inputs[pair[1]], _ = strconv.Atoi(pair[0])
d.reactions[i].inputs[pair[1]], _ = strconv.ParseInt(pair[0], 10, 64)
}
}
}
@ -54,7 +51,7 @@ func (d Day14) Num() int {
return 14
}
func (d *Day14) oreRequiredStock(qty int64) int64 {
func (d *Day14) getOreRequiredForFuel(qty int64) int64 {
oreRequired := int64(0)
needs := map[string]int64{
"FUEL": qty,
@ -62,33 +59,36 @@ func (d *Day14) oreRequiredStock(qty int64) int64 {
excess := make(map[string]int64)
getFromExcess := func(qty int64, chemical string) int64 {
inStock := excess[chemical]
qty -= inStock
excess[chemical] = int64(math.Min(math.Abs(float64(qty)), 0))
return inStock - excess[chemical]
available := u.Min(excess[chemical], qty)
excess[chemical] -= available
return available
}
for len(needs) > 0 {
keys := u.MapKeys(needs)
chemical := keys[len(keys)-1]
qtyRequired := needs[chemical]
delete(needs, chemical)
producing := keys[0]
qtyRequired := needs[producing]
delete(needs, producing)
fromExcess := getFromExcess(qtyRequired, chemical)
fromExcess := getFromExcess(qtyRequired, producing)
if fromExcess == qtyRequired {
continue
}
qtyRequired -= fromExcess
reaction := d.getReactionProducing(chemical)
reaction := d.getReactionProducing(producing)
qtyProduced := int64(reaction.output.Second)
ingredients := reaction.inputs
reactionsNeeded := int64(math.Ceil(float64(qtyRequired) / float64(qtyProduced)))
n := int64(math.Ceil(float64(qtyRequired) / float64(qtyProduced)))
excess[producing] = (qtyProduced * reactionsNeeded) - qtyRequired
excess[chemical] = (qtyProduced * n) - qtyRequired
for ingredient, qtyIngredient := range ingredients {
if ingredient == "ORE" {
oreRequired += int64(int64(qtyIngredient) * n)
for reagent, inputQty := range reaction.inputs {
qtyNeeded := inputQty * reactionsNeeded
if reagent == "ORE" {
oreRequired += qtyNeeded
} else {
needs[ingredient] += int64(qtyIngredient) * n
needs[reagent] += qtyNeeded
}
}
}
@ -97,13 +97,13 @@ func (d *Day14) oreRequiredStock(qty int64) int64 {
}
func (d *Day14) Part1() string {
neededOre := d.oreRequiredStock(1)
neededOre := d.getOreRequiredForFuel(1)
return fmt.Sprintf("%s%d%s", u.TextBold, neededOre, u.TextReset)
}
func (d *Day14) Part2() string {
oreAvailable := int64(1000000000000)
estimate := oreAvailable / d.oreRequiredStock(1)
estimate := oreAvailable / d.getOreRequiredForFuel(1)
high := estimate * 2
low := estimate
@ -112,15 +112,17 @@ func (d *Day14) Part2() string {
lastFailure := high
fuelProduced := low
for math.Abs(float64(lastFailure)-float64(lastSuccess)) > 1 {
oreConsumed := d.oreRequiredStock(fuelProduced)
for math.Abs(float64(lastFailure-lastSuccess)) > 1 {
oreConsumed := d.getOreRequiredForFuel(fuelProduced)
adjustment := (lastFailure - lastSuccess) / 2
if oreConsumed < oreAvailable {
lastSuccess = fuelProduced
fuelProduced += (lastFailure - lastSuccess) / 2
} else {
lastFailure = fuelProduced
fuelProduced -= (lastFailure - lastSuccess) / 2
adjustment = -adjustment
}
fuelProduced += adjustment
}
return fmt.Sprintf("%s%d%s", u.TextBold, lastSuccess, u.TextReset)

332
days/15.go Normal file
View File

@ -0,0 +1,332 @@
package days
//go:generate stringer -type=cellStatus,responseType,dirType -output=15_types_string.go
import (
"fmt"
"math"
"sort"
u "parnic.com/aoc2019/utilities"
)
type point u.Pair[int, int]
type cellStatus int
type responseType int
type dirType int
const (
cellStatusUnknown cellStatus = iota
cellStatusWall
cellStatusOpen
cellStatusGoal
)
const (
responseWall responseType = iota
responseSuccess
responseFoundGoal
)
const maxVisited = 3
const (
dirNorth dirType = iota + 1
dirSouth
dirWest
dirEast
dirFirst = dirNorth
dirLast = dirEast
)
var dirOrders = [][]dirType{
{dirNorth, dirSouth, dirWest, dirEast},
{dirSouth, dirWest, dirEast, dirNorth},
{dirWest, dirEast, dirNorth, dirSouth},
{dirEast, dirNorth, dirSouth, dirWest},
}
// turned out to be unnecessary on multiple datasets i tried. increases the iterations 6x
// var dirOrders = u.GetPermutations(dirNorth, dirSouth, dirWest, dirEast)
type visitedStatus struct {
timesVisited int
distanceFromStart int
}
type Day15 struct {
program u.IntcodeProgram
grid map[point]cellStatus
visited map[point]*visitedStatus
shortestPath []point
pos point
goalPos point
}
func (d *Day15) Parse() {
d.program = u.LoadIntcodeProgram("15p")
d.grid = map[point]cellStatus{
{First: 0, Second: 0}: cellStatusOpen,
}
d.visited = map[point]*visitedStatus{
{First: 0, Second: 0}: {timesVisited: 1},
}
d.shortestPath = []point{{}}
}
func (d Day15) Num() int {
return 15
}
func (d Day15) getPointInDirection(pos point, dir dirType) point {
target := pos
switch dir {
case dirNorth:
target.First--
case dirSouth:
target.First++
case dirWest:
target.Second--
case dirEast:
target.Second++
}
return target
}
func (d Day15) getCellTypeInDirection(pos point, dir dirType) (cellStatus, point) {
target := d.getPointInDirection(pos, dir)
return d.grid[target], target
}
func (d Day15) getAdjacentCellsOfType(pos point, cellType cellStatus) []point {
points := make([]point, 0, 4)
for i := dirFirst; i <= dirLast; i++ {
adjacentCell := d.getPointInDirection(pos, i)
if d.grid[adjacentCell] == cellType {
points = append(points, adjacentCell)
}
}
return points
}
func (d Day15) getDirToNextCellType(pos point, t cellStatus, maxNumVisited int, dirs []dirType) (dirType, point, error) {
for _, dir := range dirs {
cellInDirection, targetCell := d.getCellTypeInDirection(pos, dir)
if cellInDirection == t {
_, visitedTargetExists := d.visited[targetCell]
foundUnknown := t == cellStatusUnknown && !visitedTargetExists
foundOther := t != cellStatusUnknown && visitedTargetExists && d.visited[targetCell].timesVisited <= maxNumVisited
if foundUnknown || foundOther {
return dir, targetCell, nil
}
}
}
return dirFirst, point{}, fmt.Errorf("no %v tiles around %v", t, pos)
}
func (d *Day15) Draw() {
min := point{First: math.MaxInt, Second: math.MaxInt}
max := point{First: math.MinInt, Second: math.MinInt}
for p := range d.grid {
if p.First < min.First {
min.First = p.First
}
if p.First > max.First {
max.First = p.First
}
if p.Second < min.Second {
min.Second = p.Second
}
if p.Second > max.Second {
max.Second = p.Second
}
}
for x := min.First; x <= max.First; x++ {
for y := min.Second; y <= max.Second; y++ {
p := point{First: x, Second: y}
switch d.grid[p] {
case cellStatusGoal:
fmt.Printf("%s@%s", u.ColorBrightGreen, u.TextReset)
case cellStatusOpen:
if p == d.pos {
fmt.Print(u.BackgroundBrightRed)
} else if x == 0 && y == 0 {
fmt.Print(u.BackgroundYellow)
} else if u.ArrayContains(d.shortestPath, p) {
fmt.Print(u.BackgroundGreen)
} else if d.visited[p] != nil && d.visited[p].timesVisited > maxVisited {
fmt.Print(u.ColorYellow)
fmt.Print(u.BackgroundBlack)
} else if d.visited[p] != nil && d.visited[p].timesVisited > 1 {
fmt.Print(u.BackgroundMagenta)
} else {
fmt.Print(u.BackgroundBlue)
}
fmt.Printf(".%s", u.TextReset)
case cellStatusWall:
fmt.Print("█")
case cellStatusUnknown:
fmt.Print(" ")
}
}
fmt.Println()
}
}
func (d *Day15) markShortestPath() {
pos := d.goalPos
checkOffsets := []point{
{First: -1, Second: 0},
{First: 1, Second: 0},
{First: 0, Second: -1},
{First: 0, Second: 1},
}
checkPt := func(pt point) (bool, int) {
if v, exists := d.visited[pt]; exists && d.grid[pt] == cellStatusOpen {
return true, v.distanceFromStart
}
return false, math.MaxInt
}
d.shortestPath = []point{d.goalPos}
for pos.First != 0 || pos.Second != 0 {
lowestDist := math.MaxInt
lowestPoint := point{}
for _, pt := range checkOffsets {
newPt := point{First: pos.First + pt.First, Second: pos.Second + pt.Second}
if found, dist := checkPt(newPt); found && dist < lowestDist {
lowestDist = dist
lowestPoint = newPt
}
}
d.shortestPath = append(d.shortestPath, lowestPoint)
pos = lowestPoint
}
}
func (d *Day15) exploreFullMap() map[point]*visitedStatus {
grids := make([]map[point]cellStatus, 0, len(dirOrders))
goalVisited := d.visited
for _, dirOrder := range dirOrders {
d.program.Reset()
targetPos := point{}
nextDir := dirFirst
distFromStart := 0
d.pos = point{}
d.visited = map[point]*visitedStatus{
{First: 0, Second: 0}: {timesVisited: 1},
}
d.grid = map[point]cellStatus{
{First: 0, Second: 0}: cellStatusOpen,
}
d.program.RunIn(func(inputStep int) int64 {
var err error
nextDir, targetPos, err = d.getDirToNextCellType(d.pos, cellStatusUnknown, 0, dirOrder)
if err != nil {
// ensure we never try to go back into the trapped spot
d.visited[d.pos].timesVisited = maxVisited + 1
for x := 1; x <= maxVisited && err != nil; x++ {
nextDir, targetPos, err = d.getDirToNextCellType(d.pos, cellStatusOpen, x, dirOrder)
}
}
if err != nil {
// d.Draw()
// panic(err)
d.program.Stop()
}
return int64(nextDir)
}, func(val int64, state u.IntcodeProgramState) {
rVal := responseType(val)
p := d.getPointInDirection(d.pos, nextDir)
shouldMove := true
switch rVal {
case responseWall:
d.grid[p] = cellStatusWall
shouldMove = false
case responseSuccess:
d.grid[p] = cellStatusOpen
case responseFoundGoal:
d.grid[p] = cellStatusGoal
}
if shouldMove {
d.pos = targetPos
if d.visited[d.pos] == nil {
d.visited[d.pos] = &visitedStatus{}
distFromStart++
} else {
distFromStart--
}
d.visited[d.pos].timesVisited++
d.visited[d.pos].distanceFromStart = distFromStart
}
if rVal == responseFoundGoal {
// d.Draw()
d.goalPos = targetPos
goalVisited = d.visited
}
})
grids = append(grids, d.grid)
}
d.grid = map[point]cellStatus{
{First: 0, Second: 0}: cellStatusOpen,
}
for _, grid := range grids {
keys := u.MapKeys(grid)
for _, key := range keys {
d.grid[key] = grid[key]
}
}
return goalVisited
}
func (d *Day15) tagDistanceRecursive(pos, last point, dist int, distances map[point]int) {
distances[pos] = dist
for _, cell := range d.getAdjacentCellsOfType(pos, cellStatusOpen) {
if cell == last {
continue
}
d.tagDistanceRecursive(cell, pos, dist+1, distances)
}
}
func (d *Day15) Part1() string {
d.visited = d.exploreFullMap()
d.markShortestPath()
for _, visited := range d.visited {
visited.timesVisited = 1
}
d.pos = point{}
d.Draw()
return fmt.Sprintf("Moves required to reach target: %s%d%s", u.TextBold, d.visited[d.goalPos].distanceFromStart, u.TextReset)
}
func (d *Day15) Part2() string {
startLoc := d.goalPos
distanceMap := map[point]int{startLoc: 0}
d.tagDistanceRecursive(startLoc, point{}, 0, distanceMap)
cellDistances := u.MapValues(distanceMap)
sort.Slice(cellDistances, func(i, j int) bool { return cellDistances[i] > cellDistances[j] })
return fmt.Sprintf("%s%d%s", u.TextBold, cellDistances[0], u.TextReset)
}

66
days/15_types_string.go Normal file
View File

@ -0,0 +1,66 @@
// Code generated by "stringer -type=cellStatus,responseType,dirType -output=15_types_string.go"; DO NOT EDIT.
package days
import "strconv"
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[cellStatusUnknown-0]
_ = x[cellStatusWall-1]
_ = x[cellStatusOpen-2]
_ = x[cellStatusGoal-3]
}
const _cellStatus_name = "cellStatusUnknowncellStatusWallcellStatusOpencellStatusGoal"
var _cellStatus_index = [...]uint8{0, 17, 31, 45, 59}
func (i cellStatus) String() string {
if i < 0 || i >= cellStatus(len(_cellStatus_index)-1) {
return "cellStatus(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _cellStatus_name[_cellStatus_index[i]:_cellStatus_index[i+1]]
}
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[responseWall-0]
_ = x[responseSuccess-1]
_ = x[responseFoundGoal-2]
}
const _responseType_name = "responseWallresponseSuccessresponseFoundGoal"
var _responseType_index = [...]uint8{0, 12, 27, 44}
func (i responseType) String() string {
if i < 0 || i >= responseType(len(_responseType_index)-1) {
return "responseType(" + strconv.FormatInt(int64(i), 10) + ")"
}
return _responseType_name[_responseType_index[i]:_responseType_index[i+1]]
}
func _() {
// An "invalid array index" compiler error signifies that the constant values have changed.
// Re-run the stringer command to generate them again.
var x [1]struct{}
_ = x[dirNorth-1]
_ = x[dirSouth-2]
_ = x[dirWest-3]
_ = x[dirEast-4]
}
const _dirType_name = "dirNorthdirSouthdirWestdirEast"
var _dirType_index = [...]uint8{0, 8, 16, 23, 30}
func (i dirType) String() string {
i -= 1
if i < 0 || i >= dirType(len(_dirType_index)-1) {
return "dirType(" + strconv.FormatInt(int64(i+1), 10) + ")"
}
return _dirType_name[_dirType_index[i]:_dirType_index[i+1]]
}

View File

@ -1,14 +1,16 @@
package days
import (
"parnic.com/aoc2019/utilities"
"fmt"
u "parnic.com/aoc2019/utilities"
)
type DayTemplate struct {
}
func (d *DayTemplate) Parse() {
utilities.GetIntLines("Templatep")
u.GetIntLines("Templatep")
}
func (d DayTemplate) Num() int {
@ -16,9 +18,9 @@ func (d DayTemplate) Num() int {
}
func (d *DayTemplate) Part1() string {
return ""
return fmt.Sprintf("%s%d%s", u.TextBold, 0, u.TextReset)
}
func (d *DayTemplate) Part2() string {
return ""
return fmt.Sprintf("%s%d%s", u.TextBold, 0, u.TextReset)
}

View File

@ -1,60 +1,56 @@
11 RVCS => 8 CBMDT
29 QXPB, 8 QRGRH => 8 LGMKD
3 VPRVD => 6 PMFZG
1 CNWNQ, 11 MJVXS => 6 SPLM
13 SPDRZ, 13 PMFZG => 2 BLFM
8 QWPFN => 7 LWVB
1 SPLM => 8 TKWQ
2 QRGRH, 6 CNWNQ => 7 DTZW
2 DMLT, 1 SPLM, 1 TMDK => 9 NKNS
1 MJVXS, 1 HLBV => 7 PQCQH
1 JZHZP, 9 LWVB => 7 MJSCQ
29 DGFR => 7 QRGRH
14 XFLKQ, 2 NKNS, 4 KMNJF, 3 MLZGQ, 7 TKWQ, 24 WTDW, 11 CBMDT => 4 GJKX
4 TKWQ, 1 WLCFR => 4 PDKGT
2 NKNS => 4 GDKL
4 WRZST => 9 XFLKQ
19 DGFR => 4 VPRVD
10 MJSCQ, 4 QWPFN, 4 QXPB => 2 MLZGQ
1 JZHZP => 7 QWPFN
1 XFLKQ => 9 FQGVL
3 GQGXC => 9 VHGP
3 NQZTV, 1 JZHZP => 2 NVZWL
38 WLCFR, 15 GJKX, 44 LGMKD, 2 CBVXG, 2 GDKL, 77 FQGVL, 10 MKRCZ, 29 WJQD, 33 BWXGC, 19 PQCQH, 24 BKXD => 1 FUEL
102 ORE => 5 DGFR
17 NWKLB, 1 SBPLK => 5 HRQM
3 BWXGC => 8 TQDP
1 TQDP => 2 PSZDZ
2 MJVXS => 9 WNXG
2 NBTW, 1 HRQM => 2 SVHBH
8 CNWNQ, 1 DTZW => 4 RVCS
4 VHGP, 20 WNXG, 2 SVHBH => 3 SPDRZ
110 ORE => 5 TXMC
10 QRGRH => 5 NWKLB
1 SBPLK => 3 MJVXS
9 DGFR => 5 RFSRL
5 LBTV => 3 DMLT
1 NWKLB, 1 KMNJF, 1 HDQXB, 6 LBTV, 2 PSZDZ, 34 PMFZG, 2 SVHBH => 2 WJQD
1 RVCS => 5 MKRCZ
14 NQZTV, 3 FPLT, 1 SJMS => 2 GQGXC
18 RFSRL, 13 VHGP, 23 NBTW => 5 WTDW
1 VHGP, 6 TKWQ => 7 QXPB
1 JZHZP, 1 CNWNQ => 5 KMNJF
109 ORE => 9 BWXGC
2 CNWNQ, 1 PDKGT, 2 KMNJF => 5 HDQXB
1 PDKGT, 18 WRZST, 9 MJSCQ, 3 VHGP, 1 BLFM, 1 LGMKD, 7 WLCFR => 2 BKXD
11 MLJK => 6 FPLT
8 DGFR, 2 TXMC, 3 WJRC => 9 SJMS
2 SBPLK => 1 LBTV
22 QWPFN => 4 WRZST
5 WRZST, 22 WNXG, 1 VHGP => 7 NBTW
7 RVCS => 9 TMDK
1 DGFR, 14 TXMC => 5 JZHZP
2 JZHZP => 3 SBPLK
19 PDKGT => 8 HLBV
195 ORE => 6 WJRC
6 GQGXC => 8 CNWNQ
1 NVZWL, 4 GQGXC => 2 CBVXG
1 NVZWL, 1 KMNJF => 8 WLCFR
153 ORE => 4 MLJK
1 BWXGC => 6 NQZTV
6 WBVJ, 16 CDVNK => 2 PJBZT
135 ORE => 8 MWDXJ
27 NBRHT, 2 NSWK, 2 CMHMQ, 29 NFCB, 11 KNGJ, 12 MGCKC, 56 NHTKL, 7 WNFSV => 1 FUEL
1 SFJFX, 3 MXNK => 4 NLSBZ
2 PFKRW, 1 VXFRX, 22 QDJCL => 6 GBDG
7 TSTF, 4 ZLJN => 7 DMWS
5 KPCF, 1 DLMDJ, 1 FNWGH => 6 TSTF
8 DTWKS, 1 GBDG => 4 CGZQ
26 CNWZM, 4 KPCF => 3 DTWKS
1 JVLHM, 7 DTWKS, 7 PJBZT => 8 MRPHV
2 MWDXJ => 3 VHFPC
1 WXNW, 6 PFKRW => 7 ZVGVP
2 ZVGVP => 1 CMHMQ
8 JVLHM, 11 XRKN, 1 HCGKZ => 8 CHZLX
20 TSTF => 4 XDZMZ
3 CMHMQ, 7 ZVGVP, 10 XRKN => 9 FNWGH
12 HCGKZ, 4 NLSBZ, 15 RWRDP, 4 MRPHV, 31 KRDV, 6 PMXK, 2 NFVZ => 7 KNGJ
1 TXZCM => 9 BMPJ
2 ZFXQ => 3 NBRHT
13 JVLHM, 1 VHFPC => 3 PBJPZ
7 HCGKZ => 7 PMXK
2 RWRDP, 3 VSTQ, 12 PMXK => 7 MXNK
1 PJBZT, 3 QRSK => 1 KRDV
1 MGCKC, 6 CMHMQ => 6 PQTVS
1 TNHCS, 24 ZLJN => 4 RWRDP
5 MWDXJ, 1 WXNW => 9 QBCLF
1 ZFXQ, 1 DLMDJ => 4 DJXRM
1 ZFXQ => 2 CNWZM
1 KPCF => 6 ZXDVF
2 MRPHV => 1 GSTG
5 BMPJ, 2 ZLJN => 8 XQJZ
1 MWDXJ, 1 ZVGVP => 3 CDVNK
3 NFCB, 3 CMHMQ, 1 MWDXJ => 4 XRKN
1 WXNW, 1 TXZCM => 5 ZLJN
4 ZXDVF => 4 WBVJ
2 GBDG => 4 KPCF
4 CHZLX, 7 ZFXQ, 14 PQTVS => 9 VSTQ
3 TXZCM, 7 ZLJN, 7 ZXDVF => 9 JVLHM
1 DMWS, 3 TSTF => 5 HCGKZ
2 CGZQ => 4 NFVZ
2 PQTVS, 9 VMNJ => 9 TXZCM
3 KPCF => 4 DLMDJ
7 VMNJ, 24 XQJZ, 7 GSTG, 8 NLSBZ, 10 MGCKC, 2 SFJFX, 18 BMPJ => 1 NSWK
41 CNWZM, 5 DJXRM, 1 QRSK, 1 KPCF, 15 XDZMZ, 3 MRPHV, 1 NLSBZ, 9 KRDV => 2 WNFSV
10 PBJPZ, 29 BMPJ, 2 PMXK => 7 SFJFX
116 ORE => 4 WXNW
2 CNWZM => 2 TNHCS
10 QBCLF => 7 NFCB
1 QBCLF => 2 ZFXQ
15 ZLJN => 7 QRSK
183 ORE => 3 QDJCL
11 GBDG => 5 VMNJ
4 DMWS, 3 QRSK => 3 NHTKL
124 ORE => 6 VXFRX
1 MWDXJ => 6 MGCKC
108 ORE => 9 PFKRW

1
inputs/15p.txt Normal file
View File

@ -0,0 +1 @@
3,1033,1008,1033,1,1032,1005,1032,31,1008,1033,2,1032,1005,1032,58,1008,1033,3,1032,1005,1032,81,1008,1033,4,1032,1005,1032,104,99,1002,1034,1,1039,1001,1036,0,1041,1001,1035,-1,1040,1008,1038,0,1043,102,-1,1043,1032,1,1037,1032,1042,1105,1,124,102,1,1034,1039,1001,1036,0,1041,1001,1035,1,1040,1008,1038,0,1043,1,1037,1038,1042,1106,0,124,1001,1034,-1,1039,1008,1036,0,1041,1002,1035,1,1040,1002,1038,1,1043,101,0,1037,1042,1106,0,124,1001,1034,1,1039,1008,1036,0,1041,101,0,1035,1040,1002,1038,1,1043,101,0,1037,1042,1006,1039,217,1006,1040,217,1008,1039,40,1032,1005,1032,217,1008,1040,40,1032,1005,1032,217,1008,1039,37,1032,1006,1032,165,1008,1040,9,1032,1006,1032,165,1101,2,0,1044,1105,1,224,2,1041,1043,1032,1006,1032,179,1101,1,0,1044,1106,0,224,1,1041,1043,1032,1006,1032,217,1,1042,1043,1032,1001,1032,-1,1032,1002,1032,39,1032,1,1032,1039,1032,101,-1,1032,1032,101,252,1032,211,1007,0,50,1044,1106,0,224,1102,0,1,1044,1105,1,224,1006,1044,247,1001,1039,0,1034,102,1,1040,1035,102,1,1041,1036,101,0,1043,1038,102,1,1042,1037,4,1044,1106,0,0,37,22,74,27,37,99,30,8,72,31,49,29,51,32,85,21,39,72,2,2,43,94,31,11,76,43,95,21,38,8,90,13,39,97,54,47,14,6,20,49,5,30,97,9,99,64,71,24,36,87,52,94,36,18,52,42,83,38,98,53,26,87,69,32,18,94,2,93,97,15,65,65,21,40,99,19,91,13,4,89,38,70,65,41,73,49,62,54,37,46,14,49,88,86,13,89,23,89,10,3,48,57,92,43,65,4,35,97,48,10,19,64,3,79,38,87,6,13,71,49,74,43,92,8,4,71,6,35,85,98,94,6,38,59,80,65,46,62,63,62,49,61,68,6,7,64,66,40,56,82,59,30,85,45,57,36,86,70,25,83,31,96,65,19,16,67,55,36,49,54,29,75,69,3,3,37,75,49,23,65,22,6,52,75,31,7,87,85,19,48,97,65,51,78,10,35,40,59,54,14,85,6,30,94,68,42,87,46,75,26,82,36,21,65,90,16,59,14,76,55,37,41,99,80,9,79,12,59,17,75,2,40,52,45,76,45,16,82,13,55,61,14,11,49,97,81,99,38,35,20,98,51,64,13,24,85,94,38,25,87,1,42,89,18,32,54,55,17,15,84,98,25,31,21,55,44,57,59,11,78,49,72,87,20,7,33,91,80,75,18,33,37,52,7,26,87,65,36,52,92,6,8,95,89,37,38,57,25,23,71,75,47,20,87,90,37,54,38,77,32,39,67,16,69,62,15,96,47,91,95,18,96,24,45,21,64,9,72,2,54,65,39,36,54,23,71,74,18,26,97,35,44,29,87,54,48,31,55,33,85,74,13,99,82,39,35,97,43,20,62,58,86,98,41,47,92,79,74,10,85,28,66,86,18,35,5,84,67,13,91,47,44,1,84,56,32,96,7,77,21,88,92,38,31,65,82,87,45,55,4,60,58,64,49,53,3,63,32,52,43,10,66,75,96,53,11,95,44,36,16,65,91,47,32,9,3,73,29,25,93,29,18,88,45,41,46,12,94,13,89,5,36,94,88,33,10,10,2,52,90,19,63,26,84,12,76,16,42,75,63,39,32,72,72,84,70,2,63,33,74,43,68,38,84,72,44,89,18,24,78,69,4,80,41,54,75,72,4,16,91,5,48,30,64,38,4,52,38,30,95,99,32,38,52,35,58,71,38,89,86,25,84,88,41,39,32,56,79,12,52,19,80,46,66,38,32,69,67,6,87,88,36,59,51,5,33,46,45,82,15,57,80,91,12,86,29,34,15,61,19,73,46,82,60,73,13,52,36,67,3,49,87,39,12,98,58,87,32,82,47,65,6,87,71,13,17,65,69,14,34,42,82,42,1,77,63,10,63,28,90,24,13,99,19,38,68,62,44,2,65,81,95,7,54,24,58,16,58,48,95,9,80,9,51,73,23,96,49,64,58,1,6,72,69,39,2,10,63,36,9,85,59,90,41,2,72,77,23,23,80,75,33,6,20,18,59,39,36,89,35,89,42,42,22,37,24,30,51,53,43,78,48,27,76,84,22,81,72,25,95,28,15,51,58,48,7,1,90,72,19,37,52,60,39,81,20,70,6,39,82,26,77,14,96,52,30,84,33,66,80,5,52,15,72,46,55,2,21,8,97,79,43,8,91,27,67,5,18,74,71,34,51,6,83,25,52,92,5,15,85,11,72,33,85,30,59,6,84,29,51,77,99,43,95,44,83,95,89,27,54,16,85,90,82,34,98,59,87,12,73,25,74,29,95,82,51,5,81,46,51,0,0,21,21,1,10,1,0,0,0,0,0,0

View File

@ -45,6 +45,7 @@ var dayMap = []day{
&days.Day12{},
&days.Day13{},
&days.Day14{},
&days.Day15{},
}
func main() {

View File

@ -27,6 +27,7 @@ type IntcodeProgram struct {
memory []int64
program []int64
relativeBase int
haltRequested bool
}
type IntcodeProgramState struct {
@ -141,7 +142,7 @@ func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOut
p.init()
inputsRequested := 0
for instructionPointer := 0; instructionPointer < len(p.program); {
for instructionPointer := 0; instructionPointer < len(p.program) && !p.haltRequested; {
instruction := p.GetMemory(instructionPointer)
instructionPointer++
@ -247,4 +248,10 @@ func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOut
panic(fmt.Sprintf("exception executing program - unhandled opcode %d", opcode))
}
}
p.haltRequested = false
}
func (p *IntcodeProgram) Stop() {
p.haltRequested = true
}

View File

@ -1,5 +1,7 @@
package utilities
import "math"
func GCD[T Integer](a, b T) T {
if b == 0 {
return a
@ -25,3 +27,23 @@ func LCM[T Integer](nums ...T) uint64 {
func lcm[T Integer](a, b T) uint64 {
return uint64(a*b) / uint64(GCD(a, b))
}
func Min[T Number](nums ...T) T {
numNums := len(nums)
if numNums == 2 {
return T(math.Min(float64(nums[0]), float64(nums[1])))
}
if numNums == 0 {
return 0
}
least := nums[0]
for i := 1; i < numNums; i++ {
if nums[i] < least {
least = nums[i]
}
}
return least
}

View File

@ -4,7 +4,7 @@ type Permutable interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}
func GetPermutations[T Permutable](arr []T) [][]T {
func GetPermutations[T Permutable](arr ...T) [][]T {
var helper func([]T, int)
res := [][]T{}