-
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
5 changed files
with
165 additions
and
145 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
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,75 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func run2(program []int, a int) bool { | ||
reg := [3]int{a, 0, 0} | ||
var oPos int | ||
for i := 0; i < len(program); i += 2 { | ||
opcode := program[i] | ||
literal := program[i+1] | ||
|
||
var combo int | ||
switch literal { | ||
case 0, 1, 2, 3: | ||
combo = literal | ||
case 4: | ||
combo = reg[0] | ||
case 5: | ||
combo = reg[1] | ||
case 6: | ||
combo = reg[2] | ||
case 7: | ||
// ignore | ||
} | ||
|
||
switch opcode { | ||
case 0: // adv | ||
reg[0] >>= combo | ||
case 1: // bxl | ||
reg[1] ^= literal | ||
case 2: // bst | ||
reg[1] = combo % 8 | ||
case 3: // jnz | ||
if reg[0] != 0 { | ||
i = literal - 2 | ||
} | ||
case 4: // bxc, ignore operand | ||
reg[1] ^= reg[2] | ||
case 5: // out | ||
out := combo % 8 | ||
if oPos > len(program) { | ||
return false | ||
} | ||
if out != program[oPos] { | ||
return false | ||
} | ||
oPos++ | ||
case 6: // bdv | ||
reg[1] = reg[0] >> combo | ||
case 7: // cdv | ||
reg[2] = reg[0] >> combo | ||
} | ||
} | ||
return oPos == len(program) | ||
} | ||
|
||
func part2_brute(parsed Parsed) { | ||
timeStart := time.Now() | ||
if From == 0 { | ||
fmt.Println(`!!! This will take a "few" days !!!`) | ||
fmt.Println(`You might want the --from <val>`) | ||
} | ||
for a := From; ; a++ { | ||
if a%1e7 == 0 { | ||
fmt.Printf("a: %d\n", a) | ||
} | ||
if run2(parsed.program, a) { | ||
fmt.Printf("Part 2: %d %b\t\tin %v\n", a, a, time.Since(timeStart)) | ||
break | ||
} | ||
} | ||
} |
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,43 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"slices" | ||
"time" | ||
) | ||
|
||
func part2_custom(parsed Parsed) { | ||
start := time.Now() | ||
outs := [][]int{ | ||
{2, 4, 1, 1, 7, 5, 0, 3, 1, 4, 4, 5, 5, 5, 3, 0}, | ||
{2, 4, 1, 2, 7, 5, 1, 7, 4, 4, 0, 3, 5, 5, 3, 0}, | ||
} | ||
|
||
fns := []Fn{ | ||
func(A int) int { | ||
return A&7 ^ 5 ^ (A>>(A&7^1))&7 | ||
}, | ||
func(A int) int { | ||
return A&7 ^ 5 ^ (A>>(A&7^2))&7 | ||
}, | ||
} | ||
|
||
var fn Fn | ||
for i, o := range outs { | ||
if slices.Compare(parsed.program, o) == 0 { | ||
fn = fns[i] | ||
fmt.Println("found formula for", o) | ||
} | ||
} | ||
if fn == nil { | ||
fmt.Println("Unsupported input, sorry") | ||
return | ||
} | ||
|
||
if a, ok := findA(parsed.program, 0, fn); ok { | ||
confirmed := run2(parsed.program, a) | ||
fmt.Printf("Part 2 custom: %d, confirmed: %t\t\tin %v\n", a, confirmed, time.Since(start)) | ||
} else { | ||
fmt.Println("Part 2 custom: solution not found") | ||
} | ||
} |
Oops, something went wrong.