4
4
"advent-of-code/util"
5
5
"bufio"
6
6
"fmt"
7
+ "math"
7
8
"regexp"
8
9
)
9
10
@@ -30,13 +31,12 @@ func main() {
30
31
prize = parsePosition (scan .Bytes ())
31
32
scan .Scan ()
32
33
33
- if buttonA .X + buttonA .Y > buttonB .X + buttonB .Y {
34
- da , db = simulate (buttonA , buttonB , prize )
35
- dTokens = da * 3 + db
36
- } else {
37
- da , db = simulate (buttonB , buttonA , prize )
38
- dTokens = db * 3 + da
39
- }
34
+ // Part 2
35
+ prize .X += 10000000000000
36
+ prize .Y += 10000000000000
37
+
38
+ da , db = simulate (buttonA , buttonB , prize )
39
+ dTokens = da * 3 + db
40
40
41
41
fmt .Println (buttonA , buttonB , prize , dTokens )
42
42
@@ -60,31 +60,14 @@ func parsePosition(line []byte) position {
60
60
}
61
61
62
62
func simulate (buttonA , buttonB , prize position ) (int , int ) {
63
- maxA := min (100 , prize .X / buttonA .X , prize .Y / buttonA .Y )
64
- maxB := min (100 , prize .X / buttonB .X , prize .Y / buttonB .Y )
65
- var da , db , dPresses int
66
- var p position
67
- var found bool
68
-
69
- outer:
70
- for da = maxA ; da >= 0 ; da -- {
71
- dPresses = da
72
- p .X = buttonA .X * dPresses
73
- p .Y = buttonA .Y * dPresses
74
-
75
- for db = 0 ; db < maxB ; db ++ {
76
- if p .X + db * buttonB .X == prize .X && p .Y + db * buttonB .Y == prize .Y {
77
- dPresses += db
78
- found = true
79
- break outer
80
- }
81
- }
63
+ var da , db float64
82
64
83
- }
84
-
85
- if ! found {
65
+ db = float64 (buttonA .Y * prize .X - buttonA .X * prize .Y ) / float64 (buttonB .X * buttonA .Y - buttonB .Y * buttonA .X )
66
+ if db != math .Floor (db ) {
86
67
return 0 , 0
87
68
}
88
69
89
- return da , db
70
+ da = float64 (prize .Y - int (db )* buttonB .Y ) / float64 (buttonA .Y )
71
+
72
+ return int (da ), int (db )
90
73
}
0 commit comments