day11, part2

This commit is contained in:
efim
2023-12-11 11:23:06 +00:00
parent 65d6c13016
commit 7032666476
2 changed files with 152 additions and 9 deletions

View File

@@ -7,10 +7,10 @@ import (
"strings"
)
func Run() int {
func Run() int64 {
fmt.Println("hello day 11")
filename := "day11/example"
filename := "day11/input"
bytes, err := os.ReadFile(filename)
if err != nil {
panic(fmt.Sprint("cannot read file ", filename))
@@ -57,7 +57,7 @@ func Run() int {
}
}
var distanceSum int
var distanceSum int64
for i := 0; i < len(starCoords); i++ {
for j := i+1; j < len(starCoords); j++ {
// calc distance between stars i and j
@@ -71,9 +71,12 @@ func Run() int {
maxRow, minRow = minRow, maxRow
}
emptyRowsBetween := emptyRowsAbove[maxRow] - emptyRowsAbove[minRow]
var multiplier int64
multiplier = 1000000 - 1
rowDistance := maxRow - minRow + emptyRowsBetween
emptyRowsBetween := int64(emptyRowsAbove[maxRow]) - int64(emptyRowsAbove[minRow])
rowDistance := int64(maxRow) - int64(minRow) + emptyRowsBetween*multiplier
maxCol := starA.Col
minCol := starB.Col
@@ -81,13 +84,13 @@ func Run() int {
maxCol, minCol = minCol, maxCol
}
emptyColsBetween := emptyColsToTheLeft[maxCol] - emptyColsToTheLeft[minCol]
emptyColsBetween := int64(emptyColsToTheLeft[maxCol]) - int64(emptyColsToTheLeft[minCol])
colDistance := maxCol - minCol + emptyColsBetween
colDistance := int64(maxCol) - int64(minCol) + emptyColsBetween*multiplier
distance := rowDistance + colDistance
log.Printf("between stars %d %+v and %d %+v distance is %n\n", i, j, starA, starB, distance)
distanceSum += distance
log.Printf("between stars %d %+v and %d %+v distance is %d. emptyColsBetween %d ; emptyRowsBetween %d\n", i, j, starA, starB, distance, emptyColsBetween, emptyRowsBetween)
distanceSum += int64(distance)
}
}