generated from arol-polito/python-project-template
-
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
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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,113 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
func check(e error) { | ||
if e != nil { | ||
panic(e) | ||
} | ||
} | ||
|
||
func getComboOpVal(operand int, a int, b int, c int) int { | ||
if operand >= 7 || operand < 0 { | ||
panic("invalid combo operand value: " + string(operand)) | ||
} | ||
switch operand { | ||
case 4: | ||
return a | ||
case 5: | ||
return b | ||
case 6: | ||
return c | ||
default: | ||
return operand | ||
} | ||
} | ||
|
||
func exec(opcode int, operand int, a *int, b *int, c *int) bool { | ||
switch opcode { | ||
case 0: // adv | ||
opVal := getComboOpVal(operand, *a, *b, *c) | ||
res := (*a) / (1 << opVal) | ||
*a = res | ||
case 1: // bxl | ||
(*b) = (*b) ^ operand | ||
case 2: // bst | ||
opVal := getComboOpVal(operand, *a, *b, *c) | ||
(*b) = opVal % 8 | ||
case 3: // jnz | ||
if (*a) != 0 { | ||
return true | ||
} | ||
case 4: // bxc | ||
(*b) = (*b) ^ (*c) | ||
case 5: // out | ||
opVal := getComboOpVal(operand, *a, *b, *c) | ||
fmt.Printf("%d,", opVal%8) | ||
case 6: // bdv | ||
opVal := getComboOpVal(operand, *a, *b, *c) | ||
res := (*a) / (1 << opVal) | ||
*b = res | ||
case 7: // cdv | ||
opVal := getComboOpVal(operand, *a, *b, *c) | ||
res := (*a) / (1 << opVal) | ||
*c = res | ||
default: | ||
panic("invalid opcode: " + string(opcode)) | ||
} | ||
|
||
return false | ||
} | ||
|
||
func solve(lines []string) { | ||
var a, b, c int | ||
fmt.Sscanf(lines[0], "Register A: %d", &a) | ||
fmt.Sscanf(lines[1], "Register B: %d", &b) | ||
fmt.Sscanf(lines[2], "Register C: %d", &c) | ||
|
||
cmdstr := strings.Split(strings.Split(lines[4], ": ")[1], ",") | ||
cmds := make([]int, 0, len(cmdstr)) | ||
for _, s := range cmdstr { | ||
n, _ := strconv.Atoi(s) | ||
cmds = append(cmds, n) | ||
} | ||
|
||
ptr := 0 | ||
for ptr <= len(cmds)-1 { | ||
jump := exec(cmds[ptr], cmds[ptr+1], &a, &b, &c) | ||
if jump { | ||
ptr = cmds[ptr+1] | ||
} else { | ||
ptr += 2 | ||
} | ||
} | ||
} | ||
|
||
func part1(lines []string) { | ||
solve(lines) | ||
|
||
} | ||
|
||
func part2(lines []string) { | ||
solve(lines) | ||
} | ||
|
||
func main() { | ||
data, err := os.ReadFile("./input17_def.txt") | ||
check(err) | ||
dataStr := strings.ReplaceAll(string(data), "\r\n", "\n") | ||
lines := strings.Split(strings.Trim(dataStr, "\n"), "\n") | ||
|
||
start := time.Now() | ||
part1(lines) | ||
fmt.Printf("part1: %s\n", time.Since(start)) | ||
start = time.Now() | ||
part2(lines) | ||
fmt.Printf("part2: %s\n", time.Since(start)) | ||
} |
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,10 @@ | ||
module aoc17 | ||
|
||
go 1.23 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/testify v1.10.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
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,9 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= | ||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
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: 35200350 | ||
Register B: 0 | ||
Register C: 0 | ||
|
||
Program: 2,4,1,2,7,5,4,7,1,3,5,5,0,3,3,0 |