-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10b.go
More file actions
77 lines (62 loc) · 1.83 KB
/
day10b.go
File metadata and controls
77 lines (62 loc) · 1.83 KB
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
package main
import (
"fmt"
"sort"
"strconv"
)
var resultsCache map[int]int
func calcPathsRecurse(workNumber int, joltList []int, debug bool) int {
var pathCount int = 0
var loopCount int = 0
var result int
// workNumber: the item to look at
// joltList: the remaining list of adapters to examine
// Work out how many of the list *could* work from here
// Recurse with each potential and a subset of the list
// Return numberOfArrangements
if debug {
fmt.Println("===================")
fmt.Printf("Checking: %d\n", workNumber)
fmt.Println("Remaining List:", joltList)
}
for i := 0; i < len(joltList); i++ {
loopCount++
if joltList[i]-workNumber > 3 {
// List is sorted so we quit the loop when we're found all we can
break
}
if resultsCache[joltList[i]] > 0 {
pathCount += resultsCache[joltList[i]]
} else {
result = calcPathsRecurse(joltList[i], joltList[i+1:], debug)
resultsCache[joltList[i]] = result
pathCount += result
}
if loopCount > 1 {
pathCount++
}
}
return pathCount
}
func calcNumberJoltArrangements(filename string, part byte, debug bool) int {
var distinctArrangements int = 0
var joltList []int
puzzleInput, _ := readFile(filename)
joltList = make([]int, len(puzzleInput))
// Process the jolt list into a more usable form
for item, number := range puzzleInput {
joltList[item], _ = strconv.Atoi(number)
}
sort.Ints(joltList)
resultsCache = make(map[int]int)
// Don't have to use every adapter now
// Recurse with number list
// Work out how many of the list *could* work from here
// Recurse with each potential and a subset of the list
// Return numberOfArrangements
fmt.Println("Recursing....")
distinctArrangements = calcPathsRecurse(0, joltList, debug)
distinctArrangements++
fmt.Println(".....back from recurse")
return distinctArrangements
}