Skip to content

Commit

Permalink
day 11 p1
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 11, 2024
1 parent 0fd2c49 commit 5500053
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
44 changes: 44 additions & 0 deletions day11p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package day11p1

import (
"io"
"strconv"
"strings"

"aoc/utils"
)

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)
}

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

return len(arr)
}

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
}
33 changes: 33 additions & 0 deletions day11p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package day11p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `125 17`

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

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

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

result := Solve(r).(int)

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

0 comments on commit 5500053

Please sign in to comment.