fix missing
This commit is contained in:
91
2024/go/day17/day17.go
Normal file
91
2024/go/day17/day17.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package day17
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"golang.org/x/exp/slices"
|
||||
|
||||
|
||||
"adventofcode2024/utils"
|
||||
|
||||
)
|
||||
|
||||
type Opcode int
|
||||
|
||||
const (
|
||||
adv Opcode = 0
|
||||
bxl Opcode = 1
|
||||
bst Opcode = 2
|
||||
jnx Opcode = 3
|
||||
bxc Opcode = 4
|
||||
out Opcode = 5
|
||||
bdv Opcode = 6
|
||||
cdv Opcode = 7
|
||||
|
||||
)
|
||||
|
||||
func Part1(input string) string {
|
||||
m := regexp.MustCompile(`[\d,]+`).FindAllString(input, -1)
|
||||
a := utils.MustAtoi(m[0])
|
||||
b := utils.MustAtoi(m[1])
|
||||
c := utils.MustAtoi(m[2])
|
||||
|
||||
var pgm []int
|
||||
json.Unmarshal([]byte("["+m[3]+"]"), &pgm)
|
||||
|
||||
fmt.Printf("%d %d %d %v\n", a, b, c, pgm)
|
||||
out := fmt.Sprint(run(a, b, c, pgm))
|
||||
out = fmt.Sprint(strings.Trim(strings.ReplaceAll(out, " ", ","), "[]"))
|
||||
return out
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
m := regexp.MustCompile(`[\d,]+`).FindAllString(input, -1)
|
||||
b := utils.MustAtoi(m[1])
|
||||
c := utils.MustAtoi(m[2])
|
||||
|
||||
var pgm []int
|
||||
json.Unmarshal([]byte("["+m[3]+"]"), &pgm)
|
||||
|
||||
fmt.Printf("%d %d %v\n", b, c, pgm)
|
||||
|
||||
a := 0
|
||||
for n := len(pgm) - 1; n >= 0; n-- {
|
||||
a <<= 3
|
||||
for !slices.Equal(run(a, b, c, pgm), pgm[n:]) {
|
||||
a++
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
func run(a, b, c int, pgm []int) (output []int) {
|
||||
for ip := 0; ip < len(pgm); ip += 2 {
|
||||
operand := pgm[ip+1]
|
||||
combo := []int{0, 1, 2, 3, a, b, c}[operand]
|
||||
|
||||
switch pgm[ip] {
|
||||
case int(adv):
|
||||
a >>= combo
|
||||
case int(bxl):
|
||||
b ^= operand
|
||||
case int(bst):
|
||||
b = combo % 8
|
||||
case int(jnx):
|
||||
if a != 0 {
|
||||
ip = operand - 2
|
||||
}
|
||||
case int(bxc):
|
||||
b ^= c
|
||||
case int(out):
|
||||
output = append(output, combo%8)
|
||||
case int(bdv):
|
||||
b = a >> combo
|
||||
case int(cdv):
|
||||
c = a >> combo
|
||||
}
|
||||
}
|
||||
return output
|
||||
}
|
||||
25
2024/go/day17/day17_test.go
Normal file
25
2024/go/day17/day17_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package day17
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(`Register A: 729
|
||||
Register B: 0
|
||||
Register C: 0
|
||||
|
||||
Program: 0,1,5,4,3,0`)
|
||||
require.Equal(t, "4,6,3,5,6,3,5,2,1,0", r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(`Register A: 2024
|
||||
Register B: 0
|
||||
Register C: 0
|
||||
|
||||
Program: 0,3,5,4,3,0`)
|
||||
require.Equal(t, 117440, r)
|
||||
}
|
||||
5
2024/go/day17/input.txt
Normal file
5
2024/go/day17/input.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
Register A: 61156655
|
||||
Register B: 0
|
||||
Register C: 0
|
||||
|
||||
Program: 2,4,1,5,7,5,4,3,1,6,0,3,5,5,3,0
|
||||
Reference in New Issue
Block a user