Abderrahmane Faiz пре 2 недеља
родитељ
комит
cecf83597f
3 измењених фајлова са 169 додато и 1 уклоњено
  1. 60 0
      4/solve_1.go
  2. 108 0
      4/solve_2.go
  3. 1 1
      README.md

+ 60 - 0
4/solve_1.go

@@ -0,0 +1,60 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+)
+
+type coord struct {
+	x,y,nb_neighbors int
+	
+}
+
+func update_neighbors(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 main() {
+	fmt.Println("Advent of Code 2025 - Day 4 - Part 1")
+	f,_ := os.Open("4/input")
+	defer f.Close()
+
+	var positions []*coord
+	scanner := bufio.NewScanner(f)
+	line_num := 0
+	res := 0
+	for scanner.Scan() {
+		line := scanner.Text()
+		for i:= 0; i < len(line); i++{
+			if line[i] == '@'{
+				nbn := update_neighbors(&positions,line_num,i)
+				positions = append(positions, &coord{line_num,i,nbn})
+			}
+		}
+		line_num++
+	}
+
+	for _, paper_roll := range positions {
+		if paper_roll.nb_neighbors < 4 {res ++}
+	}
+	
+	fmt.Println(res)
+}

+ 108 - 0
4/solve_2.go

@@ -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++
+					}
+				}
+			}
+		}
+	}
+}

+ 1 - 1
README.md

@@ -23,7 +23,7 @@ Each day's folder is named after the day number (`1` to `12`). Inside each folde
 | 1   | ✅     | ✅     |
 | 2   | ✅     | ✅     |
 | 3   | ✅     | ✅     |
-| 4   | ⬜     | ⬜     |
+| 4   | ✅     | ✅     |
 | 5   | ⬜     | ⬜     |
 | 6   | ⬜     | ⬜     |
 | 7   | ⬜     | ⬜     |