2023 Day 11 Complete!

Added a todo file to track changes to my helpers that need
to be propagated through previous days/years
This commit is contained in:
Brian Buller 2023-12-11 08:43:32 -06:00
parent be17b84517
commit 77d53011a1
6 changed files with 284 additions and 11 deletions

140
2023/day11/input Normal file
View File

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

72
2023/day11/main.go Normal file
View File

@ -0,0 +1,72 @@
package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
part1(inp)
fmt.Println()
part2(inp)
}
func part1(input []string) {
m := h.StringSliceToCoordByteMap(input)
for y := m.Height() - 1; y >= 0; y-- {
if !m.RowContains(y, '#') {
m.InsertRowAfter(y, '.')
}
}
for x := m.Width() - 1; x >= 0; x-- {
if !m.ColContains(x, '#') {
m.InsertColAfter(x, '.')
}
}
galaxies := m.FindAll('#')
var result int
for i := 0; i < len(galaxies); i++ {
for j := i + 1; j < len(galaxies); j++ {
result += galaxies[i].Distance(galaxies[j])
}
}
fmt.Println("# Part 1")
fmt.Println(result)
}
func part2(input []string) {
m := h.StringSliceToCoordByteMap(input)
expansion := 1000000
galaxies := m.FindAll('#')
for y := m.Height() - 1; y >= 0; y-- {
if !m.RowContains(y, '#') {
// For every '#' coordinate on a row _after_ y, add 1000000 to it's y coord
for i := 0; i < len(galaxies); i++ {
if galaxies[i].Y > y {
galaxies[i].Y += (expansion - 1)
}
}
}
}
for x := m.Width() - 1; x >= 0; x-- {
if !m.ColContains(x, '#') {
// For every '#' coordinate on a col _after_ x, add 1000000 to it's x coord
for i := 0; i < len(galaxies); i++ {
if galaxies[i].X > x {
galaxies[i].X += (expansion - 1)
}
}
}
}
var result int
for i := 0; i < len(galaxies); i++ {
for j := i + 1; j < len(galaxies); j++ {
result += galaxies[i].Distance(galaxies[j])
}
}
fmt.Println("# Part 2")
fmt.Println(result)
}

10
2023/day11/testinput Normal file
View File

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

View File

@ -7,9 +7,7 @@ import (
) )
type CoordByteMap struct { type CoordByteMap struct {
Field map[Coordinate]byte Field map[Coordinate]byte
Height int
Width int
// The Top-Left-most X/Y // The Top-Left-most X/Y
TLX, TLY int TLX, TLY int
@ -34,8 +32,7 @@ func NewCoordByteMap() CoordByteMap {
func StringSliceToCoordByteMap(input []string) CoordByteMap { func StringSliceToCoordByteMap(input []string) CoordByteMap {
ret := CoordByteMap{ ret := CoordByteMap{
Field: make(map[Coordinate]byte), Field: make(map[Coordinate]byte),
Height: len(input),
} }
for y := range input { for y := range input {
for x := range input[y] { for x := range input[y] {
@ -45,6 +42,13 @@ func StringSliceToCoordByteMap(input []string) CoordByteMap {
return ret return ret
} }
func (m *CoordByteMap) Height() int {
return m.BRY - m.TLY
}
func (m *CoordByteMap) Width() int {
return m.BRX - m.TLX
}
func (m *CoordByteMap) ToByteSlices() [][]byte { func (m *CoordByteMap) ToByteSlices() [][]byte {
var ret [][]byte var ret [][]byte
for y := m.TLY; y <= m.BRY; y++ { for y := m.TLY; y <= m.BRY; y++ {
@ -63,6 +67,54 @@ func (m *CoordByteMap) ContainsCoord(c Coordinate) bool {
c.Y >= m.TLY && c.Y <= m.BRY c.Y >= m.TLY && c.Y <= m.BRY
} }
// RowContains searches a row for a byte
func (m *CoordByteMap) RowContains(row int, b byte) bool {
for x := m.TLX; x <= m.BRX; x++ {
if m.Get(Coordinate{X: x, Y: row}) == b {
return true
}
}
return false
}
// ColContains searches a column for a byte
func (m *CoordByteMap) ColContains(col int, b byte) bool {
for y := m.TLY; y <= m.BRY; y++ {
if m.Get(Coordinate{X: col, Y: y}) == b {
return true
}
}
return false
}
// InsertRowAfter grows the map south, moves all rows after 'row' south
// Then fills the 'row'th row with 'fill'
func (m *CoordByteMap) InsertRowAfter(row int, fill byte) {
m.GrowSouth(1, fill)
for y := m.BRY; y > row; y-- {
for x := m.TLX; x <= m.BRX; x++ {
m.Put(Coordinate{X: x, Y: y}, m.Get(Coordinate{X: x, Y: y - 1}))
}
}
for x := m.TLX; x <= m.BRX; x++ {
m.Put(Coordinate{X: x, Y: row}, fill)
}
}
// InsertColAfter grows the map East, moves all cols after 'col' east
// Then fills the 'col'th column with 'fill'
func (m *CoordByteMap) InsertColAfter(col int, fill byte) {
m.GrowEast(1, fill)
for x := m.BRX; x > col; x-- {
for y := m.TLY; y <= m.BRY; y++ {
m.Put(Coordinate{X: x, Y: y}, m.Get(Coordinate{X: x - 1, Y: y}))
}
}
for y := m.TLY; y <= m.BRY; y++ {
m.Put(Coordinate{X: col, Y: y}, fill)
}
}
// FindFirst searches left to right, top to bottom for the first // FindFirst searches left to right, top to bottom for the first
// instance of b. // instance of b.
func (m *CoordByteMap) FindFirst(b byte) (Coordinate, error) { func (m *CoordByteMap) FindFirst(b byte) (Coordinate, error) {
@ -199,12 +251,6 @@ func (m *CoordByteMap) Put(pos Coordinate, val byte) {
m.BRY = pos.Y m.BRY = pos.Y
} }
m.Field[pos] = val m.Field[pos] = val
if pos.X+1 > m.Width {
m.Width = m.BRX - m.TLX + 1
}
if pos.Y+1 > m.Height {
m.Height = m.BRY - m.TLY + 1
}
} }
func (m *CoordByteMap) Delete(pos Coordinate) { func (m *CoordByteMap) Delete(pos Coordinate) {
delete(m.Field, pos) delete(m.Field, pos)

View File

@ -23,6 +23,7 @@ const (
FILL_CHAR = "\u2588" FILL_CHAR = "\u2588"
CLEAR_SCREEN = "\033[H\033[2J" CLEAR_SCREEN = "\033[H\033[2J"
CLEAR_LINE = "\033[1K"
MAX_INT = int(^uint(0) >> 1) MAX_INT = int(^uint(0) >> 1)
MIN_INT = -MAX_INT - 1 MIN_INT = -MAX_INT - 1

4
todo.md Normal file
View File

@ -0,0 +1,4 @@
# TODO
## CoordByteMap Changes
* [ ] Height/Width are functions now