solve_2.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. "strings"
  7. )
  8. func is_kinvalid(id string, k int) bool{
  9. size_idstr := len(id)
  10. if (size_idstr % k) != 0 {
  11. return false
  12. }
  13. l:= size_idstr / k
  14. for i:= 0; i+l < size_idstr ; i+=l {
  15. if id[i : i+l] != id[i+l : i+2*l]{
  16. return false
  17. }
  18. }
  19. return true
  20. }
  21. func is_invalid(id int) bool {
  22. idstr := strconv.Itoa(id)
  23. size_idstr := len(idstr)
  24. for k:=2; k <= size_idstr; k++{
  25. if is_kinvalid(idstr, k) {
  26. return true
  27. }
  28. }
  29. return false
  30. }
  31. func count_invalids(lb, ub int) int{
  32. res := 0
  33. for i:=lb; i <= ub; i++ {
  34. if is_invalid(i) {
  35. res += i
  36. }
  37. }
  38. return res
  39. }
  40. func main() {
  41. fmt.Println("Advent of Code 2025 - Day 2 - Part 2")
  42. f, _ := os.ReadFile("2/input")
  43. l := strings.Split(string(f), ",")
  44. var ranges []string
  45. var lower_bound, upper_bound int
  46. res:=0
  47. for _,value := range l {
  48. ranges = strings.Split(value,"-")
  49. lower_bound, _ = strconv.Atoi(ranges[0])
  50. upper_bound, _ = strconv.Atoi(ranges[1])
  51. res += count_invalids(lower_bound, upper_bound)
  52. }
  53. fmt.Println(res)
  54. }