diff --git a/solutions/alessio/day17/aoc17 b/solutions/alessio/day17/aoc17 new file mode 100755 index 0000000..eccd0d7 Binary files /dev/null and b/solutions/alessio/day17/aoc17 differ diff --git a/solutions/alessio/day17/day17.go b/solutions/alessio/day17/day17.go new file mode 100644 index 0000000..00a0b2f --- /dev/null +++ b/solutions/alessio/day17/day17.go @@ -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)) +} diff --git a/solutions/alessio/day17/go.mod b/solutions/alessio/day17/go.mod new file mode 100644 index 0000000..4267c85 --- /dev/null +++ b/solutions/alessio/day17/go.mod @@ -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 +) diff --git a/solutions/alessio/day17/go.sum b/solutions/alessio/day17/go.sum new file mode 100644 index 0000000..fe99d71 --- /dev/null +++ b/solutions/alessio/day17/go.sum @@ -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= diff --git a/solutions/alessio/day17/input17.txt b/solutions/alessio/day17/input17.txt new file mode 100644 index 0000000..36fbf8d --- /dev/null +++ b/solutions/alessio/day17/input17.txt @@ -0,0 +1,5 @@ +Register A: 729 +Register B: 0 +Register C: 0 + +Program: 0,1,5,4,3,0 \ No newline at end of file diff --git a/solutions/alessio/day17/input17_def.txt b/solutions/alessio/day17/input17_def.txt new file mode 100644 index 0000000..9ae1608 --- /dev/null +++ b/solutions/alessio/day17/input17_def.txt @@ -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 \ No newline at end of file