Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
0a6d8ab256
|
|||
4c58e5c51c
|
|||
36615d7f06
|
|||
334aaee7a5
|
|||
1a6529c7d2
|
|||
34e6055782
|
@ -48,6 +48,7 @@ type Day17 struct {
|
|||||||
|
|
||||||
func (d *Day17) Parse() {
|
func (d *Day17) Parse() {
|
||||||
d.program = u.LoadIntcodeProgram("17p")
|
d.program = u.LoadIntcodeProgram("17p")
|
||||||
|
// d.program.SetDebugASCIIPrint(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d Day17) Num() int {
|
func (d Day17) Num() int {
|
||||||
@ -388,9 +389,8 @@ func (d *Day17) Part2() string {
|
|||||||
row := 0
|
row := 0
|
||||||
var outputState int
|
var outputState int
|
||||||
var lastOutput int64
|
var lastOutput int64
|
||||||
var instructionStr string
|
|
||||||
d.program.RunIn(func(inputStep int) int64 {
|
d.program.RunIn(func(inputStep int) int64 {
|
||||||
return int64(instructionStr[inputStep-1])
|
panic("unexpected read")
|
||||||
}, func(val int64, state u.IntcodeProgramState) {
|
}, func(val int64, state u.IntcodeProgramState) {
|
||||||
rVal := rune(val)
|
rVal := rune(val)
|
||||||
if outputState == 0 {
|
if outputState == 0 {
|
||||||
@ -401,7 +401,7 @@ func (d *Day17) Part2() string {
|
|||||||
|
|
||||||
if rVal == '\n' && lastOutput == '\n' {
|
if rVal == '\n' && lastOutput == '\n' {
|
||||||
if outputState == 0 {
|
if outputState == 0 {
|
||||||
instructionStr = beforeGrid.solvePath(beforeBotLocation, beforeBotFacing)
|
d.program.FeedInputString(beforeGrid.solvePath(beforeBotLocation, beforeBotFacing))
|
||||||
}
|
}
|
||||||
outputState++
|
outputState++
|
||||||
row = 0
|
row = 0
|
||||||
|
398
days/20.go
Normal file
398
days/20.go
Normal file
@ -0,0 +1,398 @@
|
|||||||
|
package days
|
||||||
|
|
||||||
|
import (
|
||||||
|
"container/heap"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
u "parnic.com/aoc2019/utilities"
|
||||||
|
)
|
||||||
|
|
||||||
|
type day20Cell int8
|
||||||
|
type day20Graph map[day20Portal][]u.Pair[day20Portal, int]
|
||||||
|
|
||||||
|
const (
|
||||||
|
day20CellWall day20Cell = iota
|
||||||
|
day20CellPath
|
||||||
|
day20CellDonutHole
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
day20AdjacentOffsets = []u.Vec2i{
|
||||||
|
{X: -1, Y: 0},
|
||||||
|
{X: 1, Y: 0},
|
||||||
|
{X: 0, Y: -1},
|
||||||
|
{X: 0, Y: 1},
|
||||||
|
}
|
||||||
|
|
||||||
|
entrancePortal = day20Portal{name: "AA"}
|
||||||
|
exitPortal = day20Portal{name: "ZZ"}
|
||||||
|
)
|
||||||
|
|
||||||
|
type day20Portal struct {
|
||||||
|
name string
|
||||||
|
inner bool
|
||||||
|
depth int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Day20 struct {
|
||||||
|
grid [][]day20Cell
|
||||||
|
entrance u.Vec2i
|
||||||
|
exit u.Vec2i
|
||||||
|
portals map[day20Portal]u.Vec2i
|
||||||
|
portalNames []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day20) Parse() {
|
||||||
|
d.portals = make(map[day20Portal]u.Vec2i)
|
||||||
|
d.portalNames = make([]string, 0)
|
||||||
|
|
||||||
|
lines := u.GetStringLines("20p")
|
||||||
|
d.grid = make([][]day20Cell, len(lines)-4)
|
||||||
|
currPortal := strings.Builder{}
|
||||||
|
|
||||||
|
for row, line := range lines {
|
||||||
|
y := row - 2
|
||||||
|
isGridRow := row >= 2 && row < len(lines)-2
|
||||||
|
|
||||||
|
if isGridRow {
|
||||||
|
d.grid[y] = make([]day20Cell, len(line)-4)
|
||||||
|
}
|
||||||
|
|
||||||
|
for col, ch := range lines[row] {
|
||||||
|
x := col - 2
|
||||||
|
isGridCol := col >= 2 && col < len(line)-2
|
||||||
|
|
||||||
|
if isGridRow && isGridCol {
|
||||||
|
if ch == '#' {
|
||||||
|
d.grid[y][x] = day20CellWall
|
||||||
|
} else if ch == '.' {
|
||||||
|
d.grid[y][x] = day20CellPath
|
||||||
|
} else {
|
||||||
|
d.grid[y][x] = day20CellDonutHole
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ch >= 'A' && ch <= 'Z' {
|
||||||
|
portalX := 0
|
||||||
|
portalY := 0
|
||||||
|
if len(line) > col+1 && line[col+1] >= 'A' && line[col+1] <= 'Z' {
|
||||||
|
currPortal.WriteRune(ch)
|
||||||
|
currPortal.WriteRune(rune(line[col+1]))
|
||||||
|
|
||||||
|
if len(line) > col+2 && line[col+2] == '.' {
|
||||||
|
portalY = y
|
||||||
|
portalX = x + 2
|
||||||
|
} else if col-1 >= 0 && line[col-1] == '.' {
|
||||||
|
portalY = y
|
||||||
|
portalX = x - 1
|
||||||
|
} else {
|
||||||
|
panic("!")
|
||||||
|
}
|
||||||
|
} else if len(lines) > row+1 && lines[row+1][col] >= 'A' && lines[row+1][col] <= 'Z' {
|
||||||
|
currPortal.WriteRune(ch)
|
||||||
|
currPortal.WriteRune(rune(lines[row+1][col]))
|
||||||
|
|
||||||
|
if len(lines) > row+2 && lines[row+2][col] == '.' {
|
||||||
|
portalY = y + 2
|
||||||
|
portalX = x
|
||||||
|
} else if row-1 >= 0 && lines[row-1][col] == '.' {
|
||||||
|
portalY = y - 1
|
||||||
|
portalX = x
|
||||||
|
} else {
|
||||||
|
panic("!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if currPortal.Len() == 2 {
|
||||||
|
portalName := currPortal.String()
|
||||||
|
portalVec := u.Vec2i{X: portalX, Y: portalY}
|
||||||
|
|
||||||
|
if portalName == entrancePortal.name {
|
||||||
|
d.entrance = portalVec
|
||||||
|
} else if portalName == exitPortal.name {
|
||||||
|
d.exit = portalVec
|
||||||
|
} else {
|
||||||
|
portal := day20Portal{
|
||||||
|
name: portalName,
|
||||||
|
inner: !d.isOuterPortal(portalVec),
|
||||||
|
}
|
||||||
|
d.portals[portal] = portalVec
|
||||||
|
|
||||||
|
u.AddToArray(&d.portalNames, portalName)
|
||||||
|
}
|
||||||
|
|
||||||
|
currPortal.Reset()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day20) Num() int {
|
||||||
|
return 20
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day20) isPortal(vec u.Vec2i) (bool, int) {
|
||||||
|
if d.grid[vec.Y][vec.X] != day20CellPath {
|
||||||
|
return false, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, name := range d.portalNames {
|
||||||
|
p, exists := d.portals[day20Portal{name: name, inner: !d.isOuterPortal(vec)}]
|
||||||
|
if exists && vec == p {
|
||||||
|
return true, i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day20) Draw() {
|
||||||
|
for y := range d.grid {
|
||||||
|
for x := range d.grid[y] {
|
||||||
|
switch d.grid[y][x] {
|
||||||
|
case day20CellWall:
|
||||||
|
fmt.Print("█")
|
||||||
|
case day20CellDonutHole:
|
||||||
|
fmt.Print(" ")
|
||||||
|
case day20CellPath:
|
||||||
|
posVec := u.Vec2i{X: x, Y: y}
|
||||||
|
if posVec == d.entrance {
|
||||||
|
fmt.Print("@")
|
||||||
|
} else if posVec == d.exit {
|
||||||
|
fmt.Print("!")
|
||||||
|
} else {
|
||||||
|
isPortal, portalIdx := d.isPortal(posVec)
|
||||||
|
if isPortal {
|
||||||
|
ch := 'a' + portalIdx
|
||||||
|
if ch > 'z' {
|
||||||
|
ch = 'A' + (portalIdx - 26)
|
||||||
|
}
|
||||||
|
fmt.Printf("%c", ch)
|
||||||
|
} else {
|
||||||
|
fmt.Print(" ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day20) isOuterPortal(pos u.Vec2i) bool {
|
||||||
|
return pos.X == 0 || pos.Y == 0 || pos.X == len(d.grid[0])-1 || pos.Y == len(d.grid)-1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day20) findAdjacentCells(inPos u.Vec2i) []u.Pair[day20Portal, int] {
|
||||||
|
found := make([]u.Pair[day20Portal, int], 0)
|
||||||
|
|
||||||
|
getAdjacent := func(pos u.Vec2i) []u.Vec2i {
|
||||||
|
retAdjacent := make([]u.Vec2i, 0, len(day20AdjacentOffsets))
|
||||||
|
for _, off := range day20AdjacentOffsets {
|
||||||
|
offVec := u.Vec2i{X: pos.X + off.X, Y: pos.Y + off.Y}
|
||||||
|
if offVec.Y < 0 || offVec.Y >= len(d.grid) || offVec.X < 0 || offVec.X >= len(d.grid[0]) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if d.grid[offVec.Y][offVec.X] != day20CellPath {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
retAdjacent = append(retAdjacent, offVec)
|
||||||
|
}
|
||||||
|
|
||||||
|
return retAdjacent
|
||||||
|
}
|
||||||
|
|
||||||
|
queue := make([]u.Pair[int, u.Vec2i], 0)
|
||||||
|
visited := map[u.Vec2i]bool{
|
||||||
|
inPos: true,
|
||||||
|
}
|
||||||
|
for _, adjacent := range getAdjacent(inPos) {
|
||||||
|
queue = append(queue, u.Pair[int, u.Vec2i]{First: 1, Second: adjacent})
|
||||||
|
}
|
||||||
|
|
||||||
|
for len(queue) > 0 {
|
||||||
|
next := queue[0]
|
||||||
|
queue = queue[1:]
|
||||||
|
|
||||||
|
if _, exists := visited[next.Second]; !exists {
|
||||||
|
visited[next.Second] = true
|
||||||
|
|
||||||
|
adjacentIsPortal, portalIdx := d.isPortal(next.Second)
|
||||||
|
if adjacentIsPortal || next.Second == d.entrance || next.Second == d.exit {
|
||||||
|
var portalName string
|
||||||
|
if next.Second == d.entrance {
|
||||||
|
portalName = entrancePortal.name
|
||||||
|
} else if next.Second == d.exit {
|
||||||
|
portalName = exitPortal.name
|
||||||
|
} else {
|
||||||
|
portalName = d.portalNames[portalIdx]
|
||||||
|
}
|
||||||
|
|
||||||
|
alreadyFound := false
|
||||||
|
for _, p := range found {
|
||||||
|
if p.First.name == portalName {
|
||||||
|
alreadyFound = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !alreadyFound {
|
||||||
|
found = append(found, u.Pair[day20Portal, int]{First: day20Portal{
|
||||||
|
name: portalName,
|
||||||
|
inner: !d.isOuterPortal(next.Second),
|
||||||
|
}, Second: next.First})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, neighbor := range getAdjacent(next.Second) {
|
||||||
|
if _, exists := visited[neighbor]; !exists {
|
||||||
|
queue = append(queue, u.Pair[int, u.Vec2i]{First: next.First + 1, Second: neighbor})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
|
type day20PriorityQueue struct {
|
||||||
|
distance int
|
||||||
|
neighbor day20Portal
|
||||||
|
}
|
||||||
|
type day20PriorityQueueHeap []day20PriorityQueue
|
||||||
|
|
||||||
|
func (h day20PriorityQueueHeap) Len() int { return len(h) }
|
||||||
|
func (h day20PriorityQueueHeap) Less(i, j int) bool { return h[i].distance < h[j].distance }
|
||||||
|
func (h day20PriorityQueueHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
|
||||||
|
|
||||||
|
func (h *day20PriorityQueueHeap) Push(x any) {
|
||||||
|
*h = append(*h, x.(day20PriorityQueue))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *day20PriorityQueueHeap) Pop() any {
|
||||||
|
old := *h
|
||||||
|
n := len(old)
|
||||||
|
x := old[n-1]
|
||||||
|
*h = old[0 : n-1]
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day20) dijkstra(graph day20Graph, start, end day20Portal, neighborFunc func(inPortal day20Portal) []u.Pair[day20Portal, int]) int {
|
||||||
|
distance := make(map[day20Portal]int)
|
||||||
|
|
||||||
|
ih := day20PriorityQueueHeap{
|
||||||
|
day20PriorityQueue{
|
||||||
|
distance: 0,
|
||||||
|
neighbor: start,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
heap.Init(&ih)
|
||||||
|
visited := make(map[day20Portal]bool)
|
||||||
|
|
||||||
|
for ih.Len() > 0 {
|
||||||
|
node := heap.Pop(&ih).(day20PriorityQueue)
|
||||||
|
|
||||||
|
if node.neighbor == end {
|
||||||
|
return node.distance
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, exists := visited[node.neighbor]; exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
visited[node.neighbor] = true
|
||||||
|
|
||||||
|
for _, p := range neighborFunc(node.neighbor) {
|
||||||
|
if _, exists := visited[p.First]; exists {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
newDistance := node.distance + p.Second
|
||||||
|
if dist, exists := distance[p.First]; !exists || newDistance < dist {
|
||||||
|
distance[p.First] = newDistance
|
||||||
|
heap.Push(&ih, day20PriorityQueue{
|
||||||
|
distance: newDistance,
|
||||||
|
neighbor: p.First,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return math.MaxInt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day20) buildGraph() day20Graph {
|
||||||
|
graph := make(day20Graph, len(d.portals)+1)
|
||||||
|
|
||||||
|
adjacent := d.findAdjacentCells(d.entrance)
|
||||||
|
graph[entrancePortal] = adjacent
|
||||||
|
|
||||||
|
for portal, portalVec := range d.portals {
|
||||||
|
adjacent = d.findAdjacentCells(portalVec)
|
||||||
|
graph[portal] = adjacent
|
||||||
|
}
|
||||||
|
|
||||||
|
return graph
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day20) getDepthNeighbors(graph day20Graph, portal day20Portal) []u.Pair[day20Portal, int] {
|
||||||
|
basePortal := portal
|
||||||
|
basePortal.depth = 0
|
||||||
|
baseNeighbors := graph[basePortal]
|
||||||
|
|
||||||
|
neighbors := make([]u.Pair[day20Portal, int], 0)
|
||||||
|
|
||||||
|
if portal.inner {
|
||||||
|
n := day20Portal{name: portal.name, inner: false, depth: portal.depth + 1}
|
||||||
|
neighbors = append(neighbors, u.Pair[day20Portal, int]{First: n, Second: 1})
|
||||||
|
}
|
||||||
|
|
||||||
|
if portal.depth == 0 {
|
||||||
|
for _, i := range baseNeighbors {
|
||||||
|
if i.First.inner || i.First.name == entrancePortal.name || i.First.name == exitPortal.name {
|
||||||
|
neighbors = append(neighbors, u.Pair[day20Portal, int]{First: i.First, Second: i.Second})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if !portal.inner {
|
||||||
|
n := day20Portal{name: portal.name, inner: true, depth: portal.depth - 1}
|
||||||
|
neighbors = append(neighbors, u.Pair[day20Portal, int]{First: n, Second: 1})
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, i := range baseNeighbors {
|
||||||
|
if i.First.name != entrancePortal.name && i.First.name != exitPortal.name {
|
||||||
|
n := day20Portal{name: i.First.name, inner: i.First.inner, depth: portal.depth}
|
||||||
|
neighbors = append(neighbors, u.Pair[day20Portal, int]{First: n, Second: i.Second})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return neighbors
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day20) Part1() string {
|
||||||
|
// d.Draw()
|
||||||
|
|
||||||
|
graph := d.buildGraph()
|
||||||
|
|
||||||
|
for portal, adjacent := range graph {
|
||||||
|
if portal.name == entrancePortal.name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
graph[portal] = append(adjacent, u.Pair[day20Portal, int]{First: day20Portal{name: portal.name, inner: !portal.inner}, Second: 1})
|
||||||
|
}
|
||||||
|
|
||||||
|
distance := d.dijkstra(graph, entrancePortal, exitPortal, func(inPortal day20Portal) []u.Pair[day20Portal, int] { return graph[inPortal] })
|
||||||
|
return fmt.Sprintf("Steps to traverse maze: %s%d%s", u.TextBold, distance, u.TextReset)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day20) Part2() string {
|
||||||
|
graph := d.buildGraph()
|
||||||
|
distance := d.dijkstra(graph, entrancePortal, exitPortal, func(inPortal day20Portal) []u.Pair[day20Portal, int] { return d.getDepthNeighbors(graph, inPortal) })
|
||||||
|
return fmt.Sprintf("Steps to traverse recursive maze: %s%d%s", u.TextBold, distance, u.TextReset)
|
||||||
|
}
|
85
days/21.go
Normal file
85
days/21.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package days
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
u "parnic.com/aoc2019/utilities"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Day21 struct {
|
||||||
|
program u.IntcodeProgram
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day21) Parse() {
|
||||||
|
d.program = u.LoadIntcodeProgram("21p")
|
||||||
|
// d.program.SetDebugASCIIPrint(true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day21) Num() int {
|
||||||
|
return 21
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day21) Part1() string {
|
||||||
|
// if there's any hole up to 3 ahead of us but there's ground where we'd land if we jumped
|
||||||
|
// (a jump takes 4 spaces), go ahead and jump
|
||||||
|
cmds := []string{
|
||||||
|
// check if a hole at 1 or 2 ahead
|
||||||
|
"NOT A T",
|
||||||
|
"NOT B J",
|
||||||
|
// store that result in J
|
||||||
|
"OR T J",
|
||||||
|
// check if a hole at 3 ahead
|
||||||
|
"NOT C T",
|
||||||
|
// store hole in 1, 2, or 3 in T
|
||||||
|
"OR J T",
|
||||||
|
// set J true if hole in 1, 2, or 3
|
||||||
|
"OR T J",
|
||||||
|
// set J true if also no hole at 4 ahead
|
||||||
|
"AND D J",
|
||||||
|
"WALK",
|
||||||
|
}
|
||||||
|
instructionStr := strings.Join(cmds, "\n") + "\n"
|
||||||
|
d.program.FeedInputString(instructionStr)
|
||||||
|
|
||||||
|
res := d.program.Run()
|
||||||
|
|
||||||
|
return fmt.Sprintf("Hull damage value when walking: %s%d%s", u.TextBold, res, u.TextReset)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day21) Part2() string {
|
||||||
|
// @
|
||||||
|
// #####.#.##.##.###
|
||||||
|
// ABCDEFGHI
|
||||||
|
// using the first program, this kills us. if we jump, we land at D and H becomes our new D, so it won't jump again.
|
||||||
|
// but if we waited to jump until we got one more ahead, we'd be ok.
|
||||||
|
// so now we want to know essentially the same thing as part 1, but also if our multiple (immediate second jump) would be successful.
|
||||||
|
// in problem terms, that's: if there's a hole at 1 or 2 ahead, and there's a hole at C with ground at H, and there's ground at D.
|
||||||
|
// so now for the above example we'd wait to jump until here:
|
||||||
|
// @
|
||||||
|
// #####.#.##.##.###
|
||||||
|
// ABCDEFGHI
|
||||||
|
// and all will be well.
|
||||||
|
cmds := []string{
|
||||||
|
// check if a hole at 1 or 2 ahead
|
||||||
|
"NOT A J",
|
||||||
|
"NOT B T",
|
||||||
|
// store that result in J
|
||||||
|
"OR T J",
|
||||||
|
// check if a hole at 3 ahead...
|
||||||
|
"NOT C T",
|
||||||
|
// and ground at 8 ahead (so we can immediately jump again if needed)...
|
||||||
|
"AND H T",
|
||||||
|
// combine those into J
|
||||||
|
"OR T J",
|
||||||
|
// and ensure we also still have a place to land if we jumped right away
|
||||||
|
"AND D J",
|
||||||
|
"RUN",
|
||||||
|
}
|
||||||
|
instructionStr := strings.Join(cmds, "\n") + "\n"
|
||||||
|
d.program.FeedInputString(instructionStr)
|
||||||
|
|
||||||
|
res := d.program.Run()
|
||||||
|
|
||||||
|
return fmt.Sprintf("Hull damage value when running: %s%d%s", u.TextBold, res, u.TextReset)
|
||||||
|
}
|
155
days/22.go
Normal file
155
days/22.go
Normal file
@ -0,0 +1,155 @@
|
|||||||
|
package days
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"math/big"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
u "parnic.com/aoc2019/utilities"
|
||||||
|
)
|
||||||
|
|
||||||
|
type day22Instruction int
|
||||||
|
|
||||||
|
const (
|
||||||
|
day22InstructionNewStack day22Instruction = iota
|
||||||
|
day22InstructionCut
|
||||||
|
day22InstructionDealIncrement
|
||||||
|
)
|
||||||
|
|
||||||
|
type day22Shuffle struct {
|
||||||
|
instruction day22Instruction
|
||||||
|
arg int
|
||||||
|
}
|
||||||
|
|
||||||
|
type Day22 struct {
|
||||||
|
shuffles []day22Shuffle
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day22) Parse() {
|
||||||
|
lines := u.GetStringLines("22p")
|
||||||
|
d.shuffles = make([]day22Shuffle, len(lines))
|
||||||
|
|
||||||
|
for idx, line := range lines {
|
||||||
|
split := strings.Split(line, " ")
|
||||||
|
if split[0] == "deal" {
|
||||||
|
if split[1] == "into" {
|
||||||
|
d.shuffles[idx] = day22Shuffle{instruction: day22InstructionNewStack}
|
||||||
|
} else if split[1] == "with" {
|
||||||
|
arg, err := strconv.Atoi(split[3])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
d.shuffles[idx] = day22Shuffle{
|
||||||
|
instruction: day22InstructionDealIncrement,
|
||||||
|
arg: arg,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if split[0] == "cut" {
|
||||||
|
arg, err := strconv.Atoi(split[1])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
d.shuffles[idx] = day22Shuffle{
|
||||||
|
instruction: day22InstructionCut,
|
||||||
|
arg: arg,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day22) Num() int {
|
||||||
|
return 22
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day22) applyShuffle(s day22Shuffle, stack, scratch []int) {
|
||||||
|
switch s.instruction {
|
||||||
|
case day22InstructionNewStack:
|
||||||
|
for i := 0; i < len(stack)/2; i++ {
|
||||||
|
stack[i], stack[len(stack)-1-i] = stack[len(stack)-1-i], stack[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
// there's probably a way to do these two in place...
|
||||||
|
case day22InstructionCut:
|
||||||
|
absArg := int(math.Abs(float64(s.arg)))
|
||||||
|
for i, v := range stack {
|
||||||
|
if s.arg > 0 {
|
||||||
|
if i < absArg {
|
||||||
|
scratch[len(scratch)-absArg+i] = v
|
||||||
|
} else {
|
||||||
|
scratch[i-absArg] = v
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if i < absArg {
|
||||||
|
scratch[i] = stack[len(stack)-absArg+i]
|
||||||
|
} else {
|
||||||
|
scratch[i] = stack[i-absArg]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
copy(stack, scratch)
|
||||||
|
|
||||||
|
case day22InstructionDealIncrement:
|
||||||
|
for i, v := range stack {
|
||||||
|
scratch[(i*s.arg)%len(stack)] = v
|
||||||
|
}
|
||||||
|
copy(stack, scratch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day22) Part1() string {
|
||||||
|
deckSize := 10007
|
||||||
|
// deckSize := 10
|
||||||
|
|
||||||
|
stack := make([]int, deckSize)
|
||||||
|
for i := range stack {
|
||||||
|
stack[i] = i
|
||||||
|
}
|
||||||
|
|
||||||
|
scratch := make([]int, len(stack))
|
||||||
|
|
||||||
|
for _, s := range d.shuffles {
|
||||||
|
d.applyShuffle(s, stack, scratch)
|
||||||
|
}
|
||||||
|
|
||||||
|
pos := -1
|
||||||
|
for i, v := range stack {
|
||||||
|
if v == 2019 {
|
||||||
|
pos = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("Card 2019 is at position %s%d%s", u.TextBold, pos, u.TextReset)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day22) Part2() string {
|
||||||
|
n, iter := big.NewInt(119315717514047), big.NewInt(101741582076661)
|
||||||
|
offset, increment := big.NewInt(0), big.NewInt(1)
|
||||||
|
for _, s := range d.shuffles {
|
||||||
|
switch s.instruction {
|
||||||
|
case day22InstructionNewStack:
|
||||||
|
increment.Mul(increment, big.NewInt(-1))
|
||||||
|
offset.Add(offset, increment)
|
||||||
|
case day22InstructionCut:
|
||||||
|
offset.Add(offset, big.NewInt(0).Mul(big.NewInt(int64(s.arg)), increment))
|
||||||
|
case day22InstructionDealIncrement:
|
||||||
|
increment.Mul(increment, big.NewInt(0).Exp(big.NewInt(int64(s.arg)), big.NewInt(0).Sub(n, big.NewInt(2)), n))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
finalIncr := big.NewInt(0).Exp(increment, iter, n)
|
||||||
|
|
||||||
|
finalOffs := big.NewInt(0).Exp(increment, iter, n)
|
||||||
|
finalOffs.Sub(big.NewInt(1), finalOffs)
|
||||||
|
invmod := big.NewInt(0).Exp(big.NewInt(0).Sub(big.NewInt(1), increment), big.NewInt(0).Sub(n, big.NewInt(2)), n)
|
||||||
|
finalOffs.Mul(finalOffs, invmod)
|
||||||
|
finalOffs.Mul(finalOffs, offset)
|
||||||
|
|
||||||
|
answer := big.NewInt(0).Mul(big.NewInt(2020), finalIncr)
|
||||||
|
answer.Add(answer, finalOffs)
|
||||||
|
answer.Mod(answer, n)
|
||||||
|
|
||||||
|
return fmt.Sprintf("Card at position 2020: %s%d%s", u.TextBold, answer, u.TextReset)
|
||||||
|
}
|
170
days/23.go
Normal file
170
days/23.go
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
package days
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
u "parnic.com/aoc2019/utilities"
|
||||||
|
)
|
||||||
|
|
||||||
|
type day23Computer struct {
|
||||||
|
program u.IntcodeProgram
|
||||||
|
id int
|
||||||
|
packetQueue chan u.Vec2[int64]
|
||||||
|
outputStep int
|
||||||
|
nextPacketDest int
|
||||||
|
sendingPacket u.Vec2[int64]
|
||||||
|
hasQueuedPacket bool
|
||||||
|
lastReceivedPacket u.Vec2[int64]
|
||||||
|
idle bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day23) makeComputer(id int) *day23Computer {
|
||||||
|
c := &day23Computer{
|
||||||
|
program: d.program.Copy(),
|
||||||
|
id: id,
|
||||||
|
packetQueue: make(chan u.Vec2[int64]),
|
||||||
|
idle: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
type Day23 struct {
|
||||||
|
program u.IntcodeProgram
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day23) Parse() {
|
||||||
|
d.program = u.LoadIntcodeProgram("23p")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day23) Num() int {
|
||||||
|
return 23
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day23) initComputers() []*day23Computer {
|
||||||
|
computers := make([]*day23Computer, 50)
|
||||||
|
for i := range computers {
|
||||||
|
computers[i] = d.makeComputer(i)
|
||||||
|
}
|
||||||
|
return computers
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day23) execComputers(computers []*day23Computer, nat chan u.Vec2[int64]) *sync.WaitGroup {
|
||||||
|
wg := &sync.WaitGroup{}
|
||||||
|
wg.Add(len(computers))
|
||||||
|
for _, c := range computers {
|
||||||
|
go func(c *day23Computer) {
|
||||||
|
bootedUp := false
|
||||||
|
c.program.RunIn(func(inputStep int) int64 {
|
||||||
|
if !bootedUp {
|
||||||
|
bootedUp = true
|
||||||
|
return int64(c.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.hasQueuedPacket {
|
||||||
|
// fmt.Printf(" %d finished processing packet %v\n", c.id, c.lastReceivedPacket)
|
||||||
|
c.hasQueuedPacket = false
|
||||||
|
return c.lastReceivedPacket.Y
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case c.lastReceivedPacket = <-c.packetQueue:
|
||||||
|
// fmt.Printf("computer %d received packet %v\n", c.id, packet)
|
||||||
|
c.hasQueuedPacket = true
|
||||||
|
return c.lastReceivedPacket.X
|
||||||
|
default:
|
||||||
|
c.idle = true
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
}, func(val int64, state u.IntcodeProgramState) {
|
||||||
|
c.idle = false
|
||||||
|
switch c.outputStep {
|
||||||
|
case 0:
|
||||||
|
c.nextPacketDest = int(val)
|
||||||
|
case 1:
|
||||||
|
c.sendingPacket.X = val
|
||||||
|
case 2:
|
||||||
|
c.sendingPacket.Y = val
|
||||||
|
|
||||||
|
if c.nextPacketDest == 255 {
|
||||||
|
// fmt.Printf("computer %d sending %v to 255\n", c.id, c.sendingPacket)
|
||||||
|
nat <- c.sendingPacket
|
||||||
|
} else {
|
||||||
|
// fmt.Printf("computer %d sending %v to computer %d\n", c.id, c.sendingPacket, c.nextPacketDest)
|
||||||
|
computers[c.nextPacketDest].packetQueue <- c.sendingPacket
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.outputStep = (c.outputStep + 1) % 3
|
||||||
|
})
|
||||||
|
|
||||||
|
wg.Done()
|
||||||
|
}(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
return wg
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day23) Part1() string {
|
||||||
|
computers := d.initComputers()
|
||||||
|
natChan := make(chan u.Vec2[int64])
|
||||||
|
wg := d.execComputers(computers, natChan)
|
||||||
|
|
||||||
|
answer := <-natChan
|
||||||
|
for _, c := range computers {
|
||||||
|
c.program.Stop()
|
||||||
|
}
|
||||||
|
// not really necessary, but let's make sure they all shut down in case
|
||||||
|
// we're running all days at once
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return fmt.Sprintf("First packet sent to 255 Y value: %s%d%s", u.TextBold, answer.Y, u.TextReset)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day23) Part2() string {
|
||||||
|
computers := d.initComputers()
|
||||||
|
natChan := make(chan u.Vec2[int64])
|
||||||
|
wg := d.execComputers(computers, natChan)
|
||||||
|
|
||||||
|
answerChan := make(chan int64)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
var currVal u.Vec2[int64]
|
||||||
|
var lastVal u.Vec2[int64]
|
||||||
|
hasReceived := false
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case currVal = <-natChan:
|
||||||
|
hasReceived = true
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
allIdle := true
|
||||||
|
for _, c := range computers {
|
||||||
|
if !c.idle {
|
||||||
|
allIdle = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if allIdle && hasReceived {
|
||||||
|
// fmt.Printf("all idle, sending %v to computer 0\n", currVal)
|
||||||
|
if lastVal.Y == currVal.Y {
|
||||||
|
// fmt.Printf("found answer? %d\n", currVal.Y)
|
||||||
|
answerChan <- currVal.Y
|
||||||
|
}
|
||||||
|
computers[0].packetQueue <- currVal
|
||||||
|
lastVal = currVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
answer := <-answerChan
|
||||||
|
for _, c := range computers {
|
||||||
|
c.program.Stop()
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
return fmt.Sprintf("First Y value sent to the NAT twice in a row: %s%d%s", u.TextBold, answer, u.TextReset)
|
||||||
|
}
|
289
days/24.go
Normal file
289
days/24.go
Normal file
@ -0,0 +1,289 @@
|
|||||||
|
package days
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
|
||||||
|
u "parnic.com/aoc2019/utilities"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
day24AdjacentOffsets = []u.Vec2i{
|
||||||
|
{X: -1, Y: 0},
|
||||||
|
{X: 1, Y: 0},
|
||||||
|
{X: 0, Y: -1},
|
||||||
|
{X: 0, Y: 1},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type Day24 struct {
|
||||||
|
grid [][]bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day24) Parse() {
|
||||||
|
lines := u.GetStringLines("24p")
|
||||||
|
d.grid = make([][]bool, len(lines))
|
||||||
|
for i, line := range lines {
|
||||||
|
d.grid[i] = make([]bool, len(line))
|
||||||
|
for j, ch := range line {
|
||||||
|
d.grid[i][j] = ch == '#'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day24) Num() int {
|
||||||
|
return 24
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day24) calcActivatedNeighbors(grid [][]bool, i, j int) int {
|
||||||
|
activatedNeighbors := 0
|
||||||
|
for _, o := range day24AdjacentOffsets {
|
||||||
|
newI := i + o.X
|
||||||
|
newJ := j + o.Y
|
||||||
|
if newI < 0 || newI >= len(grid) || newJ < 0 || newJ >= len(grid[i]) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if grid[newI][newJ] {
|
||||||
|
activatedNeighbors++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return activatedNeighbors
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day24) recursiveCalcActivatedNeighbors(gridMap map[int][][]bool, mapIdx, i, j int) int {
|
||||||
|
activatedNeighbors := 0
|
||||||
|
numNeighbors := 0
|
||||||
|
thisGrid := gridMap[mapIdx]
|
||||||
|
for _, o := range day24AdjacentOffsets {
|
||||||
|
newI := i + o.X
|
||||||
|
newJ := j + o.Y
|
||||||
|
if newI < 0 || newI >= len(thisGrid) || newJ < 0 || newJ >= len(thisGrid[i]) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if newI == 2 && newJ == 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
numNeighbors++
|
||||||
|
if thisGrid[newI][newJ] {
|
||||||
|
activatedNeighbors++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checkLower := (i == 1 && j == 2) ||
|
||||||
|
(i == 2 && (j == 1 || j == 3)) ||
|
||||||
|
(i == 3 && j == 2)
|
||||||
|
if checkLower {
|
||||||
|
if lowerGrid, exists := gridMap[mapIdx+1]; exists {
|
||||||
|
if i == 1 {
|
||||||
|
for _, b := range lowerGrid[0] {
|
||||||
|
numNeighbors++
|
||||||
|
if b {
|
||||||
|
activatedNeighbors++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if i == 2 {
|
||||||
|
if j == 1 {
|
||||||
|
for _, r := range lowerGrid {
|
||||||
|
numNeighbors++
|
||||||
|
if r[0] {
|
||||||
|
activatedNeighbors++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if j == 3 {
|
||||||
|
for _, r := range lowerGrid {
|
||||||
|
numNeighbors++
|
||||||
|
if r[len(lowerGrid[0])-1] {
|
||||||
|
activatedNeighbors++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if i == 3 {
|
||||||
|
for _, b := range lowerGrid[len(lowerGrid)-1] {
|
||||||
|
numNeighbors++
|
||||||
|
if b {
|
||||||
|
activatedNeighbors++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
checkUpper := (i == 0) || (i == len(thisGrid)-1) ||
|
||||||
|
((i != 0 && i != len(thisGrid)) && (j == 0 || j == len(thisGrid[0])-1))
|
||||||
|
if checkUpper {
|
||||||
|
if upperGrid, exists := gridMap[mapIdx-1]; exists {
|
||||||
|
if i == 0 {
|
||||||
|
numNeighbors++
|
||||||
|
if upperGrid[1][2] {
|
||||||
|
activatedNeighbors++
|
||||||
|
}
|
||||||
|
} else if i == len(thisGrid)-1 {
|
||||||
|
numNeighbors++
|
||||||
|
if upperGrid[3][2] {
|
||||||
|
activatedNeighbors++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if j == 0 {
|
||||||
|
numNeighbors++
|
||||||
|
if upperGrid[2][1] {
|
||||||
|
activatedNeighbors++
|
||||||
|
}
|
||||||
|
} else if j == len(thisGrid[0])-1 {
|
||||||
|
numNeighbors++
|
||||||
|
if upperGrid[2][3] {
|
||||||
|
activatedNeighbors++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return activatedNeighbors
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day24) calcRating(grid [][]bool) int {
|
||||||
|
rating := 0
|
||||||
|
for i, r := range grid {
|
||||||
|
for j := range r {
|
||||||
|
pow := (i * len(r)) + j
|
||||||
|
if grid[i][j] {
|
||||||
|
result := int(math.Pow(2, float64(pow)))
|
||||||
|
rating += result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rating
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day24) numBugs(gridMap map[int][][]bool) int {
|
||||||
|
ret := 0
|
||||||
|
for _, v := range gridMap {
|
||||||
|
for _, r := range v {
|
||||||
|
for _, b := range r {
|
||||||
|
if b {
|
||||||
|
ret++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func copy2d[T comparable](dest [][]T, src [][]T) {
|
||||||
|
for i, r := range src {
|
||||||
|
copy(dest[i], r)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Day24) Draw(grid [][]bool) {
|
||||||
|
for _, r := range grid {
|
||||||
|
for _, c := range r {
|
||||||
|
if c {
|
||||||
|
fmt.Print("#")
|
||||||
|
} else {
|
||||||
|
fmt.Print(".")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day24) Part1() string {
|
||||||
|
grid := make([][]bool, len(d.grid))
|
||||||
|
scratch := make([][]bool, len(grid))
|
||||||
|
for i, g := range d.grid {
|
||||||
|
grid[i] = make([]bool, len(g))
|
||||||
|
scratch[i] = make([]bool, len(g))
|
||||||
|
copy(grid[i], d.grid[i])
|
||||||
|
}
|
||||||
|
|
||||||
|
found := false
|
||||||
|
answer := 0
|
||||||
|
seenRatings := make([]int, 0)
|
||||||
|
for i := 1; !found; i++ {
|
||||||
|
// d.Draw(grid)
|
||||||
|
for i, r := range grid {
|
||||||
|
for j := range r {
|
||||||
|
numActivated := d.calcActivatedNeighbors(grid, i, j)
|
||||||
|
if grid[i][j] {
|
||||||
|
scratch[i][j] = numActivated == 1
|
||||||
|
} else {
|
||||||
|
scratch[i][j] = numActivated == 1 || numActivated == 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rating := d.calcRating(scratch)
|
||||||
|
if u.ArrayContains(seenRatings, rating) {
|
||||||
|
found = true
|
||||||
|
// d.Draw(scratch)
|
||||||
|
answer = rating
|
||||||
|
}
|
||||||
|
seenRatings = append(seenRatings, rating)
|
||||||
|
copy2d(grid, scratch)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("First repeated biodiversity rating is %s%d%s", u.TextBold, answer, u.TextReset)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Day24) Part2() string {
|
||||||
|
makeGrid := func(initialGrid [][]bool) ([][]bool, [][]bool) {
|
||||||
|
grid := make([][]bool, len(d.grid))
|
||||||
|
scratch := make([][]bool, len(grid))
|
||||||
|
for i, g := range d.grid {
|
||||||
|
grid[i] = make([]bool, len(g))
|
||||||
|
scratch[i] = make([]bool, len(g))
|
||||||
|
if initialGrid != nil {
|
||||||
|
copy(grid[i], initialGrid[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return grid, scratch
|
||||||
|
}
|
||||||
|
|
||||||
|
gridMap := make(map[int][][]bool)
|
||||||
|
scratchMap := make(map[int][][]bool)
|
||||||
|
gridMap[0], scratchMap[0] = makeGrid(d.grid)
|
||||||
|
|
||||||
|
min := 0
|
||||||
|
max := 0
|
||||||
|
|
||||||
|
for i := 0; i < 200; i++ {
|
||||||
|
gridMap[min-1], scratchMap[min-1] = makeGrid(nil)
|
||||||
|
gridMap[max+1], scratchMap[max+1] = makeGrid(nil)
|
||||||
|
min, max = min-1, max+1
|
||||||
|
|
||||||
|
// if i == 10 {
|
||||||
|
// keys := u.MapKeys(gridMap)
|
||||||
|
// sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })
|
||||||
|
// for _, k := range keys {
|
||||||
|
// fmt.Println("Depth", k)
|
||||||
|
// d.Draw(gridMap[k])
|
||||||
|
// }
|
||||||
|
// fmt.Println("# bugs:", d.numBugs(gridMap))
|
||||||
|
// }
|
||||||
|
|
||||||
|
for depth, grid := range gridMap {
|
||||||
|
for i, r := range grid {
|
||||||
|
for j := range r {
|
||||||
|
if i == 2 && j == 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
numActivated := d.recursiveCalcActivatedNeighbors(gridMap, depth, i, j)
|
||||||
|
if grid[i][j] {
|
||||||
|
scratchMap[depth][i][j] = numActivated == 1
|
||||||
|
} else {
|
||||||
|
scratchMap[depth][i][j] = numActivated == 1 || numActivated == 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for d := range gridMap {
|
||||||
|
copy2d(gridMap[d], scratchMap[d])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Sprintf("Bugs present after 200 minutes: %s%d%s", u.TextBold, d.numBugs(gridMap), u.TextReset)
|
||||||
|
}
|
113
inputs/20p.txt
Normal file
113
inputs/20p.txt
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
G R U E Z S S
|
||||||
|
W N C K Z G D
|
||||||
|
#################################.###########.###.#######.#########.#.#######.#####################################
|
||||||
|
#.#.......#.#.........#.#.#.....#.#.....#.#.#...#.....#.........#.....#...#.#...........#.......#.....#.....#.#...#
|
||||||
|
#.#####.###.###.###.#.#.#.###.###.#.###.#.#.###.#.###.###.#####.#.###.###.#.###.#########.###.###.#######.###.#.###
|
||||||
|
#.#.#.#...#...#.#.#.#.................#.#...#...#.#.#...#.#.....#.#.#.#.....................#.#.......#.........#.#
|
||||||
|
#.#.#.#.###.#####.#########.###########.#.#.#.###.#.#######.###.#.#.#####.###.#.###.#.###.#.###.#######.#######.#.#
|
||||||
|
#...#...#.#.#.#.........#...#...#.......#.#...#.......#.....#.#.#.....#.....#.#...#.#.#...#.#...#.#.........#.#.#.#
|
||||||
|
#.#.###.#.#.#.#####.#.#.#######.#.#######.###.#.###.#####.###.#####.#.###.#########.#.#.#######.#.#####.#####.#.#.#
|
||||||
|
#.#...#.#.#.#.#.....#.#.................#.#...#.#.#.....#...#.#.#...#...#.#.....#.#.#.#...#.#.#...........#.#.....#
|
||||||
|
###.###.#.#.#.#.#.#####.#.###.#########.#.#######.#.#####.###.#.#.#.#####.#.#.###.#.#######.#.#.###.#######.###.#.#
|
||||||
|
#...#.......#...#.#.#.#.#...#.#.........#.....#.......#.#.......#.#...#.....#.....#.#...#.........#...#.#.#.#.#.#.#
|
||||||
|
###.#######.#######.#.###########.#.#####.###.#####.###.#.#####.#.###.#.#.###.#.#######.###.#####.#####.#.#.#.#####
|
||||||
|
#.......#.#...#.......#.........#.#.#...#.#.#.#.....#.....#...#.#...#.#.#.#...#...#...#...#...#.#.#...........#.#.#
|
||||||
|
#.#.###.#.#.#########.#########.#.###.###.#.#####.#.###.#.#.#.#.#.###########.#.###.###.###.###.###.###.#.#####.#.#
|
||||||
|
#.#...#.....#.#...#.#.#.#.#.............#.....#...#...#.#.#.#...#...#.#.#.....#.........#...#.......#...#...#...#.#
|
||||||
|
###########.#.#.###.#.#.#.#######.#####.#.#####.#.#######.#####.###.#.#.#####.#.#.###.#####.###.#############.#.#.#
|
||||||
|
#.#.#.#.#.....#.#.....#.#.#.#.....#...#.#.....#.#.....#...#.#.#...#...#.#.#...#.#...#...#...#...#...#.#.......#.#.#
|
||||||
|
#.#.#.#.###.###.#####.#.#.#.#####.#.###.#.#########.#######.#.#.###.###.#.#.###.###.#####.#####.#.###.#####.#####.#
|
||||||
|
#.#...#.#...#.#.#...#...#.#.....#.#.....#.#...#.....#.......#.#.#...#.....#.#.#.#.......#.......#...#.#.#...#.#...#
|
||||||
|
#.#.#.#.###.#.#.###.#.###.#.#####.#.###.#.###.###.###.#.#.###.#.#.###.#.#.###.#.###.#####.#.#####.###.#.###.#.###.#
|
||||||
|
#.#.#.#.#...#.#...#...#.#.#...#...#...#.#.#.....#.#...#.#.#.....#.#...#.#.#.......#.......#...#.#.....#.........#.#
|
||||||
|
#.#.###.###.#.#.#####.#.#.#.###.#.#####.#.#.###.#.###.#.###.#.###.#.###.#####.#####.#####.#####.###.#########.#.#.#
|
||||||
|
#.....#...#.......#.#.#.......#.#.#...#.#...#.#.#.....#...#.#...#...#.#...#.......#...#.#...#...#...#.#.#.#.#.#.#.#
|
||||||
|
###.###.#######.###.#.#.#.#.#########.#.#####.#.#.#####.#######.#####.#.#####.#.#.###.#.#######.###.#.#.#.#.#.###.#
|
||||||
|
#.....#...#.#.#.......#.#.#.#.#.......#...#.....#.#.....#.......#.......#.#...#.#.#.#...#.....#.#.....#.#.#...#.#.#
|
||||||
|
###.###.###.#.#######.#.#####.#####.#.#.###.#########.#.###.#.#####.#####.###.#####.#######.###.###.###.#.#.###.#.#
|
||||||
|
#.....#.......#...#...#...#.#.#.#.#.#.#.#.......#.#...#.#.#.#...#.......#...#.......#...#.....#...#.#.........#...#
|
||||||
|
#.#####.#######.###.#.#.###.#.#.#.#.#.#.###.#.###.#.#####.#####.#####.#####.###.#######.###.###.###.#########.#.###
|
||||||
|
#...#...#...#.#.#...#.#.#.#.........#.....#.#.....#.......#.......#...#.............#...#...#.#.........#...#.#...#
|
||||||
|
#.###.###.###.#.#####.#.#.#.#####.#############.#######.#####.#####.#######.###########.###.#.###.#######.###.#.###
|
||||||
|
#...#...#...#...#.#...#...#.# S L B V G R #.#.#.#.....#.#.......#.#...#
|
||||||
|
#.###.#.#.#####.#.###.#.###.# D M E H X Q #.#.#.#####.#.###.#####.#.###
|
||||||
|
#.....#.#.................#.# #.........................#.#
|
||||||
|
#.###.###.#####.#.#.#.###.### #.#.#.###.#######.###.#.#.#.#
|
||||||
|
#.#.#.....#.#...#.#.#.#.....# RZ..#.#...#.....#.#...#.#.#.#..BE
|
||||||
|
#.#.#####.#.#####.#.#####.### #######.#####.#.#.#######.#.#
|
||||||
|
#.....#.#.....#.#.#.....#.#.# #.#.#.#.#.#...#.......#...#.#
|
||||||
|
###.###.###.#.#.#.#.###.#.#.# #.#.#.###.#####.#.#######.#.#
|
||||||
|
RI....#.....#.#.#...#.#.#.#....UW #.#.......#...#.#.#.#...#...#
|
||||||
|
#####.###############.###.### #.###.#####.#.#####.#.#.#####
|
||||||
|
#.....#.#.#...#.#.....#.#...# GW..........#.#.........#.#...#
|
||||||
|
#.###.#.#.#.###.###.###.##### #.###.#.#.###.#######.###.###
|
||||||
|
#...#...#.....#.#.......#....FM #.#...#.#.....#...#.........#
|
||||||
|
###.###.#.#.#.#.#.#########.# ###.#######.###.###########.#
|
||||||
|
AJ..#...#...#.#...#...#.....#.# #...#.#.#...#...#.#...#.#.#..GX
|
||||||
|
#.#.#########.#.#.#######.#.# #####.#.#######.#.###.#.#.###
|
||||||
|
#.....#.#.#.#.#.............# #...............#...#........DD
|
||||||
|
#######.#.#.###########.###.# ###.#######.###.#.#.#.#.###.#
|
||||||
|
#.....................#.#...# #.......#.....#...#.#.#.#...#
|
||||||
|
###.###.#.#.###.#####.####### #####.#########.###.#.###.###
|
||||||
|
GP..#...#.#.#.#.....#.#...#...# SG..#...#.....#...#.#.....#...#
|
||||||
|
#.###.#####.#.#.#.#.###.###.# #.#.###.#.#######.#######.###
|
||||||
|
#.#.....#.#.#.#.#.#.....#...# #.....#.#.#.#...#.#.#...#.#.#
|
||||||
|
#.#.#####.#########.#######.# #########.#.###.#.#.#.#####.#
|
||||||
|
#.....#...#..................SI DD....#........................UW
|
||||||
|
#.#######.#####.###.###.##### #.###.#####.#.###.#.###.#####
|
||||||
|
#.#.#...#...#.....#.#.#.#.#..AJ #.........#.#.#.#.#.#.....#.#
|
||||||
|
###.#.###.#.#########.###.#.# #.#####.###.#.#.#######.###.#
|
||||||
|
#.........#.#...#.....#.#.#.# #...#.....#.#...#.#.#.#.#....LM
|
||||||
|
#########.#.###.#.###.#.#.#.# #########.#.#.###.#.#.#####.#
|
||||||
|
AA......#.#.#.....#.#...#.....# #.#.#.#.#.#.#.#...#.#.#.....#
|
||||||
|
#.#####.#.#.###.#.#.###.#.### #.#.#.#.#########.#.#.#####.#
|
||||||
|
JO..........#...#...#.....#...# #.#.......#.#.............#.#
|
||||||
|
#####.###############.###.#.# #.#.#.#####.###.###.#.###.#.#
|
||||||
|
#.#.#...#.........#.#.#.#.#.# TX....#.............#.#...#...#
|
||||||
|
#.#.#######.#.#.#.#.###.###.# #.###.#.###.#####.###########
|
||||||
|
VH......#.....#.#.#.....#...#.# #.#...#.#...#...#.#.....#...#
|
||||||
|
#####.#.###########.###.##### #############.#####.###.#.#.#
|
||||||
|
#.....#.....#.#.......#.....# #.#...#...#.#.......#.#.#.#..RZ
|
||||||
|
#.#.#######.#.#######.#.##### #.###.###.#.###.#.###.#.#.###
|
||||||
|
#.#...........#.#............FW RN..#.....#...#.#.#.#.....#...#
|
||||||
|
#########.###.#.#.########### #.#.#.###.###.###.#.#####.###
|
||||||
|
#...#...#.#.....#.#.........# #...#.............#.......#.#
|
||||||
|
#.#.#.#################.#.#.# ###############.###########.#
|
||||||
|
AX..#...#.#...#.#...#...#.#.#..HR JO............#.#.#...........#
|
||||||
|
#.#.###.###.#.#.#####.###.#.# #.#.#####.#.#.###.#.#.#####.#
|
||||||
|
TX..#...#.......#...#.....#.#..EK #.#...#.#.#.......#.#...#.#..MM
|
||||||
|
###.#.###.#.#.#.#.###.#.#.### #.###.#.#######.#.###.###.#.#
|
||||||
|
#...#.....#.#...#.....#.....# #.#.#.....#.#...#...#.#.....#
|
||||||
|
#####.#.#.#####.###.#######.# R Y G U A M S #.#.#.#.###.#####.#.#.###.#.#
|
||||||
|
#.....#.#...#.....#.....#...# I Q P C X M Y #...#.#.#.....#...#.#...#.#.#
|
||||||
|
#####.#########.###.#####.###########.#####.###.###########.#####.###########.#.#######.#.###.#.#########.#.#####.#
|
||||||
|
#...#...#.#.......#.....#...#...#.....#.....#.....#.#.....#...#...........#...#...#...#.#.#.....#.#.......#.#.....#
|
||||||
|
#.#.#.###.###.#.#.#######.#####.###.###.###.#####.#.###.#.#.#########.#####.#####.#.#########.###.###.#####.###.#.#
|
||||||
|
#.#.......#...#.#.#.......#.....#.#.#.....#.#.....#...#.#.......#.#.#...#.......#.#.......#.#.#.#.....#.......#.#.#
|
||||||
|
#.#.#.#.#####.###########.#####.#.#.#.#.#.#####.###.###.#########.#.#.#######.###.#.#######.###.###.#.###.#.#.#.#.#
|
||||||
|
#.#.#.#...#.......#.......#.........#.#.#.....#.#.....#.........#.........#.#.#.....#.......#.......#.#.#.#.#.#.#.#
|
||||||
|
#######.#.#.#.#####.#.#########.#############.#.#.###.#######.#####.#.#####.#.#.#######.#####.#.#.###.#.#.#######.#
|
||||||
|
#.......#.#.#...#...#.#.........#.......#...#.#.....#.#.......#.....#...#.....#...........#.#.#.#.#.....#...#.....#
|
||||||
|
#.#.#.#####.#.#.#####.#.#######.#.###.###.###.#.###.#.#.###.#.#.#.###.#.#####.#.###.#.#.###.###.#.#.#.#####.#####.#
|
||||||
|
#.#.#.#.....#.#.#.....#...#.#.....#...#.......#.#...#.#...#.#.#.#...#.#.#.....#.#...#.#.......#.#.#.#...#.#.#.....#
|
||||||
|
#.###.###.#.###.###.#.#.###.#.#.#.#.###.###.#.#######.###.#.###.#######.#.#.#.#####.#.#.#.#############.#.#####.###
|
||||||
|
#.#.#.#.#.#...#.#...#.#.#.....#.#.#...#...#.#...#.....#...#...#...#.....#.#.#.....#.#.#.#.#.....#.#.#.......#...#.#
|
||||||
|
###.#.#.#.#.###.#####.#############.#.#.#.#.#######.###.###.###.#############.#####.#####.###.###.#.#.#####.###.#.#
|
||||||
|
#.......#.#.#.....#...#.#.......#...#.#.#.#.#.#.#.#...#...#.#.....#.#.#...#.....#.#.#...#...#.......#.....#...#...#
|
||||||
|
###.###.#.###.#########.#######.#####.#.#####.#.#.#.###.#######.###.#.#.#####.###.#.#.###.###.#####.#.#.###.###.###
|
||||||
|
#.....#.#.#.....#.....#.#.......#.#.#.#...#.......#.#.......#...#.....#.......#.........#.#.#.....#.#.#.#...#.....#
|
||||||
|
#.#####.#.###.#.#####.#.#######.#.#.#.#.#######.#.#.###.#####.#.###.#.###.#.#####.###.#####.#.#######.###.#.#.#.###
|
||||||
|
#.#.....#.#...#...#.......#.#.....#...#.#.#.....#...#.......#.#.....#.#...#...#.#...#...#.#.#.....#.#.#.#.#.#.#...#
|
||||||
|
#####.#.#.###.#######.#.###.###.#.#.###.#.#####.#######.#######.#.#####.###.###.###.#####.#.#.#####.###.#######.#.#
|
||||||
|
#.#.#.#.#.#.#.#...#...#...#.#.#.#...#.......#.#.....#.#.#...#...#.....#.#.......#.........#.#...#.#.........#.#.#.#
|
||||||
|
#.#.#######.#####.#####.###.#.#.#########.###.#####.#.#.#.#######.#########.#.###.#########.###.#.#.#########.#.#.#
|
||||||
|
#...#.#.#.#.....#.#...................#...#...#...#...#.....#...#.#...#...#.#.#.....#.#.......#.#.#.#.#.#.#...#.#.#
|
||||||
|
###.#.#.#.#.###.#.#####.#############.###.#.#.#.#.###.#.#.#####.#.#.#####.###.#.###.#.#####.###.#.#.#.#.#.###.#.###
|
||||||
|
#...........#.#.#...#...#.#.......#.#...#...#.#.#...#.#.#...#.#.......#.....#.#...#.#.#.#.#.....#.....#.#.....#...#
|
||||||
|
#####.#.#.#.#.###.#####.#.#.#.###.#.#.#######.#.###.#.#.###.#.#####.###.###.#.#.###.#.#.#.#.#####.###.#.#.#########
|
||||||
|
#.#...#.#.#.............#...#...#...#.....#...#...#...#.#.#.#...#...#...#.#...#.#...................#.............#
|
||||||
|
#.#####.#######.#.###.###.#.#.#####.#.#######.###.#####.#.###.#.#.#####.#.###.#.#.#####.###.#####.###.#.###.#.#####
|
||||||
|
#.......#.......#...#...#.#.#.#.........#.....#.....#.......#.#.......#.....#.#.#.....#...#.....#...#.#...#.#.....#
|
||||||
|
###################################.#######.###.#########.#######.#########.#######.###############################
|
||||||
|
Y S R S F F H
|
||||||
|
Q Y Q I M W R
|
19
inputs/20s1.txt
Normal file
19
inputs/20s1.txt
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
A
|
||||||
|
A
|
||||||
|
#######.#########
|
||||||
|
#######.........#
|
||||||
|
#######.#######.#
|
||||||
|
#######.#######.#
|
||||||
|
#######.#######.#
|
||||||
|
##### B ###.#
|
||||||
|
BC...## C ###.#
|
||||||
|
##.## ###.#
|
||||||
|
##...DE F ###.#
|
||||||
|
##### G ###.#
|
||||||
|
#########.#####.#
|
||||||
|
DE..#######...###.#
|
||||||
|
#.#########.###.#
|
||||||
|
FG..#########.....#
|
||||||
|
###########.#####
|
||||||
|
Z
|
||||||
|
Z
|
37
inputs/20s2.txt
Normal file
37
inputs/20s2.txt
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
A
|
||||||
|
A
|
||||||
|
#################.#############
|
||||||
|
#.#...#...................#.#.#
|
||||||
|
#.#.#.###.###.###.#########.#.#
|
||||||
|
#.#.#.......#...#.....#.#.#...#
|
||||||
|
#.#########.###.#####.#.#.###.#
|
||||||
|
#.............#.#.....#.......#
|
||||||
|
###.###########.###.#####.#.#.#
|
||||||
|
#.....# A C #.#.#.#
|
||||||
|
####### S P #####.#
|
||||||
|
#.#...# #......VT
|
||||||
|
#.#.#.# #.#####
|
||||||
|
#...#.# YN....#.#
|
||||||
|
#.###.# #####.#
|
||||||
|
DI....#.# #.....#
|
||||||
|
#####.# #.###.#
|
||||||
|
ZZ......# QG....#..AS
|
||||||
|
###.### #######
|
||||||
|
JO..#.#.# #.....#
|
||||||
|
#.#.#.# ###.#.#
|
||||||
|
#...#..DI BU....#..LF
|
||||||
|
#####.# #.#####
|
||||||
|
YN......# VT..#....QG
|
||||||
|
#.###.# #.###.#
|
||||||
|
#.#...# #.....#
|
||||||
|
###.### J L J #.#.###
|
||||||
|
#.....# O F P #.#...#
|
||||||
|
#.###.#####.#.#####.#####.###.#
|
||||||
|
#...#.#.#...#.....#.....#.#...#
|
||||||
|
#.#####.###.###.#.#.#########.#
|
||||||
|
#...#.#.....#...#.#.#.#.....#.#
|
||||||
|
#.###.#####.###.###.#.#.#######
|
||||||
|
#.#.........#...#.............#
|
||||||
|
#########.###.###.#############
|
||||||
|
B J C
|
||||||
|
U P P
|
37
inputs/20s3.txt
Normal file
37
inputs/20s3.txt
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
Z L X W C
|
||||||
|
Z P Q B K
|
||||||
|
###########.#.#.#.#######.###############
|
||||||
|
#...#.......#.#.......#.#.......#.#.#...#
|
||||||
|
###.#.#.#.#.#.#.#.###.#.#.#######.#.#.###
|
||||||
|
#.#...#.#.#...#.#.#...#...#...#.#.......#
|
||||||
|
#.###.#######.###.###.#.###.###.#.#######
|
||||||
|
#...#.......#.#...#...#.............#...#
|
||||||
|
#.#########.#######.#.#######.#######.###
|
||||||
|
#...#.# F R I Z #.#.#.#
|
||||||
|
#.###.# D E C H #.#.#.#
|
||||||
|
#.#...# #...#.#
|
||||||
|
#.###.# #.###.#
|
||||||
|
#.#....OA WB..#.#..ZH
|
||||||
|
#.###.# #.#.#.#
|
||||||
|
CJ......# #.....#
|
||||||
|
####### #######
|
||||||
|
#.#....CK #......IC
|
||||||
|
#.###.# #.###.#
|
||||||
|
#.....# #...#.#
|
||||||
|
###.### #.#.#.#
|
||||||
|
XF....#.# RF..#.#.#
|
||||||
|
#####.# #######
|
||||||
|
#......CJ NM..#...#
|
||||||
|
###.#.# #.###.#
|
||||||
|
RE....#.# #......RF
|
||||||
|
###.### X X L #.#.#.#
|
||||||
|
#.....# F Q P #.#.#.#
|
||||||
|
###.###########.###.#######.#########.###
|
||||||
|
#.....#...#.....#.......#...#.....#.#...#
|
||||||
|
#####.#.###.#######.#######.###.###.#.#.#
|
||||||
|
#.......#.......#.#.#.#.#...#...#...#.#.#
|
||||||
|
#####.###.#####.#.#.#.#.###.###.#.###.###
|
||||||
|
#.......#.....#.#...#...............#...#
|
||||||
|
#############.#.#.###.###################
|
||||||
|
A O F N
|
||||||
|
A A D M
|
1
inputs/21p.txt
Normal file
1
inputs/21p.txt
Normal file
File diff suppressed because one or more lines are too long
100
inputs/22p.txt
Normal file
100
inputs/22p.txt
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
deal into new stack
|
||||||
|
cut -2732
|
||||||
|
deal into new stack
|
||||||
|
deal with increment 57
|
||||||
|
cut 5974
|
||||||
|
deal into new stack
|
||||||
|
deal with increment 32
|
||||||
|
cut -1725
|
||||||
|
deal with increment 24
|
||||||
|
cut 6093
|
||||||
|
deal with increment 6
|
||||||
|
cut -2842
|
||||||
|
deal with increment 14
|
||||||
|
cut 2609
|
||||||
|
deal with increment 12
|
||||||
|
cut -6860
|
||||||
|
deal with increment 51
|
||||||
|
cut -6230
|
||||||
|
deal with increment 61
|
||||||
|
cut 3152
|
||||||
|
deal with increment 28
|
||||||
|
cut 2202
|
||||||
|
deal into new stack
|
||||||
|
deal with increment 60
|
||||||
|
cut 433
|
||||||
|
deal into new stack
|
||||||
|
cut -6256
|
||||||
|
deal with increment 13
|
||||||
|
deal into new stack
|
||||||
|
cut 8379
|
||||||
|
deal into new stack
|
||||||
|
deal with increment 54
|
||||||
|
cut 1120
|
||||||
|
deal with increment 16
|
||||||
|
cut -5214
|
||||||
|
deal with increment 63
|
||||||
|
deal into new stack
|
||||||
|
cut -8473
|
||||||
|
deal with increment 11
|
||||||
|
cut 228
|
||||||
|
deal with increment 45
|
||||||
|
cut -6755
|
||||||
|
deal with increment 50
|
||||||
|
cut -3391
|
||||||
|
deal with increment 44
|
||||||
|
cut -1341
|
||||||
|
deal with increment 28
|
||||||
|
cut -6788
|
||||||
|
deal with increment 52
|
||||||
|
cut 3062
|
||||||
|
deal with increment 41
|
||||||
|
cut 4541
|
||||||
|
deal with increment 57
|
||||||
|
cut -7962
|
||||||
|
deal with increment 56
|
||||||
|
cut 9621
|
||||||
|
deal with increment 57
|
||||||
|
cut 3881
|
||||||
|
deal with increment 36
|
||||||
|
deal into new stack
|
||||||
|
deal with increment 45
|
||||||
|
cut 522
|
||||||
|
deal with increment 9
|
||||||
|
deal into new stack
|
||||||
|
deal with increment 60
|
||||||
|
deal into new stack
|
||||||
|
deal with increment 12
|
||||||
|
cut -9181
|
||||||
|
deal with increment 63
|
||||||
|
deal into new stack
|
||||||
|
deal with increment 14
|
||||||
|
cut -2906
|
||||||
|
deal with increment 10
|
||||||
|
cut 848
|
||||||
|
deal with increment 75
|
||||||
|
cut 798
|
||||||
|
deal with increment 29
|
||||||
|
cut 1412
|
||||||
|
deal with increment 10
|
||||||
|
deal into new stack
|
||||||
|
cut -5295
|
||||||
|
deal into new stack
|
||||||
|
cut 4432
|
||||||
|
deal with increment 72
|
||||||
|
cut -7831
|
||||||
|
deal into new stack
|
||||||
|
cut 6216
|
||||||
|
deal into new stack
|
||||||
|
deal with increment 7
|
||||||
|
cut -1720
|
||||||
|
deal into new stack
|
||||||
|
cut -5465
|
||||||
|
deal with increment 70
|
||||||
|
cut -5173
|
||||||
|
deal with increment 7
|
||||||
|
cut 3874
|
||||||
|
deal with increment 65
|
||||||
|
cut 921
|
||||||
|
deal with increment 8
|
||||||
|
cut -3094
|
3
inputs/22s1.txt
Normal file
3
inputs/22s1.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
deal with increment 7
|
||||||
|
deal into new stack
|
||||||
|
deal into new stack
|
3
inputs/22s2.txt
Normal file
3
inputs/22s2.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
cut 6
|
||||||
|
deal with increment 7
|
||||||
|
deal into new stack
|
3
inputs/22s3.txt
Normal file
3
inputs/22s3.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
deal with increment 7
|
||||||
|
deal with increment 9
|
||||||
|
cut -2
|
10
inputs/22s4.txt
Normal file
10
inputs/22s4.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
deal into new stack
|
||||||
|
cut -2
|
||||||
|
deal with increment 7
|
||||||
|
cut 8
|
||||||
|
cut -4
|
||||||
|
deal with increment 7
|
||||||
|
cut 3
|
||||||
|
deal with increment 9
|
||||||
|
deal with increment 3
|
||||||
|
cut -1
|
1
inputs/23p.txt
Normal file
1
inputs/23p.txt
Normal file
File diff suppressed because one or more lines are too long
5
inputs/24p.txt
Normal file
5
inputs/24p.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
##..#
|
||||||
|
...##
|
||||||
|
.#.##
|
||||||
|
#..#.
|
||||||
|
..#..
|
5
inputs/24s1.txt
Normal file
5
inputs/24s1.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
....#
|
||||||
|
#..#.
|
||||||
|
#..##
|
||||||
|
..#..
|
||||||
|
#....
|
5
inputs/24s2.txt
Normal file
5
inputs/24s2.txt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
.....
|
||||||
|
.....
|
||||||
|
.....
|
||||||
|
#....
|
||||||
|
.#...
|
5
main.go
5
main.go
@ -53,6 +53,11 @@ var dayMap = []day{
|
|||||||
&days.Day17{},
|
&days.Day17{},
|
||||||
&days.Day18{},
|
&days.Day18{},
|
||||||
&days.Day19{},
|
&days.Day19{},
|
||||||
|
&days.Day20{},
|
||||||
|
&days.Day21{},
|
||||||
|
&days.Day22{},
|
||||||
|
&days.Day23{},
|
||||||
|
&days.Day24{},
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -28,6 +28,8 @@ type IntcodeProgram struct {
|
|||||||
program []int64
|
program []int64
|
||||||
relativeBase int
|
relativeBase int
|
||||||
haltRequested bool
|
haltRequested bool
|
||||||
|
printASCII bool
|
||||||
|
feedInput []rune
|
||||||
}
|
}
|
||||||
|
|
||||||
type IntcodeProgramState struct {
|
type IntcodeProgramState struct {
|
||||||
@ -140,14 +142,15 @@ func (p *IntcodeProgram) Reset() {
|
|||||||
p.relativeBase = 0
|
p.relativeBase = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *IntcodeProgram) Run() {
|
func (p *IntcodeProgram) Run() int64 {
|
||||||
p.RunIn(func(int) int64 { return 0 }, func(int64, IntcodeProgramState) {})
|
return p.RunIn(func(int) int64 { return 0 }, func(int64, IntcodeProgramState) {})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOutputFunc) {
|
func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOutputFunc) int64 {
|
||||||
p.init()
|
p.init()
|
||||||
|
|
||||||
inputsRequested := 0
|
inputsRequested := 0
|
||||||
|
lastOutput := int64(0)
|
||||||
for instructionPointer := 0; instructionPointer < len(p.program) && !p.haltRequested; {
|
for instructionPointer := 0; instructionPointer < len(p.program) && !p.haltRequested; {
|
||||||
instruction := p.GetMemory(instructionPointer)
|
instruction := p.GetMemory(instructionPointer)
|
||||||
instructionPointer++
|
instructionPointer++
|
||||||
@ -184,13 +187,28 @@ func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOut
|
|||||||
case opInput:
|
case opInput:
|
||||||
inputsRequested++
|
inputsRequested++
|
||||||
param1 := p.GetMemory(instructionPointer)
|
param1 := p.GetMemory(instructionPointer)
|
||||||
p.setMemory(int(param1), inputFunc(inputsRequested), paramModes[0])
|
var inputVal int64
|
||||||
|
if len(p.feedInput) > 0 {
|
||||||
|
inputVal = int64(p.feedInput[0])
|
||||||
|
p.feedInput = p.feedInput[1:]
|
||||||
|
} else {
|
||||||
|
inputVal = inputFunc(inputsRequested)
|
||||||
|
}
|
||||||
|
if p.printASCII && inputVal <= 255 {
|
||||||
|
fmt.Printf("%c", rune(inputVal))
|
||||||
|
}
|
||||||
|
p.setMemory(int(param1), inputVal, paramModes[0])
|
||||||
|
|
||||||
instructionPointer += 1
|
instructionPointer += 1
|
||||||
|
|
||||||
case opOutput:
|
case opOutput:
|
||||||
param1 := p.GetMemory(instructionPointer)
|
param1 := p.GetMemory(instructionPointer)
|
||||||
outputFunc(p.getParamValue(int(param1), paramModes[0]), p.makeState(instructionPointer))
|
param1Val := p.getParamValue(int(param1), paramModes[0])
|
||||||
|
if p.printASCII && param1Val <= 255 {
|
||||||
|
fmt.Printf("%c", rune(param1Val))
|
||||||
|
}
|
||||||
|
outputFunc(param1Val, p.makeState(instructionPointer))
|
||||||
|
lastOutput = param1Val
|
||||||
|
|
||||||
instructionPointer += 1
|
instructionPointer += 1
|
||||||
|
|
||||||
@ -256,8 +274,19 @@ func (p *IntcodeProgram) RunIn(inputFunc ProvideInputFunc, outputFunc ReceiveOut
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.haltRequested = false
|
p.haltRequested = false
|
||||||
|
|
||||||
|
return lastOutput
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *IntcodeProgram) Stop() {
|
func (p *IntcodeProgram) Stop() {
|
||||||
p.haltRequested = true
|
p.haltRequested = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *IntcodeProgram) SetDebugASCIIPrint(enable bool) {
|
||||||
|
p.printASCII = enable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *IntcodeProgram) FeedInputString(str string) {
|
||||||
|
p.feedInput = make([]rune, len(str))
|
||||||
|
copy(p.feedInput, []rune(str))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user