day 07 part 2

This commit is contained in:
Simon Ziegler
2025-12-10 14:37:59 +01:00
parent 65a974ba95
commit 80ae5ea954

View File

@@ -2,105 +2,69 @@ package main
import (
"fmt"
"strings"
"git.z1glr.de/advent-of-code-2025/pkg/aoc"
)
type Schematic struct {
rows [][]byte
rows []string
timelines [][]int
splits int
}
func (s *Schematic) Calculate() {
startIndex := strings.Index(s.rows[0], "S")
s.timelines = make([][]int, len(s.rows))
for ii, rr := range s.rows {
s.timelines[ii] = make([]int, len(rr))
}
s.timelines[0][startIndex] = 1
s.CalculateRow(0)
}
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)
for ii, count := range s.timelines[i] {
switch s.rows[i+1][ii] {
case byte('^'):
s.timelines[i+1][ii-1] += count
s.timelines[i+1][ii+1] += count
if s.timelines[i][ii] > 0 {
s.splits++
}
default:
s.timelines[i+1][ii] += count
}
}
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)),
rows: aoc.ReadFileRows(test),
}
for ii, rr := range rows {
schematic.rows[ii] = []byte(rr)
schematic.Calculate()
timelineCount := 0
for _, cc := range schematic.timelines[len(schematic.timelines)-1] {
timelineCount += cc
}
quantumSchematic := schematic.Clone()
schematic.CalculateRow(0)
timelines := 1
quantumSchematic.CalculateQuantumRow(0, &timelines)
fmt.Println(schematic.splits)
fmt.Println(timelines)
fmt.Println(timelineCount)
return schematic.splits, timelines
return schematic.splits, timelineCount
}
func main() {