diff --git a/day11.go b/day11.go new file mode 100644 index 0000000..612d491 --- /dev/null +++ b/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" +} diff --git a/input/11-1 b/input/11-1 new file mode 100644 index 0000000..2a3748e --- /dev/null +++ b/input/11-1 @@ -0,0 +1,140 @@ +.................................#.....................................................#..............................................#..... +..........#.........#..........................................#............................................................................ +..........................................................#.......................................................#............#............ +..#.................................#........................................#.............#.........#...................#.................. +..............#............................#..........#.........................................#..........#................................ +.........................#......................#........................#.................................................................. +.......................................#....................#............................................................................... +..............................#.................................................................................................#........... +...................#..............................................#...............#.....................#.........#........................# +..#......................................................#.................................................................................. +.....................................#...................................................#.........#......................#................. +.......#...................................#.......#...................#......#............................................................. +.....................#.....................................................................................#..................#.......#..... +............................................................................................#............................................... +..........................#............#.................................................................................................... +...................................................................................#..................#..........#...............#.......... +#........................................................................................#............................#..................... +........#...........#...............#.............................#...........#.................#....................................#...... +..............................#.............................#............................................................................... +.................................................#.......................................................................................... +.....#............................................................................#......................#......................#..........# +................#..................................................................................#........................................ +.........................................................................#..................#..............................#................ +..#..............................................................#.......................................................................... +.........................#...................................................................................#..........................#... +................................#......................................................................................#.................... +.........#................................................#............#.................................................................... +................#............................................................................#..........#................................... +.......................................#...........#...........................................................#...........................# +....................................................................................................................................#....... +...............................................................................#.............................................#.............. +.....................#............................................................................................#......................... +.............................#.......................................................#........#..........#.............#.................... +....................................#....................................................................................................... +.........................................#........#............#........#.................#................................................. +#.....................................................................................................................................#..... +.......................#.......#............................................................................................................ +........................................................#.......................#...........................#............................... +...............#.....................................................................................#.................#.................... +....#......................#....................................................................................................#........... +...................................#....................................#.................................................................#. +...........#...................................#.....#..............................#.............#.................#...............#....... +........................................#................................................................................................... +....................#..........#..............................................................#............................................. +......#...................................................................................................................#................. +.#.............................................................................................................................#............ +............................................................................................................................................ +.............#............#.................................................................#............................................... +...............................................................#.......................#.................................................... +.............................................#..........#................#................................................................#. +............................................................................................................................................ +.#.............................#.......................................................................#.............#.........#............ +.............................................................#.....#.............#...............#.......................................... +......#..............#..............#....................................................................................................... +.........................................................#.................................................................................. +..............#.........................#.......................#........................................................................... +................................#.................#............................................................#...................#........ +.............................................................................................#.............................................. +............................................................................................................................................ +........................#.................#.......................#..................#...................................................... +..#.......................................................................................#................#.................#........#..... +.........#........#................#...............#.........#....................................#......................................... +............................................................................................................................................ +........................................#..............................#.......................................#......#..........#.......... +............................................................................................................................................ +......................#......................................................#...........................#.................................. +..#............#................#.......................#.....#...................................................#......................... +............................................................................................................................................ +............................#...............................................................#........#...................................... +............................................#...................................#......#..........................................#......... +...........#......................#..................#....................#....................................#............................ +....................#..............................................#...................................................#.................... +...........................................................................................................................................# +...#....................................#...............#.....#.....................#....................................................... +...............#.......#........................#...............................................#......#..............................#..... +.............................................................................................................................#.............. +........................................................................................................................#................... +......#...............................................#........................#.................................................#.......... +....................................................................................................................#....................#.. +..#..........................................................#.........#...............#.....................#.............................. +.....................#............................#.........................................#.....#......................................... +........#..........................#......#..............#...............................................................#...........#...... +.............#....................................................................................................#......................... +................................................................#........................#.................................................. +.........................................................................#............................#.......#.................#........... +............................................................................................................................................ +.....................#...................#..................#.......#.................#.....#.............#............#.................... +.......#......................................#.......#..................................................................................... +............................................................................................................................................ +...............................#...............................#...................................................#...................#.... +............................................................................................................................................ +.....#.................................#...............................................#....................#................#.............. +.................................................#......#................................................................................... +......................................................................#............................................................#........ +.........#........#.......#........#................................................................................#....................... +..............................................#............................................................................................. +..#......................................#............#...........#...........................#...........................................#. +...............#............................................................................................................................ +...........................................................................................................#................................ +.............................#..........................................#................#.................................................. +.....#......................................#..............#........................#...............................#.....#.............#... +.......................#.............#...............#...................................................................................... +..................#.............#.................................#..........#.............................................................. +................................................#.......................................................#........#.............#...........# +...#.......................#................................................................................................................ +..............#............................#...............................................................................#................ +........................................................#..............................#..........#..................#...................... +..............................................................#...................#........................#................................ +................................#........................................................................................................... +..#..............#.............................#...................#.........#....................................#............#............ +...........#............................................................................................................................#... +......#............................................#...................#.................................#.................................. +................................................................................#............#......#....................................... +....................#.......................#........................................#....................................#.........#....... +...............#.....................#...................................................................................................... +.............................#..................................................................................#........................... +...#...................................................#..............#.........................#.......#................................... +...............................................................................#............................................................ +.........................#.................................................................#.......................................#........ +..............................................................#...............................................................#............. +........#............#.............#.........#.......#...............................................................#...................... +.......................................................................#...............#...........#........................................ +....#........#................#.........#.......................................#........................#.................................. +..............................................................................................................#.............#............... +............................................................................................................................................ +.........................#.....................................#..........#.......................................................#......... +.........#...................................#.............................................................................................. +.......................................#..........#.........................................................#............................... +.#............#............................................#..........#................#...........................#........................ +............................................................................................................................................ +........................................................................................................#...............................#... +......#............................#...........................#..............................#..................................#.......... +......................#..........................................................................................#......#................... +............................................#.....#......................#...........................#...................................... +...............#............#............................#.........................#........................................................ +........................................#............................................................................#...................... +........#...........................................................#...................#......................#..................#......... +........................#.............................#............................................#........................................ +...............................#.............#..............#.............................................................#................. +.................#............................................................................#.........................................#... \ No newline at end of file diff --git a/input/11-2 b/input/11-2 new file mode 100644 index 0000000..2a3748e --- /dev/null +++ b/input/11-2 @@ -0,0 +1,140 @@ +.................................#.....................................................#..............................................#..... +..........#.........#..........................................#............................................................................ +..........................................................#.......................................................#............#............ +..#.................................#........................................#.............#.........#...................#.................. +..............#............................#..........#.........................................#..........#................................ +.........................#......................#........................#.................................................................. +.......................................#....................#............................................................................... +..............................#.................................................................................................#........... +...................#..............................................#...............#.....................#.........#........................# +..#......................................................#.................................................................................. +.....................................#...................................................#.........#......................#................. +.......#...................................#.......#...................#......#............................................................. +.....................#.....................................................................................#..................#.......#..... +............................................................................................#............................................... +..........................#............#.................................................................................................... +...................................................................................#..................#..........#...............#.......... +#........................................................................................#............................#..................... +........#...........#...............#.............................#...........#.................#....................................#...... +..............................#.............................#............................................................................... +.................................................#.......................................................................................... +.....#............................................................................#......................#......................#..........# +................#..................................................................................#........................................ +.........................................................................#..................#..............................#................ +..#..............................................................#.......................................................................... +.........................#...................................................................................#..........................#... +................................#......................................................................................#.................... +.........#................................................#............#.................................................................... +................#............................................................................#..........#................................... +.......................................#...........#...........................................................#...........................# +....................................................................................................................................#....... +...............................................................................#.............................................#.............. +.....................#............................................................................................#......................... +.............................#.......................................................#........#..........#.............#.................... +....................................#....................................................................................................... +.........................................#........#............#........#.................#................................................. +#.....................................................................................................................................#..... +.......................#.......#............................................................................................................ +........................................................#.......................#...........................#............................... +...............#.....................................................................................#.................#.................... +....#......................#....................................................................................................#........... +...................................#....................................#.................................................................#. +...........#...................................#.....#..............................#.............#.................#...............#....... +........................................#................................................................................................... +....................#..........#..............................................................#............................................. +......#...................................................................................................................#................. +.#.............................................................................................................................#............ +............................................................................................................................................ +.............#............#.................................................................#............................................... +...............................................................#.......................#.................................................... +.............................................#..........#................#................................................................#. +............................................................................................................................................ +.#.............................#.......................................................................#.............#.........#............ +.............................................................#.....#.............#...............#.......................................... +......#..............#..............#....................................................................................................... +.........................................................#.................................................................................. +..............#.........................#.......................#........................................................................... +................................#.................#............................................................#...................#........ +.............................................................................................#.............................................. +............................................................................................................................................ +........................#.................#.......................#..................#...................................................... +..#.......................................................................................#................#.................#........#..... +.........#........#................#...............#.........#....................................#......................................... +............................................................................................................................................ +........................................#..............................#.......................................#......#..........#.......... +............................................................................................................................................ +......................#......................................................#...........................#.................................. +..#............#................#.......................#.....#...................................................#......................... +............................................................................................................................................ +............................#...............................................................#........#...................................... +............................................#...................................#......#..........................................#......... +...........#......................#..................#....................#....................................#............................ +....................#..............................................#...................................................#.................... +...........................................................................................................................................# +...#....................................#...............#.....#.....................#....................................................... +...............#.......#........................#...............................................#......#..............................#..... +.............................................................................................................................#.............. +........................................................................................................................#................... +......#...............................................#........................#.................................................#.......... +....................................................................................................................#....................#.. +..#..........................................................#.........#...............#.....................#.............................. +.....................#............................#.........................................#.....#......................................... +........#..........................#......#..............#...............................................................#...........#...... +.............#....................................................................................................#......................... +................................................................#........................#.................................................. +.........................................................................#............................#.......#.................#........... +............................................................................................................................................ +.....................#...................#..................#.......#.................#.....#.............#............#.................... +.......#......................................#.......#..................................................................................... +............................................................................................................................................ +...............................#...............................#...................................................#...................#.... +............................................................................................................................................ +.....#.................................#...............................................#....................#................#.............. +.................................................#......#................................................................................... +......................................................................#............................................................#........ +.........#........#.......#........#................................................................................#....................... +..............................................#............................................................................................. +..#......................................#............#...........#...........................#...........................................#. +...............#............................................................................................................................ +...........................................................................................................#................................ +.............................#..........................................#................#.................................................. +.....#......................................#..............#........................#...............................#.....#.............#... +.......................#.............#...............#...................................................................................... +..................#.............#.................................#..........#.............................................................. +................................................#.......................................................#........#.............#...........# +...#.......................#................................................................................................................ +..............#............................#...............................................................................#................ +........................................................#..............................#..........#..................#...................... +..............................................................#...................#........................#................................ +................................#........................................................................................................... +..#..............#.............................#...................#.........#....................................#............#............ +...........#............................................................................................................................#... +......#............................................#...................#.................................#.................................. +................................................................................#............#......#....................................... +....................#.......................#........................................#....................................#.........#....... +...............#.....................#...................................................................................................... +.............................#..................................................................................#........................... +...#...................................................#..............#.........................#.......#................................... +...............................................................................#............................................................ +.........................#.................................................................#.......................................#........ +..............................................................#...............................................................#............. +........#............#.............#.........#.......#...............................................................#...................... +.......................................................................#...............#...........#........................................ +....#........#................#.........#.......................................#........................#.................................. +..............................................................................................................#.............#............... +............................................................................................................................................ +.........................#.....................................#..........#.......................................................#......... +.........#...................................#.............................................................................................. +.......................................#..........#.........................................................#............................... +.#............#............................................#..........#................#...........................#........................ +............................................................................................................................................ +........................................................................................................#...............................#... +......#............................#...........................#..............................#..................................#.......... +......................#..........................................................................................#......#................... +............................................#.....#......................#...........................#...................................... +...............#............#............................#.........................#........................................................ +........................................#............................................................................#...................... +........#...........................................................#...................#......................#..................#......... +........................#.............................#............................................#........................................ +...............................#.............#..............#.............................................................#................. +.................#............................................................................#.........................................#... \ No newline at end of file diff --git a/main.go b/main.go index fae2481..de8ae75 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ var ( &day08{}, &day09{}, &day10{}, + &day11{}, } ) diff --git a/main_test.go b/main_test.go index 509a5a2..61b1578 100644 --- a/main_test.go +++ b/main_test.go @@ -177,5 +177,19 @@ ZZZ = (ZZZ, ZZZ)`, LJ...`, ans1: "8", }, + { + // day 11 + inp1: `...#...... + .......#.. + #......... + .......... + ......#... + .#........ + .........# + .......... + .......#.. + #...#.....`, + ans1: "374", + }, } ) diff --git a/parse.go b/parse.go index 19e2a1e..5b99225 100644 --- a/parse.go +++ b/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 +}