day18, example second parallel

This commit is contained in:
efim 2023-12-18 20:33:04 +00:00
parent 568fdd9a70
commit 6efd55ae6a
2 changed files with 126 additions and 79 deletions

View File

@ -7,12 +7,13 @@ import (
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
"sync"
) )
func Run() int { func Run() int {
log.Println("hello day 18") log.Println("hello day 18")
log.Println("problem of lagoon bgins") log.Println("problem of lagoon bgins")
filename := "day18/example" filename := "day18/input"
instructions := ReadInstructionas2(filename) instructions := ReadInstructionas2(filename)
h, w := calcHeightWidth(instructions) h, w := calcHeightWidth(instructions)
log.Printf("read %+v instructions", instructions) log.Printf("read %+v instructions", instructions)
@ -210,8 +211,10 @@ type Cell struct {
} }
type BorderSymbol rune type BorderSymbol rune
// '' always left to right
const (Vertical BorderSymbol = '|' // ” always left to right
const (
Vertical BorderSymbol = '|'
ToDown BorderSymbol = '7' ToDown BorderSymbol = '7'
ToUp BorderSymbol = 'J' ToUp BorderSymbol = 'J'
FromUp BorderSymbol = 'F' FromUp BorderSymbol = 'F'
@ -225,6 +228,7 @@ type Field struct {
MinRow, MaxRow, MinCol, MaxCol int MinRow, MaxRow, MinCol, MaxCol int
BordersFromLeft map[int]map[int]BorderSymbol BordersFromLeft map[int]map[int]BorderSymbol
} }
func (f *Field) confirmCoord(c Coord) { func (f *Field) confirmCoord(c Coord) {
// log.Printf("configming coord %+v", c) // log.Printf("configming coord %+v", c)
@ -337,7 +341,6 @@ func getTurnAsIfGoingFromLeft(directionFrom, directionTo Direction) []BorderSym
symbol = FromUp symbol = FromUp
} }
// panic(fmt.Sprint("got strange from %s to %s", directionFrom.String(), directionTo.String())) // panic(fmt.Sprint("got strange from %s to %s", directionFrom.String(), directionTo.String()))
return []BorderSymbol{symbol} return []BorderSymbol{symbol}
} }
@ -376,14 +379,36 @@ func (f *Field) String() string {
s += "\"" s += "\""
return s return s
} }
func (f *Field) digInsides() (countInside int) { func (f *Field) digInsides() (result int) {
lineSum := make(chan int)
var wg sync.WaitGroup
rowsCount := f.MaxRow - f.MinRow
wg.Add(rowsCount)
done := make(chan bool)
go func() {
wg.Wait()
close(lineSum)
}()
go func() {
for rowInternalCount := range lineSum {
result += rowInternalCount
}
close(done)
}()
for row := f.MinRow; row < f.MaxRow; row++ { for row := f.MinRow; row < f.MaxRow; row++ {
go func(row int){
if row%10000 == 0 { if row%10000 == 0 {
log.Printf("processed rows %d out of %d", row, f.MaxRow) log.Printf("processed rows %d out of %d", row, f.MaxRow)
} }
specialBorders := f.BordersFromLeft[row] specialBorders := f.BordersFromLeft[row]
if len(specialBorders) == 0 { if len(specialBorders) == 0 {
continue wg.Done()
return
} }
type BorderItem struct { type BorderItem struct {
border BorderSymbol border BorderSymbol
@ -431,7 +456,8 @@ func (f *Field) digInsides() (countInside int) {
// ToBeDug: true, // ToBeDug: true,
// } // }
} }
countInside += diff lineSum <- diff
// countInside += diff
} }
if specialBorder.border == Vertical { if specialBorder.border == Vertical {
@ -440,8 +466,14 @@ func (f *Field) digInsides() (countInside int) {
prevBorder = specialBorder prevBorder = specialBorder
} }
wg.Done()
}(row)
} }
return
<-done
return result
} }
// func (f *Field) digInsides() (countInside int) { // func (f *Field) digInsides() (countInside int) {

View File

@ -34,3 +34,18 @@ day18 result: 952408144115
952408144115 952408144115
*** YES. *** YES.
*** about 1M for 4 minutes
** so, my input is ~16M rows
3.5 seconds per 10k
** well, maybe i can parallel.
*** parallel example
day18 result: 952407566854
*** and with separate done channel from the summing goroutine
952408144115
**** YES
** and
2023/12/18 23:35:31 border is 195341588; inside is 148441957805559
2023/12/18 23:35:31
day18 result: 148442153147147
* i should have used a formula. maybe then it would taken less than 4 hours