day1, example
This commit is contained in:
parent
37ee3e99da
commit
65d6c13016
|
@ -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
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
...#......
|
||||||
|
.......#..
|
||||||
|
#.........
|
||||||
|
..........
|
||||||
|
......#...
|
||||||
|
.#........
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
.......#..
|
||||||
|
#...#.....
|
6
main.go
6
main.go
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue