From 65d6c130166710761c1393078115d8de01b2fb4e Mon Sep 17 00:00:00 2001 From: efim Date: Mon, 11 Dec 2023 11:15:19 +0000 Subject: [PATCH] day1, example --- day11/dayEleven.go | 106 +++++++++++++++++++++++++++++++++++++++++++++ day11/example | 10 +++++ main.go | 6 +-- 3 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 day11/dayEleven.go create mode 100644 day11/example diff --git a/day11/dayEleven.go b/day11/dayEleven.go new file mode 100644 index 0000000..c44b613 --- /dev/null +++ b/day11/dayEleven.go @@ -0,0 +1,106 @@ +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 +} diff --git a/day11/example b/day11/example new file mode 100644 index 0000000..986aad4 --- /dev/null +++ b/day11/example @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... diff --git a/main.go b/main.go index f320e06..c9768ea 100644 --- a/main.go +++ b/main.go @@ -3,12 +3,12 @@ package main import ( "log" - "sunshine.industries/aoc2023/day10" + "sunshine.industries/aoc2023/day11" ) func main() { log.Print("> starting run:") - result := day10.Run() - log.Printf("day10 result: %d\n****\n", result) + result := day11.Run() + log.Printf("day11 result: %d\n****\n", result) }