|
@@ -14,6 +14,7 @@ type Object struct {
|
|
zRealMin, zRealMax int
|
|
zRealMin, zRealMax int
|
|
supports []*Object
|
|
supports []*Object
|
|
standsOn []*Object
|
|
standsOn []*Object
|
|
|
|
+ canBeDeleted bool
|
|
}
|
|
}
|
|
|
|
|
|
func readInput(file *os.File) []Object {
|
|
func readInput(file *os.File) []Object {
|
|
@@ -81,6 +82,7 @@ func part1(objects []Object) int {
|
|
process(objects)
|
|
process(objects)
|
|
for i := range objects {
|
|
for i := range objects {
|
|
if len(objects[i].supports) == 0 {
|
|
if len(objects[i].supports) == 0 {
|
|
|
|
+ objects[i].canBeDeleted = true
|
|
result++
|
|
result++
|
|
continue
|
|
continue
|
|
}
|
|
}
|
|
@@ -94,6 +96,7 @@ func part1(objects []Object) int {
|
|
}
|
|
}
|
|
|
|
|
|
if canBeDeleted {
|
|
if canBeDeleted {
|
|
|
|
+ objects[i].canBeDeleted = true
|
|
result++
|
|
result++
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -101,6 +104,37 @@ func part1(objects []Object) int {
|
|
return result
|
|
return result
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func countFallen(object *Object, fallen map[*Object]bool) int {
|
|
|
|
+ var count int
|
|
|
|
+ ok, _ := fallen[object]
|
|
|
|
+ if !ok {
|
|
|
|
+ fallen[object] = true
|
|
|
|
+ count++
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for i := range object.supports {
|
|
|
|
+ count += countFallen(object.supports[i], fallen)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return count
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func part2(objects []Object) int {
|
|
|
|
+ var result int
|
|
|
|
+ for i := range objects {
|
|
|
|
+ if objects[i].canBeDeleted {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ fallen := make(map[*Object]bool)
|
|
|
|
+ for j := range objects[i].supports {
|
|
|
|
+ result += countFallen(objects[i].supports[j], fallen)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result
|
|
|
|
+}
|
|
|
|
+
|
|
func main() {
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
if len(os.Args) < 2 {
|
|
log.Fatal("You need to specify a file!")
|
|
log.Fatal("You need to specify a file!")
|
|
@@ -115,4 +149,5 @@ func main() {
|
|
|
|
|
|
objects := readInput(file)
|
|
objects := readInput(file)
|
|
fmt.Println("Part1:", part1(objects))
|
|
fmt.Println("Part1:", part1(objects))
|
|
|
|
+ fmt.Println("Part2:", part2(objects))
|
|
}
|
|
}
|