From 77d53011a1156d383535891cc1e5af3e74bd00cd Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 11 Dec 2023 08:43:32 -0600 Subject: [PATCH] 2023 Day 11 Complete! Added a todo file to track changes to my helpers that need to be propagated through previous days/years --- 2023/day11/input | 140 +++++++++++++++++++++++++++++++++++ 2023/day11/main.go | 72 ++++++++++++++++++ 2023/day11/testinput | 10 +++ helpers/coordinateByteMap.go | 68 ++++++++++++++--- helpers/helpers.go | 1 + todo.md | 4 + 6 files changed, 284 insertions(+), 11 deletions(-) create mode 100644 2023/day11/input create mode 100644 2023/day11/main.go create mode 100644 2023/day11/testinput create mode 100644 todo.md diff --git a/2023/day11/input b/2023/day11/input new file mode 100644 index 0000000..79bead4 --- /dev/null +++ b/2023/day11/input @@ -0,0 +1,140 @@ +......................................#.....................................#......#.......#.....................#.................#........ +............................................................................................................................................ +.............................................#............................................................................................#. +..........................#..................................#........#............................#........................................ +...#...........................#.................#............................................#...............................#............. +....................#.................................#..................................................#.........#........................ +......................................#...............................................................................................#..... +...................................................................................#.............#.......................................... +.......................#.........................................#......................#..................................#.....#.......... +...............#................#...................#.........................#.................................#........................... +.......#..............................................................#..................................................................... +........................................#...................#.............................................#................................. +...........................#.....................#..................................................#..................#.................... +.................#........................................................................#.......................#......................... +............#..................................................#...............................#........................................#... +.#................................#.........................................................................#............................... +.....................................................#...............#....................................................#................. +..........................#...................#....................................#.................#.........................#............ +........#..............................#............................................................................#....................... +........................................................#................#.................................................................. +...................#.............#...............................#.........................................#................................ +........................#...........................#.........................#......#.....................................................# +..#........#.................#....................................................................................................#......... +.............................................#.............#.........................................#............#......................... +...............#......................................................#......................#..........................................#... +............................................................................................................................................ +..................................................................................#........................................#................ +..........................#........#......#..............#.....#................................................................#........... +.........................................................................................#.....#..............#............................. +....................#..........#.....................#................................................................................#..... +.#....................................................................#........#...........................................................# +..........#..............................................................................................................#.................. +............................................................#......................................#........................................ +...............#..........#................................................................................#................................ +........................................................#..................................#.....................................#.......... +......................#................#...............................................................................................#.... +........#.......................................#.........................................................................#................. +.........................................................................#........#..................................#...................... +#..................................#......#............................................................#...........................#........ +............................................................................................................................................ +......#.................................................#..............................#.................................................... +.........................#....................................................#..............................................#..........#... +.................................#.............................................................................#............................ +.................#..................................#................#...........................#.......................................... +..............................................................#......................#......#............................................... +.............#..........................#.................................................................................................#. +...............................#........................#................................................................................... +....................................#.................................................................#..............#...................... +.......#....................................................................#.................#................................#............ +..........................#.................#.....#.................#....................................................#.............#.... +............................................................................................................................................ +............#.............................................................................................#......#................#......... +...#..........................#..........................#......................................#........................................... +............................................................................................................................................ +........#.............#...................................................#................................................................. +...........................................#....................#.................#...........................#......#.........#............ +............................................................................................................................................ +............#....................#.............................................................#......#..................................... +.....................................................................#..................#................................................... +.....................#..............................#........#...............#........................................................#..... +....................................#...................................................................................#................... +..................................................................................................................#......................... +.....#.......#...............#..................................................................#..............................#............ +.............................................#.........#.................................................................................... +..................................................................#.......#................................#................................ +.........................#.................................................................#.......#.................#...................... +#...............................................#.........................................................................#.......#......... +...........................................#.................#................#...........................................................#. +......................#...............#............................................#........................................................ +...............#..............#....................#....................................#.........................#......................... +..#..........................................................................................................#.............................. +..........#...................................................................................#........#.................................... +...........................#.......#.........#.........#..................#................................................................. +................................................................#......................................................#.................... +............................................................................................................................................ +...............................................................................................................................#............ +........#...............................#............#...................................................#........#....................#.... +....................#....................................................................................................................... +............#...................................#....................................#................................#.....#............... +.............................#....................................#...............................#......................................... +...#....................................................#..................#...........................#.................................... +.......................#..............#..................................................#...............................#........#......... +..............#.................#............................................................................#.............................. +........#..........................................#......................................................................................#. +..........................#............................................#.....#.......................................#...................... +...................................#.....................#........#...............#.........#............................................... +...........................................................................................................................#................ +.........................................#.............................................................#...........................#........ +......#.........................#...........................#............#.....#............................................................ +...............#.....#.................................#.......................................................................#............ +..................................................................................................#..............#........................#. +...................................#.......#................................................................................................ +..............................................................#......................#..................#.........................#......... +..............................#.................#.........................................#................................................. +..#......................................................................................................................................... +..................#..................................................#......#......................#........................#............... +...........#...........................................................................................................#..............#..... +.....................................#...................................................................................................... +........................................................#.........................................................#......................... +..........................#....................................#............................................................................ +............................................................................................#...............#............................#.. +#..............................................#...................................#..................#..................................... +.........#.............#..................#.....................................................................#........................... +.............................................................................................................................#.............. +.....................................#...........................#............#..................#.....................................#.... +.................#..........#............................#..............................................................#........#.......... +.........................................................................#...................#.............................................. +.................................#...........................................................................#.....#........................ +........................................#..........................#........................................................................ +...#.....#.................................................#......................................#......#..........................#.....#. +....................................#.............................................#......................................................... +....................#.......#................#..............................#............................................................... +.....................................................................#....................#....................#............................ +.................................................#................................................................................#......... +...........#...........#...................................................................................................................# +................#.......................#.........................#...................................................#..................... +...#.......................#...............................#.........................................#.........................#............ +....................................................................................#....................................................... +.............................................................................................#...........................................#.. +..............#...............................#................................................................#..........#................. +.......................................................#.............#.....#................................................................ +............................#...................................................................#........................................... +.....#..............................#...........................................................................................#..........# +..................................................#...............#................#........................................................ +..........#..........#.......................................................................................#.........#.............#...... +........................................................#....................................................................#.............. +.............................#...................................................................................#.......................#.. +.#.........................................#...................#...........#..........................#..................................... +....................................................................#......................#................................................ +...........#......................................................................#.................................#....................... +.....#.................................................................................#.......................................#............ +...............................#.....#..................................................................#................................... +....................#.........................#......................................................................................#...... +..................................................................................................#...........#........#.................... +...............#.........................................#............#..................................................................... +..................................#......#....................................#............................................................. +...........................#........................#.....................................#..............#...............................#.. +....#..............#..............................................................................................#......................... +.............#...................................................................................#.......................................... +...................................................................................#..................#..................#............#..... diff --git a/2023/day11/main.go b/2023/day11/main.go new file mode 100644 index 0000000..6bb00ae --- /dev/null +++ b/2023/day11/main.go @@ -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) +} diff --git a/2023/day11/testinput b/2023/day11/testinput new file mode 100644 index 0000000..986aad4 --- /dev/null +++ b/2023/day11/testinput @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... diff --git a/helpers/coordinateByteMap.go b/helpers/coordinateByteMap.go index ea3a498..3a92af6 100644 --- a/helpers/coordinateByteMap.go +++ b/helpers/coordinateByteMap.go @@ -7,9 +7,7 @@ import ( ) type CoordByteMap struct { - Field map[Coordinate]byte - Height int - Width int + Field map[Coordinate]byte // The Top-Left-most X/Y TLX, TLY int @@ -34,8 +32,7 @@ func NewCoordByteMap() CoordByteMap { func StringSliceToCoordByteMap(input []string) CoordByteMap { ret := CoordByteMap{ - Field: make(map[Coordinate]byte), - Height: len(input), + Field: make(map[Coordinate]byte), } for y := range input { for x := range input[y] { @@ -45,6 +42,13 @@ func StringSliceToCoordByteMap(input []string) CoordByteMap { 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 { var ret [][]byte 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 } +// 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 // instance of b. 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.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) { delete(m.Field, pos) diff --git a/helpers/helpers.go b/helpers/helpers.go index 6e6e45a..6221247 100644 --- a/helpers/helpers.go +++ b/helpers/helpers.go @@ -23,6 +23,7 @@ const ( FILL_CHAR = "\u2588" CLEAR_SCREEN = "\033[H\033[2J" + CLEAR_LINE = "\033[1K" MAX_INT = int(^uint(0) >> 1) MIN_INT = -MAX_INT - 1 diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..56e85d6 --- /dev/null +++ b/todo.md @@ -0,0 +1,4 @@ +# TODO + +## CoordByteMap Changes +* [ ] Height/Width are functions now