107 lines
2.3 KiB
Go
107 lines
2.3 KiB
Go
package day11
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func Run() int {
|
|
fmt.Println("hello day 11")
|
|
|
|
filename := "day11/example"
|
|
bytes, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
panic(fmt.Sprint("cannot read file ", filename))
|
|
}
|
|
|
|
text := strings.TrimSpace( string(bytes) )
|
|
starCoords := make([]StarCoord, 0)
|
|
|
|
lines := strings.Split(text, "\n")
|
|
|
|
nonEmptyRows := make(map[int]any)
|
|
nonEmptyCols := make(map[int]any)
|
|
|
|
for rowNum, line := range lines {
|
|
for colNum, symb := range line {
|
|
if symb == '#' {
|
|
starCoords = append(starCoords, StarCoord{rowNum, colNum})
|
|
nonEmptyCols[colNum] = struct{}{}
|
|
nonEmptyRows[rowNum] = struct{}{}
|
|
}
|
|
}
|
|
}
|
|
|
|
emptyRowsAbove := make([]int, len(lines))
|
|
emptyColsToTheLeft := make([]int, len(lines))
|
|
|
|
for rowNum, _ := range lines {
|
|
if rowNum > 0 {
|
|
emptyRowsAbove[rowNum] = emptyRowsAbove[rowNum-1]
|
|
}
|
|
_, isRowNonempty := nonEmptyRows[rowNum]
|
|
if !isRowNonempty {
|
|
emptyRowsAbove[rowNum] += 1
|
|
}
|
|
}
|
|
|
|
for colNum, _ := range lines[0] {
|
|
if colNum > 0 {
|
|
emptyColsToTheLeft[colNum] = emptyColsToTheLeft[colNum-1]
|
|
}
|
|
_, isColNonempty := nonEmptyCols[colNum]
|
|
if !isColNonempty {
|
|
emptyColsToTheLeft[colNum] += 1
|
|
}
|
|
}
|
|
|
|
var distanceSum int
|
|
for i := 0; i < len(starCoords); i++ {
|
|
for j := i+1; j < len(starCoords); j++ {
|
|
// calc distance between stars i and j
|
|
starA := starCoords[i]
|
|
starB := starCoords[j]
|
|
|
|
maxRow := starA.Row
|
|
minRow := starB.Row
|
|
|
|
if maxRow < minRow {
|
|
maxRow, minRow = minRow, maxRow
|
|
}
|
|
|
|
emptyRowsBetween := emptyRowsAbove[maxRow] - emptyRowsAbove[minRow]
|
|
|
|
rowDistance := maxRow - minRow + emptyRowsBetween
|
|
|
|
maxCol := starA.Col
|
|
minCol := starB.Col
|
|
if maxCol < minCol {
|
|
maxCol, minCol = minCol, maxCol
|
|
}
|
|
|
|
emptyColsBetween := emptyColsToTheLeft[maxCol] - emptyColsToTheLeft[minCol]
|
|
|
|
colDistance := maxCol - minCol + emptyColsBetween
|
|
|
|
distance := rowDistance + colDistance
|
|
log.Printf("between stars %d %+v and %d %+v distance is %n\n", i, j, starA, starB, distance)
|
|
distanceSum += distance
|
|
}
|
|
}
|
|
|
|
// oh, i have list of all stars, i can just iterate over them and
|
|
// only keep rowNums for which there are stars. yeah
|
|
|
|
fmt.Println(starCoords)
|
|
fmt.Println(emptyRowsAbove)
|
|
fmt.Println(emptyColsToTheLeft)
|
|
|
|
return distanceSum
|
|
}
|
|
|
|
type StarCoord struct {
|
|
Row, Col int
|
|
}
|