Compare commits
3 Commits
03459bf31a
...
521c576fc1
Author | SHA1 | Date | |
---|---|---|---|
521c576fc1
|
|||
b34a4fc00f
|
|||
a86430f13a
|
24
days/14.go
24
days/14.go
@ -104,26 +104,10 @@ func (d *Day14) Part1() string {
|
||||
func (d *Day14) Part2() string {
|
||||
oreAvailable := int64(1000000000000)
|
||||
estimate := oreAvailable / d.getOreRequiredForFuel(1)
|
||||
|
||||
high := estimate * 2
|
||||
low := estimate
|
||||
|
||||
lastSuccess := low
|
||||
lastFailure := high
|
||||
fuelProduced := low
|
||||
|
||||
for math.Abs(float64(lastFailure-lastSuccess)) > 1 {
|
||||
oreConsumed := d.getOreRequiredForFuel(fuelProduced)
|
||||
adjustment := (lastFailure - lastSuccess) / 2
|
||||
if oreConsumed < oreAvailable {
|
||||
lastSuccess = fuelProduced
|
||||
} else {
|
||||
lastFailure = fuelProduced
|
||||
adjustment = -adjustment
|
||||
}
|
||||
|
||||
fuelProduced += adjustment
|
||||
}
|
||||
lastSuccess := u.Bisect(estimate, estimate*2, 1, func(val int64) bool {
|
||||
oreConsumed := d.getOreRequiredForFuel(val)
|
||||
return oreConsumed < oreAvailable
|
||||
})
|
||||
|
||||
return fmt.Sprintf("Maximum fuel we can make from 1 trillion ore: %s%d%s", u.TextBold, lastSuccess, u.TextReset)
|
||||
}
|
||||
|
122
days/19.go
Normal file
122
days/19.go
Normal file
@ -0,0 +1,122 @@
|
||||
package days
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
u "parnic.com/aoc2019/utilities"
|
||||
)
|
||||
|
||||
type Day19 struct {
|
||||
program u.IntcodeProgram
|
||||
}
|
||||
|
||||
func (d *Day19) Parse() {
|
||||
d.program = u.LoadIntcodeProgram("19p")
|
||||
}
|
||||
|
||||
func (d Day19) Num() int {
|
||||
return 19
|
||||
}
|
||||
|
||||
func (d *Day19) Part1() string {
|
||||
grid := make([][]bool, 50)
|
||||
for y := 0; y < len(grid); y++ {
|
||||
grid[y] = make([]bool, 50)
|
||||
}
|
||||
|
||||
count := int64(0)
|
||||
|
||||
for y := 0; y < 50; y++ {
|
||||
for x := 0; x < 50; x++ {
|
||||
d.program.Reset()
|
||||
d.program.RunIn(func(inputStep int) int64 {
|
||||
if inputStep == 1 {
|
||||
return int64(x)
|
||||
}
|
||||
|
||||
return int64(y)
|
||||
}, func(val int64, state u.IntcodeProgramState) {
|
||||
res := val == 1
|
||||
grid[y][x] = res
|
||||
if res {
|
||||
count++
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// fmt.Println("50x50 tractor view:")
|
||||
// for y := 0; y < len(grid); y++ {
|
||||
// for x := 0; x < len(grid[y]); x++ {
|
||||
// if grid[y][x] {
|
||||
// fmt.Print("█")
|
||||
// } else {
|
||||
// fmt.Print(" ")
|
||||
// }
|
||||
// }
|
||||
// fmt.Println()
|
||||
// }
|
||||
|
||||
return fmt.Sprintf("Points affected in 50x50 area: %s%d%s", u.TextBold, count, u.TextReset)
|
||||
}
|
||||
|
||||
func (d *Day19) Part2() string {
|
||||
f := func(x, y int) bool {
|
||||
ret := false
|
||||
d.program.Reset()
|
||||
d.program.RunIn(func(inputStep int) int64 {
|
||||
if inputStep == 1 {
|
||||
return int64(x)
|
||||
}
|
||||
return int64(y)
|
||||
}, func(val int64, state u.IntcodeProgramState) {
|
||||
ret = val == 1
|
||||
})
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
// find lower bound
|
||||
startY := 0
|
||||
startX := 0
|
||||
for y := 1; startY == 0; y++ {
|
||||
for x := 0; x < 10*y; x++ {
|
||||
if f(x, y) {
|
||||
startY = y
|
||||
startX = x
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lastGoodX := 0
|
||||
threshold := 1
|
||||
y := u.Bisect(startY+100, 2500, threshold, func(y int) bool {
|
||||
foundX := false
|
||||
for x := startX; ; x++ {
|
||||
if !f(x, y) {
|
||||
if !foundX {
|
||||
continue
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if !foundX {
|
||||
foundX = true
|
||||
}
|
||||
|
||||
if !f(x+99, y) {
|
||||
return true
|
||||
}
|
||||
if !f(x, y+99) {
|
||||
continue
|
||||
}
|
||||
|
||||
lastGoodX = x
|
||||
return false
|
||||
}
|
||||
})
|
||||
|
||||
result := (lastGoodX * 10000) + y
|
||||
return fmt.Sprintf("Closest 100x100 square for the ship starts at %d,%d = %s%d%s", lastGoodX, y, u.TextBold, result, u.TextReset)
|
||||
}
|
1
inputs/19p.txt
Normal file
1
inputs/19p.txt
Normal file
@ -0,0 +1 @@
|
||||
109,424,203,1,21102,11,1,0,1105,1,282,21101,0,18,0,1105,1,259,2101,0,1,221,203,1,21102,1,31,0,1105,1,282,21102,1,38,0,1105,1,259,20102,1,23,2,21201,1,0,3,21102,1,1,1,21102,57,1,0,1106,0,303,2102,1,1,222,21002,221,1,3,20101,0,221,2,21101,0,259,1,21101,0,80,0,1105,1,225,21101,44,0,2,21102,91,1,0,1105,1,303,1202,1,1,223,21002,222,1,4,21102,259,1,3,21102,1,225,2,21102,225,1,1,21101,118,0,0,1106,0,225,21002,222,1,3,21101,163,0,2,21101,0,133,0,1106,0,303,21202,1,-1,1,22001,223,1,1,21102,148,1,0,1106,0,259,1202,1,1,223,20101,0,221,4,21001,222,0,3,21102,1,24,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,21101,195,0,0,105,1,108,20207,1,223,2,21002,23,1,1,21102,-1,1,3,21102,1,214,0,1106,0,303,22101,1,1,1,204,1,99,0,0,0,0,109,5,2101,0,-4,249,22102,1,-3,1,22101,0,-2,2,22101,0,-1,3,21102,250,1,0,1106,0,225,21202,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,22102,1,-2,-2,109,-3,2105,1,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,21202,-2,1,3,21101,0,343,0,1106,0,303,1106,0,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,21201,-4,0,1,21101,384,0,0,1105,1,303,1105,1,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
|
1
main.go
1
main.go
@ -52,6 +52,7 @@ var dayMap = []day{
|
||||
&days.Day16{},
|
||||
&days.Day17{},
|
||||
&days.Day18{},
|
||||
&days.Day19{},
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
27
utilities/bisect.go
Normal file
27
utilities/bisect.go
Normal file
@ -0,0 +1,27 @@
|
||||
package utilities
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// Bisect takes a known-good low and known-bad high value as the bounds
|
||||
// to bisect, and a function to test each value for success or failure.
|
||||
// If the function succeeds, the value is adjusted toward the maximum,
|
||||
// and if the function fails, the value is adjusted toward the minimum.
|
||||
// The final value is returned when the difference between the success
|
||||
// and the failure is less than or equal to the acceptance threshold
|
||||
// (usually 1, for integers).
|
||||
func Bisect[T Number](low, high, threshold T, tryFunc func(val T) bool) T {
|
||||
for T(math.Abs(float64(high-low))) > threshold {
|
||||
currVal := low + ((high - low) / 2)
|
||||
success := tryFunc(currVal)
|
||||
if success {
|
||||
low = currVal
|
||||
} else {
|
||||
high = currVal
|
||||
}
|
||||
}
|
||||
|
||||
return low
|
||||
}
|
@ -128,9 +128,15 @@ func (p *IntcodeProgram) ensureMemoryCapacity(address int) {
|
||||
}
|
||||
|
||||
func (p *IntcodeProgram) Reset() {
|
||||
wiped := false
|
||||
if len(p.memory) != len(p.program) {
|
||||
wiped = true
|
||||
p.memory = nil
|
||||
}
|
||||
p.init()
|
||||
if !wiped {
|
||||
copy(p.memory, p.program)
|
||||
}
|
||||
p.relativeBase = 0
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user