day 07 part 1 works, 2 too slow

This commit is contained in:
z1glr
2025-12-09 13:05:23 +01:00
parent 8da2c12663
commit 2b78ac2eff
3 changed files with 268 additions and 0 deletions

110
07/main.go Normal file
View File

@@ -0,0 +1,110 @@
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)
}
}