feat: I think I'm getting closer
This commit is contained in:
+86
-13
@@ -5,6 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/file"
|
||||||
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/queue"
|
||||||
"github.com/StevanFreeborn/advent-of-code-2025/internal/stack"
|
"github.com/StevanFreeborn/advent-of-code-2025/internal/stack"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -47,8 +48,9 @@ func SolvePartOne(filePath string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type node struct {
|
type node struct {
|
||||||
value string
|
value string
|
||||||
numberOfParentPaths int
|
fftSeen bool
|
||||||
|
dacSeen bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func SolvePartTwo(filePath string) int {
|
func SolvePartTwo(filePath string) int {
|
||||||
@@ -65,21 +67,20 @@ func SolvePartTwo(filePath string) int {
|
|||||||
stack.Push(node{
|
stack.Push(node{
|
||||||
value: SVR_NODE,
|
value: SVR_NODE,
|
||||||
})
|
})
|
||||||
visisted := map[node]int{}
|
|
||||||
pathCount := 0
|
pathCount := 0
|
||||||
|
|
||||||
for stack.IsEmpty() == false {
|
for stack.IsEmpty() == false {
|
||||||
current, _ := stack.Pop()
|
current, _ := stack.Pop()
|
||||||
|
|
||||||
_, ok := visisted[current]
|
if current.value == FFT_NODE {
|
||||||
|
current.fftSeen = true
|
||||||
if ok {
|
|
||||||
visisted[current]++
|
|
||||||
} else {
|
|
||||||
visisted[current] = 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if current.value == OUT_NODE {
|
if current.value == DAC_NODE {
|
||||||
|
current.dacSeen = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if current.value == OUT_NODE && current.dacSeen && current.fftSeen {
|
||||||
pathCount++
|
pathCount++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -88,12 +89,84 @@ func SolvePartTwo(filePath string) int {
|
|||||||
|
|
||||||
for _, n := range neighbors {
|
for _, n := range neighbors {
|
||||||
stack.Push(node{
|
stack.Push(node{
|
||||||
value: n,
|
value: n,
|
||||||
|
fftSeen: current.fftSeen,
|
||||||
|
dacSeen: current.dacSeen,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(visisted)
|
|
||||||
|
|
||||||
return pathCount
|
return pathCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SolvePartTwoAgain(filePath string) int {
|
||||||
|
adjacencyList := map[string][]string{}
|
||||||
|
inDegrees := map[string]int{}
|
||||||
|
allNodes := map[string]bool{}
|
||||||
|
|
||||||
|
for line := range file.ReadLines(filePath) {
|
||||||
|
parts := strings.Split(line, ": ")
|
||||||
|
from := parts[0]
|
||||||
|
toList := strings.Split(parts[1], " ")
|
||||||
|
|
||||||
|
allNodes[from] = true
|
||||||
|
|
||||||
|
_, existingInDegrees := inDegrees[from]
|
||||||
|
|
||||||
|
if existingInDegrees == false {
|
||||||
|
inDegrees[from] = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, to := range toList {
|
||||||
|
allNodes[to] = true
|
||||||
|
adjacencyList[from] = append(adjacencyList[from], to)
|
||||||
|
inDegrees[to]++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
queue := queue.New[string]()
|
||||||
|
|
||||||
|
for node := range allNodes {
|
||||||
|
if inDegrees[node] == 0 {
|
||||||
|
queue.Enqueue(node)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
processOrder := []string{}
|
||||||
|
|
||||||
|
for queue.IsEmpty() == false {
|
||||||
|
current, _ := queue.Dequeue()
|
||||||
|
processOrder = append(processOrder, current)
|
||||||
|
|
||||||
|
for _, neighbor := range adjacencyList[current] {
|
||||||
|
inDegrees[neighbor]--
|
||||||
|
|
||||||
|
if inDegrees[neighbor] == 0 {
|
||||||
|
queue.Enqueue(neighbor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pathsCount := map[string]int{}
|
||||||
|
pathsCount[SVR_NODE] = 1
|
||||||
|
|
||||||
|
for _, item := range processOrder {
|
||||||
|
if pathsCount[item] == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, n := range adjacencyList[item] {
|
||||||
|
pathsCount[n] += pathsCount[item]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Could I process the graph
|
||||||
|
// in smaller pieces and then multiply
|
||||||
|
// those path to figure out what the
|
||||||
|
// total number of paths are.
|
||||||
|
fmt.Println(pathsCount[OUT_NODE])
|
||||||
|
fmt.Println(pathsCount[DAC_NODE])
|
||||||
|
fmt.Println(pathsCount[FFT_NODE])
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|||||||
@@ -48,3 +48,23 @@ func TestSolvePartTwoWithInput(t *testing.T) {
|
|||||||
t.Errorf("SolvePartTwo(INPUT.txt) = %d; want %d", result, expected)
|
t.Errorf("SolvePartTwo(INPUT.txt) = %d; want %d", result, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSolvePartTwoAgainWithExampleInput(t *testing.T) {
|
||||||
|
expected := 2
|
||||||
|
|
||||||
|
result := solution.SolvePartTwoAgain("EXAMPLE_TWO.txt")
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("SolvePartTwoAgain(EXAMPLE_TWO.txt) = %d; want %d", result, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSolvePartTwoAgainWithInput(t *testing.T) {
|
||||||
|
expected := -1
|
||||||
|
|
||||||
|
result := solution.SolvePartTwoAgain("INPUT.txt")
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf("SolvePartTwoAgain(INPUT.txt) = %d; want %d", result, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user