Skip to content

Commit

Permalink
day11p2 and optimize p1
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 11, 2024
1 parent 5500053 commit f07be6e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 23 deletions.
52 changes: 29 additions & 23 deletions day11p1/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,40 @@ import (
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)

arrStr := strings.Split(lines[0], " ")
arr := make([]int, len(arrStr))
for i, s := range arrStr {
arr[i], _ = strconv.Atoi(s)
stones := make(map[int]int)
for _, line := range strings.Split(lines[0], " ") {
stone, _ := strconv.Atoi(line)
stones[stone] = 1
}

for i := 0; i < 25; i++ {
arr = blink(arr)
stones = blink(stones)
}

return len(arr)
total := 0
for _, stone := range stones {
total += stone
}

return total
}

func blink(arr []int) []int {
var result []int
for _, stone := range arr {
stonestr := strconv.Itoa(stone)
if stone == 0 {
result = append(result, 1)
} else if len(stonestr)%2 == 0 {
part1 := stonestr[:len(stonestr)/2]
part2 := stonestr[len(stonestr)/2:]
part1int, _ := strconv.Atoi(part1)
part2int, _ := strconv.Atoi(part2)
result = append(result, part1int, part2int)
} else {
result = append(result, stone*2024)
}
}
return result
func blink(stones map[int]int) map[int]int {
result := make(map[int]int)
for stone, cnt := range stones {
if stone == 0 {
result[1] += cnt
} else {
stonestr := strconv.Itoa(stone)
if len(stonestr)%2 == 0 {
left, _ := strconv.Atoi(stonestr[:len(stonestr)/2])
right, _ := strconv.Atoi(stonestr[len(stonestr)/2:])
result[left] += cnt
result[right] += cnt
} else {
result[stone*2024] += cnt
}
}
}
return result
}
50 changes: 50 additions & 0 deletions day11p2/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package day11p2

import (
"io"
"strconv"
"strings"

"aoc/utils"
)

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)

stones := make(map[int64]int64)
for _, line := range strings.Split(lines[0], " ") {
stone, _ := strconv.ParseInt(line, 10, 64)
stones[stone] = 1
}

for i := 0; i < 75; i++ {
stones = blink(stones)
}

total := int64(0)
for _, stone := range stones {
total += stone
}

return total
}

func blink(stones map[int64]int64) map[int64]int64 {
result := make(map[int64]int64)
for stone, cnt := range stones {
if stone == 0 {
result[1] += cnt
} else {
stonestr := strconv.FormatInt(stone, 10)
if len(stonestr)%2 == 0 {
left, _ := strconv.Atoi(stonestr[:len(stonestr)/2])
right, _ := strconv.Atoi(stonestr[len(stonestr)/2:])
result[int64(left)] += cnt
result[int64(right)] += cnt
} else {
result[stone*2024] += cnt
}
}
}
return result
}
33 changes: 33 additions & 0 deletions day11p2/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day11p2

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `125 17`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int64
}{
{testInput, int64(65601038650482)},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int64)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}

0 comments on commit f07be6e

Please sign in to comment.