111 lines
1.9 KiB
Go
111 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.z1glr.de/advent-of-code-2025/pkg/aoc"
|
|
)
|
|
|
|
type Schematic struct {
|
|
rows [][]byte
|
|
splits int
|
|
}
|
|
|
|
func (s *Schematic) CalculateRow(i int) {
|
|
if i == len(s.rows)-1 {
|
|
return
|
|
}
|
|
|
|
for ii, char := range s.rows[i] {
|
|
switch char {
|
|
case byte('S'), byte('|'):
|
|
s.processPosition(i+1, ii)
|
|
}
|
|
}
|
|
|
|
s.CalculateRow(i + 1)
|
|
}
|
|
|
|
func (s *Schematic) processPosition(row, index int) {
|
|
if s.rows[row][index] == byte('.') {
|
|
s.rows[row][index] = byte('|')
|
|
} else if s.rows[row][index] == byte('^') {
|
|
s.splits++
|
|
|
|
s.rows[row][index-1] = byte('|')
|
|
s.rows[row][index+1] = byte('|')
|
|
}
|
|
}
|
|
|
|
func (s *Schematic) Clone() *Schematic {
|
|
returnSchematic := Schematic{
|
|
rows: make([][]byte, len(s.rows)),
|
|
splits: s.splits,
|
|
}
|
|
|
|
for ii, rr := range s.rows {
|
|
returnSchematic.rows[ii] = make([]byte, len(rr))
|
|
copy(returnSchematic.rows[ii], s.rows[ii])
|
|
}
|
|
|
|
return &returnSchematic
|
|
}
|
|
|
|
func (s Schematic) CalculateQuantumRow(i int, pathCount *int) {
|
|
if i == len(s.rows)-1 {
|
|
return
|
|
}
|
|
|
|
for ii, char := range s.rows[i] {
|
|
switch char {
|
|
case byte('S'), byte('|'):
|
|
if s.rows[i+1][ii] == byte('^') {
|
|
(*pathCount)++
|
|
|
|
parallelPath := s.Clone()
|
|
|
|
s.rows[i+1][ii-1] = byte('|')
|
|
parallelPath.rows[i+1][ii+1] = byte('|')
|
|
|
|
parallelPath.CalculateQuantumRow(i+1, pathCount)
|
|
} else {
|
|
s.rows[i+1][ii] = byte('|')
|
|
}
|
|
s.CalculateQuantumRow(i+1, pathCount)
|
|
return
|
|
}
|
|
}
|
|
s.CalculateQuantumRow(i+1, pathCount)
|
|
}
|
|
|
|
func do(test bool) (int, int) {
|
|
rows := aoc.ReadFileRows(test)
|
|
|
|
schematic := Schematic{
|
|
rows: make([][]byte, len(rows)),
|
|
}
|
|
|
|
for ii, rr := range rows {
|
|
schematic.rows[ii] = []byte(rr)
|
|
}
|
|
|
|
quantumSchematic := schematic.Clone()
|
|
|
|
schematic.CalculateRow(0)
|
|
|
|
timelines := 1
|
|
|
|
quantumSchematic.CalculateQuantumRow(0, &timelines)
|
|
|
|
fmt.Println(schematic.splits)
|
|
fmt.Println(timelines)
|
|
|
|
return schematic.splits, timelines
|
|
}
|
|
|
|
func main() {
|
|
if r1, r2 := do(true); r1 == 21 && r2 == 40 {
|
|
do(false)
|
|
}
|
|
}
|