Browse Source

2019 Day 19 Begun

master
Brian Buller 2 years ago
parent
commit
eea657e331
  1. 6
      2019/day17/main.go
  2. 1
      2019/day19/input
  3. 185
      2019/day19/main.go

6
2019/day17/main.go

@ -7,12 +7,6 @@ import (
var layout [][]byte
/*
L,4,L,4,L,10,R,4,R,4,L,4,L,4,R,8,R,10,L,4,L,4,L,10,R,4,R,4,L,10,R,10,L,4,L,4,L,10,R,4,R,4,L,10,R,10,R,4,L,4,L,4,R,8,R,10,R,4,L,10,R,10,R,4,L,10,R,10,R,4,L,4,L,4,R,8,R,10
*/
func main() {
pt := helpers.GetArgNumber(1)
prog := intcode.ReadIntCodeFile("input")

1
2019/day19/input

@ -0,0 +1 @@
109,424,203,1,21101,0,11,0,1106,0,282,21102,18,1,0,1106,0,259,2101,0,1,221,203,1,21102,1,31,0,1105,1,282,21101,0,38,0,1106,0,259,20102,1,23,2,22101,0,1,3,21102,1,1,1,21101,57,0,0,1105,1,303,2102,1,1,222,20101,0,221,3,21002,221,1,2,21101,0,259,1,21102,1,80,0,1105,1,225,21102,125,1,2,21102,1,91,0,1106,0,303,2101,0,1,223,21002,222,1,4,21102,1,259,3,21102,225,1,2,21102,225,1,1,21101,0,118,0,1106,0,225,20102,1,222,3,21101,0,69,2,21102,1,133,0,1106,0,303,21202,1,-1,1,22001,223,1,1,21102,148,1,0,1106,0,259,1201,1,0,223,20101,0,221,4,21001,222,0,3,21102,1,22,2,1001,132,-2,224,1002,224,2,224,1001,224,3,224,1002,132,-1,132,1,224,132,224,21001,224,1,1,21102,195,1,0,106,0,108,20207,1,223,2,20101,0,23,1,21102,-1,1,3,21101,0,214,0,1105,1,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,1202,-4,1,249,21202,-3,1,1,22102,1,-2,2,21201,-1,0,3,21101,250,0,0,1106,0,225,22102,1,1,-4,109,-5,2105,1,0,109,3,22107,0,-2,-1,21202,-1,2,-1,21201,-1,-1,-1,22202,-1,-2,-2,109,-3,2106,0,0,109,3,21207,-2,0,-1,1206,-1,294,104,0,99,22101,0,-2,-2,109,-3,2106,0,0,109,5,22207,-3,-4,-1,1206,-1,346,22201,-4,-3,-4,21202,-3,-1,-1,22201,-4,-1,2,21202,2,-1,-1,22201,-4,-1,1,22102,1,-2,3,21101,0,343,0,1106,0,303,1105,1,415,22207,-2,-3,-1,1206,-1,387,22201,-3,-2,-3,21202,-2,-1,-1,22201,-3,-1,3,21202,3,-1,-1,22201,-3,-1,2,22102,1,-4,1,21101,384,0,0,1106,0,303,1106,0,415,21202,-4,-1,-4,22201,-4,-3,-4,22202,-3,-2,-2,22202,-2,-4,-4,22202,-3,-2,-3,21202,-4,-1,-2,22201,-3,-2,1,21202,1,1,-4,109,-5,2105,1,0

185
2019/day19/main.go

@ -0,0 +1,185 @@
package main
import (
"fmt"
"strings"
"time"
intcode "git.bullercodeworks.com/brian/adventofcode/2019/intcode-processor"
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
)
var beam map[string]int
var maxY int
func main() {
pt := helpers.GetArgNumber(1)
prog := intcode.ReadIntCodeFile("input")
beam = make(map[string]int)
if pt == "1" {
part1(prog)
} else {
part2(prog)
}
}
func part1(prog []int) {
var count int
var lastRowStart int
for y := 0; y < 50; y++ {
var foundFirst bool
fmt.Print(strings.Repeat(" ", lastRowStart))
for x := lastRowStart; x < 50; x++ {
out := runCodeAt(prog, x, y)
fmt.Print(out)
beam[c(x, y)] = out
if out == 1 {
if !foundFirst {
lastRowStart = x
}
foundFirst = true
count++
} else if foundFirst {
break
}
}
fmt.Println()
maxY = y
}
fmt.Println("Total:", count)
}
func part2(prog []int) {
shipSize := 100
// Guesses
//x, y := 0, 1000
var x, square int
bot, _ := 1025, 1037
for square != shipSize {
bot++
fmt.Print("Calculating ", bot, "... ")
square, x = maxSquare(prog, bot, bot/2)
fmt.Println(square)
}
fmt.Println(x, bot)
}
func part2Almost(prog []int) {
shipSize := 100
// Guesses
x, y := 0, 1000
var square int
bot, top := 1026, 1037
square, x = maxSquare(prog, y, 0)
lastCheckRow := y
for {
if square > shipSize {
y = y - (y-bot)/2
} else if square < shipSize {
y = y + (top-y)/2
}
if lastCheckRow == y {
break
}
lastCheckRow = y
fmt.Print("Calculating at ", y, "... ")
square, x = maxSquare(prog, y, (y / 2))
if square > 100 && y < top {
top = y
} else if square < 100 && y > bot {
bot = y
}
fmt.Println("Square:", square)
}
fmt.Println("Final Square:", square)
fmt.Println(x, y)
}
func part2Slow(prog []int) {
shipSize := 100
var x, y int
retX, retY := -1, -1
var lastRowStart int
for y = maxY; true; y++ {
var foundFirst bool
fmt.Print(strings.Repeat(" ", lastRowStart))
for x = lastRowStart; x < y; x++ {
out := runCodeAt(prog, x, y)
fmt.Print(out)
beam[c(x, y)] = out
if out == 1 {
if !foundFirst {
lastRowStart = x
}
foundFirst = true
} else if foundFirst {
break
}
}
square, _ := maxSquare(prog, y, lastRowStart)
fmt.Println(":", square)
// Every row, check what the maximum square is
if square >= shipSize {
retY, retX = y, lastRowStart
break
}
}
fmt.Println(retX, ",", retY)
}
// maxSquare returns the square size and the bottom-left x position it starts at
func maxSquare(prog []int, row, firstX int) (int, int) {
for ; firstX < row; firstX++ {
// Find the first X
if runCodeAt(prog, firstX, row) == 0 {
continue
}
var square int
for d := 0; true; d++ {
if runCodeAt(prog, firstX+d, row-d) == 0 {
return square, firstX
}
square++
}
}
return 0, firstX
}
func runCodeAt(prog []int, x, y int) int {
// If we've already cached this pos, just return it
if v, ok := beam[c(x, y)]; ok {
return v
}
var res int
p := intcode.NewProgram(prog)
go func() {
sendInputs(p, []int{x, y})
res = getOutput(p)
}()
p.Run()
beam[c(x, y)] = res
return res
}
func sendInputs(p *intcode.Program, inp []int) {
for k := range inp {
for !p.NeedsInput() {
time.Sleep(1)
}
p.Input(inp[k])
}
}
func getOutput(p *intcode.Program) int {
for !p.NeedsOutput() {
time.Sleep(1)
}
return p.Output()
}
func c(x, y int) string {
return fmt.Sprintf("[%d, %d]", x, y)
}
Loading…
Cancel
Save