day02
This commit is contained in:
59
2025/go/day02/day02.go
Normal file
59
2025/go/day02/day02.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package day02
|
||||||
|
|
||||||
|
import (
|
||||||
|
"adventofcode2025/utils"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Part1(input string) int {
|
||||||
|
ranges := strings.Split(input, ",")
|
||||||
|
count := 0
|
||||||
|
for _, r := range ranges {
|
||||||
|
min_s, max_s := strings.Split(r, "-")[0], strings.Split(r, "-")[1]
|
||||||
|
min := utils.MustAtoi(min_s)
|
||||||
|
max := utils.MustAtoi(max_s)
|
||||||
|
for i := min; i <= max; i++ {
|
||||||
|
s := fmt.Sprintf("%d", i)
|
||||||
|
if len(s)%2 != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if s[:len(s)/2] != s[len(s)/2:] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
count += utils.MustAtoi(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
func Part2(input string) int {
|
||||||
|
ranges := strings.Split(input, ",")
|
||||||
|
count := 0
|
||||||
|
for _, r := range ranges {
|
||||||
|
min_s, max_s := strings.Split(r, "-")[0], strings.Split(r, "-")[1]
|
||||||
|
min := utils.MustAtoi(min_s)
|
||||||
|
max := utils.MustAtoi(max_s)
|
||||||
|
for i := min; i <= max; i++ {
|
||||||
|
s := fmt.Sprintf("%d", i)
|
||||||
|
for numDigits := 1; numDigits <= len(s)/2; numDigits++ {
|
||||||
|
if len(s)%numDigits != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
found := true
|
||||||
|
for j := 1; j < len(s)/numDigits; j++ {
|
||||||
|
if s[0:numDigits] != s[j*numDigits:(j+1)*numDigits] {
|
||||||
|
found = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if found {
|
||||||
|
count += utils.MustAtoi(s)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
17
2025/go/day02/day02_test.go
Normal file
17
2025/go/day02/day02_test.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package day02
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPart1(t *testing.T) {
|
||||||
|
r := Part1("446443-446449")
|
||||||
|
require.Equal(t, 446446, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPart2(t *testing.T) {
|
||||||
|
r := Part2("222220-222224")
|
||||||
|
require.Equal(t, 222222, r)
|
||||||
|
}
|
||||||
1
2025/go/day02/input.txt
Normal file
1
2025/go/day02/input.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
69810572-69955342,3434061167-3434167492,76756725-76781020,49-147,296131-386620,910523-946587,34308309-34358652,64542-127485,640436-659023,25-45,35313993-35393518,753722181-753795479,1544-9792,256-647,444628-483065,5863911-6054673,6969623908-6969778569,658-1220,12631-63767,670238-830345,1-18,214165106-214245544,3309229-3355697
|
||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
// "strings"
|
// "strings"
|
||||||
// "time"
|
// "time"
|
||||||
"adventofcode2025/day01"
|
"adventofcode2025/day01"
|
||||||
|
"adventofcode2025/day02"
|
||||||
"adventofcode2025/utils"
|
"adventofcode2025/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,9 +23,9 @@ func main() {
|
|||||||
case 1:
|
case 1:
|
||||||
fmt.Printf("part 1: %d\n", day01.Part1(utils.Readfile(d)))
|
fmt.Printf("part 1: %d\n", day01.Part1(utils.Readfile(d)))
|
||||||
fmt.Printf("part 2: %d\n", day01.Part2(utils.Readfile(d)))
|
fmt.Printf("part 2: %d\n", day01.Part2(utils.Readfile(d)))
|
||||||
// case 2:
|
case 2:
|
||||||
// fmt.Printf("part 1: %d\n", day02.Part1(utils.Readfile(d)))
|
fmt.Printf("part 1: %d\n", day02.Part1(utils.Readfile(d)))
|
||||||
// fmt.Printf("part 2: %d\n", day02.Part2(utils.Readfile(d)))
|
fmt.Printf("part 2: %d\n", day02.Part2(utils.Readfile(d)))
|
||||||
// case 3:
|
// case 3:
|
||||||
// fmt.Printf("part 1: %d\n", day03.Part1(utils.Readfile(d)))
|
// fmt.Printf("part 1: %d\n", day03.Part1(utils.Readfile(d)))
|
||||||
// fmt.Printf("part 2: %d\n", day03.Part2(utils.Readfile(d)))
|
// fmt.Printf("part 2: %d\n", day03.Part2(utils.Readfile(d)))
|
||||||
@@ -97,7 +98,7 @@ func main() {
|
|||||||
|
|
||||||
// Reads day from os.Args.
|
// Reads day from os.Args.
|
||||||
func day() int {
|
func day() int {
|
||||||
latest := 0
|
latest := 1
|
||||||
if len(os.Args) == 1 {
|
if len(os.Args) == 1 {
|
||||||
return latest
|
return latest
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user