solve_1.go 796 B

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