mirror of
https://github.com/tiennm99/adventofcode.git
synced 2026-10-13 10:15:30 +00:00
feat(2025): add day2
This commit is contained in:
1 parent
2b4ddffbcf
commit
cb90b2f8de
2 files changed
+90
No files matched your search
@@ -0,0 +1,87 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func isInvalid(num int) bool {
|
||||||
|
s := strconv.Itoa(num)
|
||||||
|
n := len(s)
|
||||||
|
if n%2 != 0 || n < 2 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
half := n / 2
|
||||||
|
return s[:half] == s[half:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func part1() {
|
||||||
|
data, _ := os.ReadFile("input.txt")
|
||||||
|
content := strings.TrimSpace(string(data))
|
||||||
|
ranges := strings.Split(content, ",")
|
||||||
|
sum := 0
|
||||||
|
for _, v := range ranges {
|
||||||
|
arr := strings.Split(v, "-")
|
||||||
|
start, _ := strconv.Atoi(arr[0])
|
||||||
|
end, _ := strconv.Atoi(arr[1])
|
||||||
|
for i := start; i <= end; i++ {
|
||||||
|
if isInvalid(i) {
|
||||||
|
// fmt.Printf("%d\n", i)
|
||||||
|
sum += i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("%d\n", sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
func isRepeating(num int) bool {
|
||||||
|
s := strconv.Itoa(num)
|
||||||
|
n := len(s)
|
||||||
|
if n <= 1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for k := 1; k <= n/2; k++ {
|
||||||
|
if n%k != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
isRepeat := true
|
||||||
|
for i := k; i < n; i++ {
|
||||||
|
if s[i] != s[i%k] {
|
||||||
|
isRepeat = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if isRepeat {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2() {
|
||||||
|
data, _ := os.ReadFile("input.txt")
|
||||||
|
content := strings.TrimSpace(string(data))
|
||||||
|
ranges := strings.Split(content, ",")
|
||||||
|
sum := 0
|
||||||
|
for _, v := range ranges {
|
||||||
|
arr := strings.Split(v, "-")
|
||||||
|
start, _ := strconv.Atoi(arr[0])
|
||||||
|
end, _ := strconv.Atoi(arr[1])
|
||||||
|
for i := start; i <= end; i++ {
|
||||||
|
if isRepeating(i) {
|
||||||
|
// fmt.Printf("%d\n", i)
|
||||||
|
sum += i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Printf("%d\n", sum)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
part1()
|
||||||
|
part2()
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module day2
|
||||||
|
|
||||||
|
go 1.23.4
|
||||||
Reference in new issue
Block a user