fix missing
This commit is contained in:
76
2024/go/day11/day11.go
Normal file
76
2024/go/day11/day11.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package day11
|
||||
|
||||
import (
|
||||
"adventofcode2024/utils"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Part1(input string) int {
|
||||
stones := strings.Fields(input)
|
||||
for i := 0; i < 25; i++ {
|
||||
n_stones := []string{}
|
||||
for _, stone := range stones {
|
||||
switch {
|
||||
case stone == "0":
|
||||
n_stones = append(n_stones, "1")
|
||||
case len(stone)%2 == 0:
|
||||
d1 := strconv.Itoa(utils.MustAtoi(stone[0 : len(stone)/2]))
|
||||
d2 := strconv.Itoa(utils.MustAtoi(stone[len(stone)/2:]))
|
||||
n_stones = append(n_stones, d1)
|
||||
n_stones = append(n_stones, d2)
|
||||
|
||||
default:
|
||||
n_stones = append(n_stones, fmt.Sprintf("%d", utils.MustAtoi(stone)*2024))
|
||||
}
|
||||
}
|
||||
stones = n_stones
|
||||
}
|
||||
return len(stones)
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
stones := strings.Fields(input)
|
||||
smap := make(map[string]int)
|
||||
smap2 := make(map[string]int)
|
||||
|
||||
for _, stone := range stones {
|
||||
smap[stone] = 1
|
||||
}
|
||||
for i:=0;i<75;i++ {
|
||||
for k, v := range smap {
|
||||
switch {
|
||||
case k == "0":
|
||||
smap2["1"] = smap2["1"] + v
|
||||
case len(k)%2 == 0:
|
||||
d1 := strconv.Itoa(utils.MustAtoi(k[0 : len(k)/2]))
|
||||
d2 := strconv.Itoa(utils.MustAtoi(k[len(k)/2:]))
|
||||
smap2[d1] += v
|
||||
smap2[d2] += v
|
||||
default:
|
||||
k2 := fmt.Sprintf("%d", utils.MustAtoi(k)*2024)
|
||||
smap2[k2] += v
|
||||
}
|
||||
}
|
||||
smap = CopyMap(smap2)
|
||||
smap2 = make(map[string]int)
|
||||
}
|
||||
count := 0
|
||||
for _, v := range smap {
|
||||
count += v
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func CopyMap(original map[string]int) map[string]int {
|
||||
// Create a new map to hold the copy
|
||||
copied := make(map[string]int)
|
||||
|
||||
// Copy each key-value pair from the original map to the new map
|
||||
for key, value := range original {
|
||||
copied[key] = value
|
||||
}
|
||||
|
||||
return copied
|
||||
}
|
||||
17
2024/go/day11/day11_test.go
Normal file
17
2024/go/day11/day11_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package day11
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1("125 17")
|
||||
require.Equal(t, 55312, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2("125 17")
|
||||
require.Equal(t, 55312, r)
|
||||
}
|
||||
1
2024/go/day11/input.txt
Normal file
1
2024/go/day11/input.txt
Normal file
@@ -0,0 +1 @@
|
||||
3 386358 86195 85 1267 3752457 0 741
|
||||
Reference in New Issue
Block a user