-
Notifications
You must be signed in to change notification settings - Fork 1
/
24.go
110 lines (89 loc) · 1.79 KB
/
24.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"strconv"
)
func combinations(packages []int, target int) [][]int {
queue := [][]int{}
combos := [][]int{}
for _, p := range packages {
queue = append(queue, []int{p})
}
bestCombo := len(packages)
for {
if len(queue) == 0 {
break
}
var item []int
item, queue = queue[0], queue[1:]
sum := 0
for _, v := range item {
sum += v
}
if sum == target {
combos = append(combos, append(item))
if len(item) < bestCombo {
bestCombo = len(item)
}
continue
}
if len(item) >= bestCombo {
continue
}
for _, v := range packages {
if v < item[len(item)-1] && sum+v <= target {
// avoid references
newItem := make([]int, len(item))
copy(newItem, item)
queue = append(queue, append(newItem, v))
}
}
}
return combos
}
func bestCombo(combos [][]int) int {
bestLength := 1000000
for _, v := range combos {
if len(v) < bestLength {
bestLength = len(v)
}
}
bestProduct := 1000000000000
for _, v := range combos {
prd := 1
for _, w := range v {
prd *= w
}
if prd < bestProduct {
bestProduct = prd
}
}
return bestProduct
}
func main() {
file, err := os.Open("input/24.txt")
if err != nil {
log.Fatal(err)
}
packages := []int{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
weight, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Fatal(err)
}
packages = append(packages, weight)
}
// sort descending
sort.Sort(sort.Reverse(sort.IntSlice(packages)))
sum := 0
for _, p := range packages {
sum += p
}
fmt.Println(bestCombo(combinations(packages, sum/3)))
fmt.Println(bestCombo(combinations(packages, sum/4)))
}