solve_1.go 994 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. )
  7. type coord struct {
  8. x,y,nb_neighbors int
  9. }
  10. func update_neighbors(positions* []*coord, i,j int) int{
  11. nbn := 0
  12. for _, paper_roll := range *positions {
  13. switch(paper_roll.x){
  14. case i:
  15. switch(paper_roll.y){
  16. case j-1, j+1:
  17. nbn++
  18. paper_roll.nb_neighbors++
  19. }
  20. case i-1, i+1:
  21. switch(paper_roll.y){
  22. case j-1,j,j+1:
  23. nbn++
  24. paper_roll.nb_neighbors++
  25. }
  26. }
  27. }
  28. return nbn
  29. }
  30. func main() {
  31. fmt.Println("Advent of Code 2025 - Day 4 - Part 1")
  32. f,_ := os.Open("4/input")
  33. defer f.Close()
  34. var positions []*coord
  35. scanner := bufio.NewScanner(f)
  36. line_num := 0
  37. res := 0
  38. for scanner.Scan() {
  39. line := scanner.Text()
  40. for i:= 0; i < len(line); i++{
  41. if line[i] == '@'{
  42. nbn := update_neighbors(&positions,line_num,i)
  43. positions = append(positions, &coord{line_num,i,nbn})
  44. }
  45. }
  46. line_num++
  47. }
  48. for _, paper_roll := range positions {
  49. if paper_roll.nb_neighbors < 4 {res ++}
  50. }
  51. fmt.Println(res)
  52. }