Skip to content

Commit d0c0b4a

Browse files
committed
refactor: cleanups
1 parent a3d8e85 commit d0c0b4a

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

19/main.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ type State struct {
3333
collected Resources
3434
}
3535

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+
3646
func Min(a, b int) int {
3747
if a < b {
3848
return a
@@ -77,25 +87,25 @@ func (resources *Resources) CanAfford(costs Resources) bool {
7787
resources.geode >= costs.geode
7888
}
7989

80-
func (state State) CompareFitness(champion State) bool {
90+
func (state *State) CompareFitness(champion State) int {
8191
weights := Resources{
8292
1,
8393
16,
8494
256,
8595
1024,
8696
}
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)
91101
}
92102

93103
func (states States) Len() int {
94104
return len(states)
95105
}
96106

97107
func (states States) Less(i, j int) bool {
98-
return states[i].CompareFitness(states[j])
108+
return states[i].CompareFitness(states[j]) < 0
99109
}
100110

101111
func (states States) Swap(i, j int) {
@@ -129,19 +139,19 @@ func Solve(blueprint Blueprint, time int, maxStates int) int {
129139
sort.Sort(sort.Reverse(newStates))
130140
states = newStates[:Min(maxStates, len(newStates))]
131141
}
142+
132143
maxGeodes := 0
133144
for _, state := range states {
134145
maxGeodes = Max(state.collected.geode, maxGeodes)
135146
}
136147
return maxGeodes
137148
}
138149

139-
func Solve1(blueprints []Blueprint) int {
140-
result := 0
150+
func Solve1(blueprints []Blueprint) (result int) {
141151
for _, blueprint := range blueprints {
142152
result += blueprint.id * Solve(blueprint, 24, 1024)
143153
}
144-
return result
154+
return
145155
}
146156

147157
func Solve2(blueprints []Blueprint) int {

0 commit comments

Comments
 (0)