Bladeren bron

day 2 part 2

Abderrahmane Faiz 2 weken geleden
bovenliggende
commit
5bc57ee067
3 gewijzigde bestanden met toevoegingen van 67 en 5 verwijderingen
  1. 4 4
      2/solve_1.go
  2. 62 0
      2/solve_2.go
  3. 1 1
      README.md

+ 4 - 4
2/solve_1.go

@@ -7,7 +7,7 @@ import (
 	"strings"
 )
 
-func is_valid(id int) bool {
+func is_invalid(id int) bool {
 	idstr := strconv.Itoa(id)
 	size_idstr := len(idstr)
 	if (size_idstr % 2) != 0 {
@@ -16,10 +16,10 @@ func is_valid(id int) bool {
 	return idstr[:size_idstr/2] == idstr[size_idstr/2:]
 }
 
-func count_valids(lb, ub int) int{
+func count_invalids(lb, ub int) int{
 	res := 0
 	for i:=lb; i <= ub; i++ {
-		if is_valid(i) {
+		if is_invalid(i) {
 			res += i
 		}
 	}
@@ -38,7 +38,7 @@ func main() {
 		ranges = strings.Split(value,"-")
 		lower_bound, _ = strconv.Atoi(ranges[0])
 		upper_bound, _ = strconv.Atoi(ranges[1])
-		res += count_valids(lower_bound, upper_bound)
+		res += count_invalids(lower_bound, upper_bound)
 	}
 	fmt.Println(res)
 }

+ 62 - 0
2/solve_2.go

@@ -0,0 +1,62 @@
+package main
+
+import (
+	"fmt"
+	"os"
+	"strconv"
+	"strings"
+)
+
+
+func is_kinvalid(id string, k int) bool{
+	size_idstr := len(id)
+	if (size_idstr % k) != 0 {
+		return false
+	}
+
+	l:= size_idstr / k
+	
+	for i:= 0; i+l < size_idstr ; i+=l {
+		if id[i : i+l] != id[i+l : i+2*l]{
+			return false
+		}
+	}
+	return true
+}
+
+func is_invalid(id int) bool {
+	idstr := strconv.Itoa(id)
+	size_idstr := len(idstr)
+	for k:=2; k <= size_idstr; k++{
+		if is_kinvalid(idstr, k) {
+			return true
+		}
+	}	
+	return false
+}
+
+func count_invalids(lb, ub int) int{
+	res := 0
+	for i:=lb; i <= ub; i++ {
+		if is_invalid(i) {
+			res += i
+		}
+	}
+	return res
+}
+
+func main() {
+	fmt.Println("Advent of Code 2025 - Day 2 - Part 2")
+	f, _ := os.ReadFile("2/input")
+	l := strings.Split(string(f), ",")
+	var ranges []string
+	var lower_bound, upper_bound int
+	res:=0
+	for _,value := range l {
+		ranges = strings.Split(value,"-")
+		lower_bound, _ = strconv.Atoi(ranges[0])
+		upper_bound, _ = strconv.Atoi(ranges[1])
+		res += count_invalids(lower_bound, upper_bound)
+	}
+	fmt.Println(res)
+}

+ 1 - 1
README.md

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