-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
248 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters