package day11 import ( "fmt" "log" "os" "strings" ) func Run() int64 { fmt.Println("hello day 11") filename := "day11/input" 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 int64 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 } var multiplier int64 multiplier = 1000000 - 1 emptyRowsBetween := int64(emptyRowsAbove[maxRow]) - int64(emptyRowsAbove[minRow]) rowDistance := int64(maxRow) - int64(minRow) + emptyRowsBetween*multiplier maxCol := starA.Col minCol := starB.Col if maxCol < minCol { maxCol, minCol = minCol, maxCol } emptyColsBetween := int64(emptyColsToTheLeft[maxCol]) - int64(emptyColsToTheLeft[minCol]) colDistance := int64(maxCol) - int64(minCol) + emptyColsBetween*multiplier distance := rowDistance + colDistance 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) } } // 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 }