|
@@ -9,12 +9,12 @@ import (
|
|
|
|
|
|
type Button struct {
|
|
type Button struct {
|
|
id byte
|
|
id byte
|
|
- x, y int
|
|
|
|
|
|
+ x, y int64
|
|
}
|
|
}
|
|
|
|
|
|
type Machine struct {
|
|
type Machine struct {
|
|
buttons []Button
|
|
buttons []Button
|
|
- x, y int
|
|
|
|
|
|
+ x, y int64
|
|
}
|
|
}
|
|
|
|
|
|
func readInput(file *os.File) []Machine {
|
|
func readInput(file *os.File) []Machine {
|
|
@@ -53,7 +53,7 @@ func readInput(file *os.File) []Machine {
|
|
return machines
|
|
return machines
|
|
}
|
|
}
|
|
|
|
|
|
-func min(a, b int) int {
|
|
|
|
|
|
+func min(a, b int64) int64 {
|
|
if a < b {
|
|
if a < b {
|
|
return a
|
|
return a
|
|
}
|
|
}
|
|
@@ -61,8 +61,8 @@ func min(a, b int) int {
|
|
return b
|
|
return b
|
|
}
|
|
}
|
|
|
|
|
|
-func calculate(machine Machine, button int, limit int) [2]int {
|
|
|
|
- var results [2]int
|
|
|
|
|
|
+func calculate(machine Machine, button int, limit int64, minimum int64) [2]int64 {
|
|
|
|
+ var results [2]int64
|
|
otherButton := (button + 1) % 2
|
|
otherButton := (button + 1) % 2
|
|
|
|
|
|
if machine.x%machine.buttons[button].x == 0 && machine.y%machine.buttons[button].y == 0 {
|
|
if machine.x%machine.buttons[button].x == 0 && machine.y%machine.buttons[button].y == 0 {
|
|
@@ -79,7 +79,7 @@ func calculate(machine Machine, button int, limit int) [2]int {
|
|
start = limit
|
|
start = limit
|
|
}
|
|
}
|
|
|
|
|
|
- for ; start > 0; start-- {
|
|
|
|
|
|
+ for ; start > minimum; start-- {
|
|
deltaX := machine.x - start*machine.buttons[button].x
|
|
deltaX := machine.x - start*machine.buttons[button].x
|
|
if deltaX%machine.buttons[otherButton].x == 0 {
|
|
if deltaX%machine.buttons[otherButton].x == 0 {
|
|
otherPushes := deltaX / machine.buttons[otherButton].x
|
|
otherPushes := deltaX / machine.buttons[otherButton].x
|
|
@@ -100,9 +100,12 @@ func calculate(machine Machine, button int, limit int) [2]int {
|
|
return results
|
|
return results
|
|
}
|
|
}
|
|
|
|
|
|
-func checkMachine(machine Machine, limit int) int {
|
|
|
|
- resultA := calculate(machine, 0, limit)
|
|
|
|
- resultB := calculate(machine, 1, limit)
|
|
|
|
|
|
+func checkMachine(machine Machine, limit int64, modifier int64, minimum int64) int64 {
|
|
|
|
+ machine.x += modifier
|
|
|
|
+ machine.y += modifier
|
|
|
|
+
|
|
|
|
+ resultA := calculate(machine, 0, limit, minimum)
|
|
|
|
+ resultB := calculate(machine, 1, limit, minimum)
|
|
|
|
|
|
costA := resultA[0]*3 + resultA[1]
|
|
costA := resultA[0]*3 + resultA[1]
|
|
costB := resultB[0]*3 + resultB[1]
|
|
costB := resultB[0]*3 + resultB[1]
|
|
@@ -110,10 +113,10 @@ func checkMachine(machine Machine, limit int) int {
|
|
return min(costA, costB)
|
|
return min(costA, costB)
|
|
}
|
|
}
|
|
|
|
|
|
-func solve(machines []Machine, limit int) int {
|
|
|
|
- var result int
|
|
|
|
|
|
+func solve(machines []Machine, limit int64, modifier int64, minimum int64) int64 {
|
|
|
|
+ var result int64
|
|
for _, machine := range machines {
|
|
for _, machine := range machines {
|
|
- result += checkMachine(machine, limit)
|
|
|
|
|
|
+ result += checkMachine(machine, limit, modifier, minimum)
|
|
}
|
|
}
|
|
|
|
|
|
return result
|
|
return result
|
|
@@ -131,5 +134,6 @@ func main() {
|
|
}
|
|
}
|
|
|
|
|
|
machines := readInput(file)
|
|
machines := readInput(file)
|
|
- fmt.Println("Part1:", solve(machines, 100))
|
|
|
|
|
|
+ fmt.Println("Part1:", solve(machines, 100, 0, 0))
|
|
|
|
+ fmt.Println("Part2:", solve(machines, -1, 10000000000000, 100000000000))
|
|
}
|
|
}
|