-
Notifications
You must be signed in to change notification settings - Fork 0
/
day12.go
106 lines (98 loc) · 2.67 KB
/
day12.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
package main
import (
"aoc2021/util"
"fmt"
"strings"
"time"
)
func main() {
lines := util.GetLines("day12\\12.in")
start := time.Now()
day12a(lines)
duration := time.Since(start)
day12b(lines)
duration2 := time.Since(start)
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
}
func makeConnections(lines []string) map[string][]string {
connections := map[string][]string{}
for _, line := range lines {
nodes := strings.Split(line, "-")
nodeA, nodeB := nodes[0], nodes[1]
if connections[nodeA] == nil {
connections[nodeA] = []string{nodeB}
} else {
connections[nodeA] = append(connections[nodeA], nodeB)
}
if connections[nodeB] == nil {
connections[nodeB] = []string{nodeA}
} else {
connections[nodeB] = append(connections[nodeB], nodeA)
}
}
return connections
}
type node struct {
name string
previousNodes string
doubleVisit bool
}
func day12a(lines []string) {
connectionTable := makeConnections(lines)
nodeQueue := []*node{}
// add first node
nodeQueue = append(nodeQueue, &node{"start", "start", false})
counter := 0
for len(nodeQueue) != 0 {
newQueue := []*node{}
for _, n := range nodeQueue {
connections := connectionTable[n.name]
for _, c := range connections {
if c == "end" {
counter++
//fmt.Printf("%s - end\n", n.previousNodes)
} else if strings.ToUpper(c) == c || !strings.Contains(n.previousNodes, c) {
newPrevious := n.previousNodes + " - " + c
newNode := node{c, newPrevious, false}
newQueue = append(newQueue, &newNode)
}
}
}
nodeQueue = newQueue
}
fmt.Printf("Solution for part A: %d\n", counter)
}
func day12b(lines []string) {
connectionTable := makeConnections(lines)
nodeQueue := []*node{}
// add first node
nodeQueue = append(nodeQueue, &node{"start", "start", false})
counter := 0
for len(nodeQueue) != 0 {
newQueue := []*node{}
for _, n := range nodeQueue {
connections := connectionTable[n.name]
for _, c := range connections {
doubleVisit := n.doubleVisit
if c == "start" {
continue
} else if c == "end" {
counter++
//fmt.Printf("%s,end\n", n.previousNodes)
continue
} else if strings.ToUpper(c) != c && strings.Contains(n.previousNodes, c) {
// skip small caves if already visit and double visit checked
if doubleVisit {
continue
}
doubleVisit = true
}
newPrevious := n.previousNodes + "," + c
newNode := node{c, newPrevious, doubleVisit}
newQueue = append(newQueue, &newNode)
}
}
nodeQueue = newQueue
}
fmt.Printf("Solution for part B: %d\n", counter)
}