Finished day 11
This commit is contained in:
parent
ca331c600e
commit
9dbd04b40f
5 changed files with 134 additions and 0 deletions
86
2024/day11/day11.go
Normal file
86
2024/day11/day11.go
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
package day11
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func update(stones map[int]int) map[int]int {
|
||||
processed := make(map[int]int)
|
||||
for stone, count := range stones {
|
||||
if stone == 0 {
|
||||
processed[1] += count
|
||||
continue
|
||||
}
|
||||
|
||||
places := int(math.Log10(float64(stone))) + 1
|
||||
if places%2 == 0 {
|
||||
half := places / 2
|
||||
divisor := int(math.Pow10(half))
|
||||
|
||||
left := stone / divisor
|
||||
right := stone % divisor
|
||||
|
||||
processed[left] += count
|
||||
processed[right] += count
|
||||
continue
|
||||
}
|
||||
|
||||
processed[stone*2024] += count
|
||||
}
|
||||
|
||||
return processed
|
||||
}
|
||||
|
||||
func Part1(stones map[int]int, steps int) int {
|
||||
for i := 0; i < steps; i++ {
|
||||
stones = update(stones)
|
||||
}
|
||||
|
||||
sum := 0
|
||||
for _, count := range stones {
|
||||
sum += count
|
||||
}
|
||||
|
||||
return sum
|
||||
}
|
||||
|
||||
func Part2(stones map[int]int, steps int) int {
|
||||
return Part1(stones, steps)
|
||||
}
|
||||
|
||||
func Run(filename string) (int, int) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
stones := make(map[int]int)
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
for _, c := range strings.Split(line, " ") {
|
||||
id, err := strconv.Atoi(c)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
stones[id] += 1
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
part1 := Part1(stones, 25)
|
||||
part2 := Part2(stones, 75)
|
||||
|
||||
return part1, part2
|
||||
}
|
||||
44
2024/day11/day11_test.go
Normal file
44
2024/day11/day11_test.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package day11
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDay11(t *testing.T) {
|
||||
filename := "test.txt"
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
t.Fatalf("test file does not exist: %v", filename)
|
||||
}
|
||||
|
||||
part1, part2 := Run(filename)
|
||||
|
||||
expectedPart1 := 55312
|
||||
if part1 != expectedPart1 {
|
||||
t.Fatalf("unexpected part1:\nwant:\t%d\ngot:\t%d", expectedPart1, part1)
|
||||
}
|
||||
|
||||
expectedPart2 := 65601038650482
|
||||
if part2 != expectedPart2 {
|
||||
t.Fatalf("unexpected part2:\nwant:\t%d\ngot:\t%d", expectedPart2, part2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDay11Solution(t *testing.T) {
|
||||
filename := "input.txt"
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
t.Fatalf("test file does not exist: %v", filename)
|
||||
}
|
||||
|
||||
part1, part2 := Run(filename)
|
||||
|
||||
expectedPart1 := 197157
|
||||
if part1 != expectedPart1 {
|
||||
t.Fatalf("unexpected part1:\nwant:\t%d\ngot:\t%d", expectedPart1, part1)
|
||||
}
|
||||
|
||||
expectedPart2 := 234430066982597
|
||||
if part2 != expectedPart2 {
|
||||
t.Fatalf("unexpected part2:\nwant:\t%d\ngot:\t%d", expectedPart2, part2)
|
||||
}
|
||||
}
|
||||
1
2024/day11/input.txt
Normal file
1
2024/day11/input.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
0 44 175060 3442 593 54398 9 8101095
|
||||
1
2024/day11/test.txt
Normal file
1
2024/day11/test.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
125 17
|
||||
|
|
@ -8,6 +8,7 @@ import (
|
|||
|
||||
"cpatino.com/advent-of-code/2024/day1"
|
||||
"cpatino.com/advent-of-code/2024/day10"
|
||||
"cpatino.com/advent-of-code/2024/day11"
|
||||
"cpatino.com/advent-of-code/2024/day2"
|
||||
"cpatino.com/advent-of-code/2024/day3"
|
||||
"cpatino.com/advent-of-code/2024/day4"
|
||||
|
|
@ -29,6 +30,7 @@ var days = map[int]func(string) (int, int){
|
|||
8: day8.Run,
|
||||
9: day9.Run,
|
||||
10: day10.Run,
|
||||
11: day11.Run,
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue