Skip to content

Commit

Permalink
2024 day 17
Browse files Browse the repository at this point in the history
  • Loading branch information
dbut2 committed Dec 17, 2024
1 parent 8790d79 commit 2cfffa0
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 2 deletions.
70 changes: 70 additions & 0 deletions 2024/17/1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"fmt"
"strings"

"github.com/dbut2/advent-of-code/pkg/harness"
)

func solve(input [][]int) string {
a, b, c := input[0][0], input[1][0], input[2][0]
prog := input[4]

output := []string{}

combo := func(i int) int {
switch {
case i < 4:
return i
case i == 4:
return a
case i == 5:
return b
case i == 6:
return c
case i == 7:
fallthrough
default:
panic("invalid combo operand")
}
}

i := 0
for {
if i >= len(prog) {
break
}
switch prog[i] {
case 0:
a >>= combo(prog[i+1])
case 1:
b ^= prog[i+1]
case 2:
b = combo(prog[i+1]) & 7
case 3:
if a != 0 {
i = prog[i+1]
continue
}
case 4:
b ^= c
case 5:
output = append(output, fmt.Sprint(combo(prog[i+1])&7))
case 6:
b = a >> combo(prog[i+1])
case 7:
c = a >> combo(prog[i+1])
}

i += 2
}

return strings.Join(output, ",")
}

func main() {
h := harness.New(solve)
h.Expect(1, "4,6,3,5,6,3,5,2,1,0")
h.Run()
}
88 changes: 88 additions & 0 deletions 2024/17/2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package main

import (
"slices"

"github.com/dbut2/advent-of-code/pkg/harness"
)

func solve(input [][]int) int {
b, c := input[1][0], input[2][0]
prog := input[4]

a := 1
for {
out := run(a, b, c, prog)
if slices.Equal(out, prog) {
break
}
if slices.Equal(out, prog[len(prog)-len(out):]) {
a *= 8
} else {
if a%8 == 7 {
a /= 8
}
a++
}
}

return a
}

func run(a int, b int, c int, prog []int) []int {
output := []int{}

combo := func(i int) int {
switch {
case i < 4:
return i
case i == 4:
return a
case i == 5:
return b
case i == 6:
return c
case i == 7:
fallthrough
default:
panic("invalid combo operand")
}
}

i := 0
for {
if i >= len(prog) {
break
}
switch prog[i] {
case 0:
a >>= combo(prog[i+1])
case 1:
b ^= prog[i+1]
case 2:
b = combo(prog[i+1]) & 7
case 3:
if a != 0 {
i = prog[i+1]
continue
}
case 4:
b ^= c
case 5:
output = append(output, combo(prog[i+1])&7)
case 6:
b = a >> combo(prog[i+1])
case 7:
c = a >> combo(prog[i+1])
}

i += 2
}
return output
}

func main() {
h := harness.New(solve)
h.Expect(2, 117440)
h.Run()
}
5 changes: 5 additions & 0 deletions 2024/17/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Register A: 729
Register B: 0
Register C: 0

Program: 0,1,5,4,3,0
5 changes: 5 additions & 0 deletions 2024/17/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Register A: 2024
Register B: 0
Register C: 0

Program: 0,3,5,4,3,0
49 changes: 49 additions & 0 deletions pkg/harness/harness.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package harness

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand All @@ -10,6 +11,7 @@ import (
"path/filepath"
"regexp"
"runtime"
"slices"
strings2 "strings"

"github.com/dbut2/advent-of-code/pkg/benchmark"
Expand Down Expand Up @@ -198,6 +200,49 @@ func (h *Harness[T, U]) getInput() string {
return input
}

func (h *Harness[T, U]) getAnswers() []U {
data, _ := os.ReadFile(filepath.Join(h.metadata.workdir, "answers.txt"))

var answers []U
switch any(*new(U)).(type) {
case string:
for _, line := range bytes.Split(data, []byte("\n")) {
answers = append(answers, any(string(line)).(U))
}
case int:
for _, line := range bytes.Split(data, []byte("\n")) {
answers = append(answers, any(sti.Int(string(line))).(U))
}
}
return answers
}

func (h *Harness[T, U]) addAnswer(v U) {
answers := h.getAnswers()
answers = append(answers, v)

var data []byte
switch any(*new(U)).(type) {
case string:
for _, a := range answers {
data = append(data, []byte(fmt.Sprintf("%v\n", a))...)
}
case int:
for _, a := range answers {
data = append(data, []byte(fmt.Sprintf("%d\n", a))...)
}
}

err := os.WriteFile(filepath.Join(h.metadata.workdir, "answers.txt"), data, 0644)
if err != nil {
panic(err.Error())
}
}

func (h *Harness[T, U]) hasAnswered(v U) bool {
return slices.Contains(h.getAnswers(), v)
}

func (h *Harness[T, U]) fetchInput() string {
url := fmt.Sprintf("https://adventofcode.com/%d/day/%d/input", h.metadata.year, h.metadata.day)
session := os.Getenv("AOC_SESSION")
Expand Down Expand Up @@ -233,6 +278,10 @@ func (h *Harness[T, U]) getTestInput(n int) string {
}

func (h *Harness[T, U]) submitAnswer(answer U) string {
if h.hasAnswered(answer) {
panic("already tried")
}
h.addAnswer(answer)
url := fmt.Sprintf("https://adventofcode.com/%d/day/%d/answer", h.metadata.year, h.metadata.day)
session := os.Getenv("AOC_SESSION")
if session == "" {
Expand Down
24 changes: 24 additions & 0 deletions pkg/std/std.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package std

import (
"github.com/dbut2/advent-of-code/pkg/lists"
"github.com/dbut2/advent-of-code/pkg/math"
"github.com/dbut2/advent-of-code/pkg/space"
"github.com/dbut2/advent-of-code/pkg/sti"
"github.com/dbut2/advent-of-code/pkg/strings"
)

type Grid = space.Grid[byte]

var Abs = math.Abs
var Fill = lists.Fill
var Filter = lists.Filter
var GCD = math.GCD
var Int = sti.Int
var Ints = strings.Ints
var LCM = math.LCM
var Pow = math.Pow
var Product = math.Product
var Reduce = lists.Reduce
var Sign = math.Sign
var Sum = math.Sum
9 changes: 7 additions & 2 deletions template/1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package main

import (
"github.com/dbut2/advent-of-code/pkg/harness"
. "github.com/dbut2/advent-of-code/pkg/std"
)

func solve(input []string) int {

// func solve(input string) int {
// func solve(input []string) int {
// func solve(input [][]int) int {
// func solve(input Grid) int {

}

func main() {
h := harness.New(solve)
h.Expect(1, 0)
h.Run()
}

0 comments on commit 2cfffa0

Please sign in to comment.