@@ -33,6 +33,16 @@ type State struct {
33
33
collected Resources
34
34
}
35
35
36
+ func Sign (n int ) int {
37
+ if n < 0 {
38
+ return - 1
39
+ }
40
+ if n > 0 {
41
+ return 1
42
+ }
43
+ return 0
44
+ }
45
+
36
46
func Min (a , b int ) int {
37
47
if a < b {
38
48
return a
@@ -77,25 +87,25 @@ func (resources *Resources) CanAfford(costs Resources) bool {
77
87
resources .geode >= costs .geode
78
88
}
79
89
80
- func (state State ) CompareFitness (champion State ) bool {
90
+ func (state * State ) CompareFitness (champion State ) int {
81
91
weights := Resources {
82
92
1 ,
83
93
16 ,
84
94
256 ,
85
95
1024 ,
86
96
}
87
- return ( state .collected .ore - champion .collected .ore )* weights .ore +
88
- (state .collected .clay - champion .collected .clay )* weights .clay +
89
- (state .collected .obsidian - champion .collected .obsidian )* weights .obsidian +
90
- (state .collected .geode - champion .collected .geode )* weights .geode < 0
97
+ return Sign (( state .collected .ore - champion .collected .ore )* weights .ore +
98
+ (state .collected .clay - champion .collected .clay )* weights .clay +
99
+ (state .collected .obsidian - champion .collected .obsidian )* weights .obsidian +
100
+ (state .collected .geode - champion .collected .geode )* weights .geode )
91
101
}
92
102
93
103
func (states States ) Len () int {
94
104
return len (states )
95
105
}
96
106
97
107
func (states States ) Less (i , j int ) bool {
98
- return states [i ].CompareFitness (states [j ])
108
+ return states [i ].CompareFitness (states [j ]) < 0
99
109
}
100
110
101
111
func (states States ) Swap (i , j int ) {
@@ -129,19 +139,19 @@ func Solve(blueprint Blueprint, time int, maxStates int) int {
129
139
sort .Sort (sort .Reverse (newStates ))
130
140
states = newStates [:Min (maxStates , len (newStates ))]
131
141
}
142
+
132
143
maxGeodes := 0
133
144
for _ , state := range states {
134
145
maxGeodes = Max (state .collected .geode , maxGeodes )
135
146
}
136
147
return maxGeodes
137
148
}
138
149
139
- func Solve1 (blueprints []Blueprint ) int {
140
- result := 0
150
+ func Solve1 (blueprints []Blueprint ) (result int ) {
141
151
for _ , blueprint := range blueprints {
142
152
result += blueprint .id * Solve (blueprint , 24 , 1024 )
143
153
}
144
- return result
154
+ return
145
155
}
146
156
147
157
func Solve2 (blueprints []Blueprint ) int {
0 commit comments