This commit is contained in:
Karl Spickermann 2022-12-18 19:39:49 +01:00
parent 83834436a1
commit 367e5eda0f
5 changed files with 2272 additions and 11 deletions

View File

@ -4,7 +4,6 @@ import (
"AOC2022/helper"
"fmt"
"os"
"strconv"
"strings"
)
@ -110,19 +109,11 @@ func getPoints(lines []string) [][2][2]int {
for i, line := range lines {
intPair := [2][2]int{}
stringParts := strings.Split(line[12:], ":")
sensorStringNumbers := stringSliceToIntSlice(strings.Split(stringParts[0], ", y="))
beaconStringNumbers := stringSliceToIntSlice(strings.Split(strings.ReplaceAll(stringParts[1], " closest beacon is at x=", ""), ", y="))
sensorStringNumbers := helper.StringSliceToIntSlice(strings.Split(stringParts[0], ", y="))
beaconStringNumbers := helper.StringSliceToIntSlice(strings.Split(strings.ReplaceAll(stringParts[1], " closest beacon is at x=", ""), ", y="))
intPair[0] = [2]int{sensorStringNumbers[0], sensorStringNumbers[1]}
intPair[1] = [2]int{beaconStringNumbers[0], beaconStringNumbers[1]}
points[i] = intPair
}
return points
}
func stringSliceToIntSlice(input []string) []int {
intSlice := []int{}
for _, str := range input {
intSlice = append(intSlice, helper.RemoveError(strconv.Atoi(str)))
}
return intSlice
}

158
day18/day18.go Normal file
View File

@ -0,0 +1,158 @@
package main
import (
"AOC2022/helper"
"fmt"
"os"
"strings"
)
func main() {
args := os.Args[1:]
lines := helper.ReadTextFile(args[0])
points := make(map[[3]int]bool, len(lines))
for _, line := range lines {
point := [3]int{}
for j, number := range helper.StringSliceToIntSlice(strings.Split(line, ",")) {
point[j] = number
}
points[point] = true
}
fmt.Println(removeOpenSurfacePointsWhichHaveNoConnectionToOutside(points))
same := false
pastLenPoints := len(points)
for !same {
removeOpenSurfacePointsWhichHaveNoConnectionToOutside(points)
if pastLenPoints != len(points) {
pastLenPoints = len(points)
} else {
same = true
}
}
openSurfacesSum := 0
for point, _ := range points {
openSurfacesPoint := 6
neighbors := getNeighbors(point)
for _, neighbor := range neighbors {
if points[neighbor] {
openSurfacesPoint--
}
}
openSurfacesSum += openSurfacesPoint
}
fmt.Println(openSurfacesSum)
}
func removeOpenSurfacePointsWhichHaveNoConnectionToOutside(points map[[3]int]bool) int {
openSurfacesSum := 0
openSurfacesPoints := make(map[[3]int]bool)
for point, _ := range points {
openSurfacesPoint := 6
neighbors := getNeighbors(point)
for _, neighbor := range neighbors {
if points[neighbor] {
openSurfacesPoint--
} else {
openSurfacesPoints[neighbor] = true
}
}
openSurfacesSum += openSurfacesPoint
}
for point, _ := range openSurfacesPoints {
if !checkIfPathToOutSideExists(points, point) {
points[point] = true
}
}
return openSurfacesSum
}
func getNeighbors(point [3]int) (neighbors [6][3]int) {
directions := [][3]int{{0, -1, 0}, {+1, 0, 0}, {0, 1, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}}
for i, direction := range directions {
neighbors[i] = [3]int{point[0] + direction[0], point[1] + direction[1], point[2] + direction[2]}
}
return
}
func checkIfPathToOutSideExists(points map[[3]int]bool, startPoint [3]int) bool {
activePoints := make(map[[3]int]bool)
pointsCopy := deepCopyMap(points)
activePoints[startPoint] = true
foundAWay := false
areaLimits := getAreaLimits(points)
for !foundAWay && len(activePoints) > 0 {
step(&pointsCopy, &activePoints, &foundAWay, areaLimits)
}
return foundAWay
}
func step(points *map[[3]int]bool, activePoints *map[[3]int]bool, foundAWay *bool, areaLimits [6]int) {
activePoint := get_some_key(*activePoints)
delete(*activePoints, activePoint)
if (activePoint[0] > areaLimits[0] ||
activePoint[0] < areaLimits[1]) &&
(activePoint[1] > areaLimits[2] ||
activePoint[1] < areaLimits[3]) &&
(activePoint[2] > areaLimits[4] ||
activePoint[2] < areaLimits[5]) {
*foundAWay = true
return
}
(*points)[activePoint] = true
neighbors := getNeighbors(activePoint)
for _, neighbor := range neighbors {
if !(*points)[neighbor] {
(*activePoints)[neighbor] = true
}
}
}
func getAreaLimits(points map[[3]int]bool) [6]int {
highestX := -1
lowestX := 999999
highestY := -1
lowestY := 999999
highestZ := -1
lowestZ := 999999
for point, _ := range points {
if point[0] > highestX {
highestX = point[0]
}
if point[0] < lowestX {
lowestX = point[0]
}
if point[1] > highestY {
highestY = point[1]
}
if point[1] < lowestY {
lowestY = point[1]
}
if point[2] > highestZ {
highestZ = point[2]
}
if point[2] < lowestZ {
lowestZ = point[2]
}
}
return [6]int{highestX, lowestX, highestY, lowestY, highestZ, lowestZ}
}
func get_some_key[T any](m map[[3]int]T) [3]int {
for k := range m {
return k
}
return [3]int{-1, -1, -1}
}
func deepCopyMap(points map[[3]int]bool) map[[3]int]bool {
tmpValves := make(map[[3]int]bool)
for k, v := range points {
tmpValves[k] = v
}
return tmpValves
}

2090
day18/input Normal file

File diff suppressed because it is too large Load Diff

13
day18/testinput Normal file
View File

@ -0,0 +1,13 @@
2,2,2
1,2,2
3,2,2
2,1,2
2,3,2
2,2,1
2,2,3
2,2,4
2,2,6
1,2,5
3,2,5
2,1,5
2,3,5

View File

@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
)
@ -144,3 +145,11 @@ func Abs[T constraints.Integer](x T) T {
func ManHattanDistance(p1, p2 [2]int) int {
return Abs(p1[0]-p2[0]) + Abs(p1[1]-p2[1])
}
func StringSliceToIntSlice(input []string) []int {
intSlice := []int{}
for _, str := range input {
intSlice = append(intSlice, RemoveError(strconv.Atoi(str)))
}
return intSlice
}