Browse Source

complete day 11 part 1

master
Alexander Avery 11 months ago
parent
commit
5722d9106c
  1. 101
      day11.go
  2. 140
      input/11-1
  3. 140
      input/11-2
  4. 1
      main.go
  5. 14
      main_test.go
  6. 7
      parse.go

101
day11.go

@ -0,0 +1,101 @@
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"unicode"
)
type galaxy struct {
val byte // either '#' or '.'
x int
y int
}
func galaxyDistance(g1, g2 galaxy, blankRows, blankColumns []int) int {
maxX, minX := maxmin(g1.x, g2.x)
// g2.y will always be greater than g1.y
diffy := g2.y - g1.y
diffx := maxX - minX
for _, col := range blankColumns {
if maxX > col && minX < col {
diffy++
}
}
for _, row := range blankRows {
if g2.y > row && g1.y < row {
diffx++
}
}
return diffy + diffx
}
type day11 struct{}
func (d day11) solve1(r io.Reader) string {
scanner := bufio.NewScanner(r)
space := make([][]galaxy, 0, 50)
galaxies := make([]galaxy, 0, 50)
blankRows := make([]int, 0, 10)
blankColumns := make([]int, 0, 10)
for y := 0; scanner.Scan(); y++ {
layer := make([]galaxy, 0, 50)
bytes := bytes.TrimFunc(scanner.Bytes(), unicode.IsSpace)
seenGalaxy := false
for x, c := range bytes {
galaxy := galaxy{x: x, y: y, val: c}
layer = append(layer, galaxy)
// only galaxies get tracked in
// the galaxy slice
if c == '#' {
galaxies = append(galaxies, galaxy)
seenGalaxy = true
}
}
space = append(space, layer)
if !seenGalaxy {
blankRows = append(blankRows, y)
}
}
for i := 0; i < len(space[0]); i++ {
seenGalaxy := false
for j := range space {
if space[j][i].val == '#' {
seenGalaxy = true
break
}
}
if !seenGalaxy {
blankColumns = append(blankColumns, i)
}
}
sum := 0
for i := 0; i < len(galaxies)-1; i++ {
for j := i + 1; j < len(galaxies); j++ {
g1 := galaxies[i]
g2 := galaxies[j]
sum += galaxyDistance(g1, g2, blankRows, blankColumns)
}
}
return fmt.Sprintf("%d", sum)
}
func (d day11) solve2(r io.Reader) string {
return "incomplete"
}

140
input/11-1

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

140
input/11-2

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

1
main.go

@ -30,6 +30,7 @@ var (
&day08{},
&day09{},
&day10{},
&day11{},
}
)

14
main_test.go

@ -177,5 +177,19 @@ ZZZ = (ZZZ, ZZZ)`,
LJ...`,
ans1: "8",
},
{
// day 11
inp1: `...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....`,
ans1: "374",
},
}
)

7
parse.go

@ -48,3 +48,10 @@ func reverseSlice[T any](s []T) {
s[i], s[j] = s[j], s[i]
}
}
func maxmin(v1, v2 int) (int, int) {
if v1 > v2 {
return v1, v2
}
return v2, v1
}

Loading…
Cancel
Save