|
|
@@ -0,0 +1,108 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "bufio"
|
|
|
+ "fmt"
|
|
|
+ "os"
|
|
|
+)
|
|
|
+
|
|
|
+type coord struct {
|
|
|
+ x,y,nb_neighbors int
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func update_neighbors_insertion(positions* []*coord, i,j int) int{
|
|
|
+ nbn := 0
|
|
|
+ for _, paper_roll := range *positions {
|
|
|
+ switch(paper_roll.x){
|
|
|
+ case i:
|
|
|
+ switch(paper_roll.y){
|
|
|
+ case j-1, j+1:
|
|
|
+ nbn++
|
|
|
+ paper_roll.nb_neighbors++
|
|
|
+ }
|
|
|
+ case i-1, i+1:
|
|
|
+ switch(paper_roll.y){
|
|
|
+ case j-1,j,j+1:
|
|
|
+ nbn++
|
|
|
+ paper_roll.nb_neighbors++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nbn
|
|
|
+}
|
|
|
+
|
|
|
+func continue_removing(positions* []*coord){
|
|
|
+ for i:=0; i < len(*positions); {
|
|
|
+ paper_roll := (*positions)[i]
|
|
|
+ if paper_roll.nb_neighbors < 4 {
|
|
|
+ (*positions)[i] = (*positions)[len(*positions)-1]
|
|
|
+ (*positions)[len(*positions)-1] = nil
|
|
|
+ *positions = (*positions)[:len(*positions)-1]
|
|
|
+ } else {
|
|
|
+ i++
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ fmt.Println("Advent of Code 2025 - Day 4 - Part 2")
|
|
|
+ f,_ := os.Open("4/input")
|
|
|
+ defer f.Close()
|
|
|
+
|
|
|
+ var positions []*coord
|
|
|
+ scanner := bufio.NewScanner(f)
|
|
|
+ line_num := 0
|
|
|
+ for scanner.Scan() {
|
|
|
+ line := scanner.Text()
|
|
|
+ for i:= 0; i < len(line); i++{
|
|
|
+ if line[i] == '@'{
|
|
|
+ nbn := update_neighbors_insertion(&positions,line_num,i)
|
|
|
+ positions = append(positions, &coord{line_num,i,nbn})
|
|
|
+ }
|
|
|
+ }
|
|
|
+ line_num++
|
|
|
+ }
|
|
|
+
|
|
|
+ paper_rolls_pre := len(positions)
|
|
|
+
|
|
|
+ for {
|
|
|
+ loop_entry_paper_rolls := len(positions)
|
|
|
+ // remove paper_rolls with less than 4 neighbors
|
|
|
+ continue_removing(&positions)
|
|
|
+ update_neighbors_after_removal(&positions)
|
|
|
+ // if nothing was removed break from loop
|
|
|
+ if loop_entry_paper_rolls == len(positions) {
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fmt.Println(paper_rolls_pre - len(positions))
|
|
|
+}
|
|
|
+
|
|
|
+func update_neighbors_after_removal(positions* []*coord){
|
|
|
+ // reset nb_neighbors
|
|
|
+ for _, paper_roll := range *positions{
|
|
|
+ paper_roll.nb_neighbors = 0
|
|
|
+ }
|
|
|
+
|
|
|
+ // update nb_neighbors
|
|
|
+ for _, paper_roll := range *positions{
|
|
|
+ x,y := paper_roll.x, paper_roll.y
|
|
|
+ for _, npar := range *positions {
|
|
|
+ if &npar != &paper_roll{
|
|
|
+ switch(npar.x){
|
|
|
+ case x+1, x-1:
|
|
|
+ switch(npar.y){
|
|
|
+ case y, y+1, y-1:
|
|
|
+ paper_roll.nb_neighbors++
|
|
|
+ }
|
|
|
+ case x:
|
|
|
+ switch(npar.y){
|
|
|
+ case y+1, y-1:
|
|
|
+ paper_roll.nb_neighbors++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|