day 11
This commit is contained in:
parent
70ced1f6a3
commit
4b8bf419e2
101
year25/day11/day.go
Normal file
101
year25/day11/day.go
Normal file
@ -0,0 +1,101 @@
|
||||
package day11
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.bizdoc.ro/gabi-public/Advent-of-Code.git/aoc"
|
||||
"git.bizdoc.ro/private/devkit.git/collections/dsa"
|
||||
)
|
||||
|
||||
func parseInput(ctx aoc.Context) (map[string][]string, error) {
|
||||
graph := make(map[string][]string)
|
||||
|
||||
s := ctx.Scanner()
|
||||
for s.Scan() {
|
||||
line := s.Text()
|
||||
parts := strings.Split(line, ": ")
|
||||
key := parts[0]
|
||||
graph[key] = strings.Split(parts[1], " ")
|
||||
}
|
||||
if err := s.Err(); err != nil {
|
||||
return nil, fmt.Errorf("scanner error: %w", err)
|
||||
}
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
func Part1(ctx aoc.Context) (int, error) {
|
||||
graph, err := parseInput(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
const start = "you"
|
||||
const end = "out"
|
||||
|
||||
stack := dsa.NewStack[string]()
|
||||
stack.Push(start)
|
||||
var pathsToEnd int
|
||||
for !stack.IsEmpty() {
|
||||
this := stack.Pop()
|
||||
for _, next := range graph[this] {
|
||||
stack.Push(next)
|
||||
}
|
||||
if this == end {
|
||||
pathsToEnd += 1
|
||||
}
|
||||
}
|
||||
|
||||
return pathsToEnd, nil
|
||||
}
|
||||
|
||||
func Part2(ctx aoc.Context) (uint64, error) {
|
||||
graph, err := parseInput(ctx)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
DAC, FFT bool
|
||||
Name string
|
||||
}
|
||||
cache := make(map[Item]uint64)
|
||||
|
||||
var recursion func(current Item, end string) uint64
|
||||
recursion = func(current Item, end string) uint64 {
|
||||
if current.Name == end {
|
||||
return bool2int(current.DAC && current.FFT)
|
||||
}
|
||||
|
||||
switch current.Name {
|
||||
case "dac":
|
||||
current.DAC = true
|
||||
case "fft":
|
||||
current.FFT = true
|
||||
}
|
||||
|
||||
var sum uint64
|
||||
for _, next := range graph[current.Name] {
|
||||
current.Name = next
|
||||
|
||||
paths, ok := cache[current]
|
||||
if !ok {
|
||||
paths = recursion(current, end)
|
||||
cache[current] = paths
|
||||
}
|
||||
sum += paths
|
||||
}
|
||||
return sum
|
||||
}
|
||||
return recursion(Item{DAC: false, FFT: false, Name: "svr"}, "out"), nil
|
||||
}
|
||||
|
||||
func bool2int(b bool) uint64 {
|
||||
var i uint64
|
||||
if b {
|
||||
i = 1
|
||||
} else {
|
||||
i = 0
|
||||
}
|
||||
return i
|
||||
}
|
||||
27
year25/day11/day_test.go
Normal file
27
year25/day11/day_test.go
Normal file
@ -0,0 +1,27 @@
|
||||
package day11
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.bizdoc.ro/gabi-public/Advent-of-Code.git/tests"
|
||||
)
|
||||
|
||||
func TestPart1Example(t *testing.T) {
|
||||
const want = 5
|
||||
tests.Test(t, "example.txt", tests.Handler(Part1), want)
|
||||
}
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
const want = 764
|
||||
tests.Test(t, "input.txt", tests.Handler(Part1), want)
|
||||
}
|
||||
|
||||
func TestPart2Example(t *testing.T) {
|
||||
const want = 2
|
||||
tests.Test(t, "example_part2.txt", tests.Handler(Part2), want)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
const want = 462444153119850
|
||||
tests.Test(t, "input.txt", tests.Handler(Part2), want)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user