day16, example two
This commit is contained in:
parent
8436426d3a
commit
ee9c2c1ca0
|
@ -3,6 +3,7 @@ package day16
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -11,13 +12,63 @@ import (
|
|||
func Run() int {
|
||||
fmt.Println("hello from day 16")
|
||||
log.Println("starting")
|
||||
field := ReadField("day16/input")
|
||||
fmt.Println(field.String())
|
||||
field.StartTraversal()
|
||||
filename := "day16/example"
|
||||
field := ReadField(filename)
|
||||
startPoints := StartPoints(&field)
|
||||
|
||||
fmt.Println(field.ShowEnergyzed())
|
||||
var startPointsWaitGroup sync.WaitGroup
|
||||
startPointsWaitGroup.Add(len(startPoints))
|
||||
|
||||
return field.CountEnergized()
|
||||
results := make(chan int)
|
||||
|
||||
go func() {
|
||||
startPointsWaitGroup.Wait()
|
||||
close(results)
|
||||
}()
|
||||
|
||||
for _, start := range startPoints {
|
||||
go func(start MovementPoint) {
|
||||
cleanField := ReadField(filename)
|
||||
cleanField.StartTraversal(start)
|
||||
thisResult := cleanField.CountEnergized()
|
||||
results <- thisResult
|
||||
startPointsWaitGroup.Done()
|
||||
}(start)
|
||||
}
|
||||
|
||||
max := math.MinInt
|
||||
for energized := range results {
|
||||
if energized > max {
|
||||
max = energized
|
||||
log.Println("found new max: ", max)
|
||||
}
|
||||
}
|
||||
|
||||
// fmt.Println(field.String())
|
||||
// field.StartTraversal()
|
||||
|
||||
// fmt.Println(field.ShowEnergyzed())
|
||||
|
||||
return max
|
||||
}
|
||||
|
||||
func StartPoints(f *Field) []MovementPoint {
|
||||
result := make([]MovementPoint, 0)
|
||||
|
||||
for rowNum, row := range f.cells {
|
||||
result = append(result,
|
||||
MovementPoint{Row: rowNum, Col: 0, Direction: Rightward},
|
||||
MovementPoint{Row: rowNum, Col: len(row) - 1, Direction: Leftward})
|
||||
}
|
||||
|
||||
for colNum, _ := range f.cells[0] {
|
||||
result = append(result,
|
||||
MovementPoint{Row: 0, Col: colNum, Direction: Downward},
|
||||
MovementPoint{Row: len(f.cells) - 1, Col: colNum, Direction: Upward})
|
||||
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// have shared field
|
||||
|
@ -117,14 +168,11 @@ type MovementPoint struct {
|
|||
Direction Direction
|
||||
}
|
||||
|
||||
func (f *Field)StartTraversal() {
|
||||
func (f *Field) StartTraversal(startPoint MovementPoint) {
|
||||
reportedVisits := make(chan MovementPoint)
|
||||
var wg sync.WaitGroup
|
||||
|
||||
go f.RecordVisits(reportedVisits)
|
||||
startPoint := MovementPoint {
|
||||
Row: 0, Col: 0, Direction: Rightward,
|
||||
}
|
||||
wg.Add(1)
|
||||
go f.TraverseFrom(startPoint, reportedVisits, &wg)
|
||||
|
||||
|
|
Loading…
Reference in New Issue