day1, example

This commit is contained in:
efim 2023-12-11 11:15:19 +00:00
parent 37ee3e99da
commit 65d6c13016
3 changed files with 119 additions and 3 deletions

106
day11/dayEleven.go Normal file
View File

@ -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
}

10
day11/example Normal file
View File

@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....

View File

@ -3,12 +3,12 @@ package main
import ( import (
"log" "log"
"sunshine.industries/aoc2023/day10" "sunshine.industries/aoc2023/day11"
) )
func main() { func main() {
log.Print("> starting run:") log.Print("> starting run:")
result := day10.Run() result := day11.Run()
log.Printf("day10 result: %d\n****\n", result) log.Printf("day11 result: %d\n****\n", result)
} }