adventofcode/2023/day11/main.go

73 lines
1.5 KiB
Go
Raw Normal View History

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)
}