2017 Day 23 Complete

This commit is contained in:
2017-12-23 09:01:32 -06:00
parent d16cc22158
commit 6c4e6ec824
8 changed files with 387 additions and 268 deletions

3
2017/day23/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
day23-part2
day23-part1
test

116
2017/day23/day23-part1.go Normal file
View File

@@ -0,0 +1,116 @@
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
inp := StdinToStrings()
run(inp)
}
var reg map[string]int
func run(inp []string) {
reg = make(map[string]int)
reg["a"] = 1
var mulCount int
var instCnt int
for i := 0; i >= 0 && i < len(inp); i++ {
instCnt++
if instCnt%10 == 0 {
ClearScreen()
printInstructions(inp, i)
}
pts := strings.Split(inp[i], " ")
if pts[1] == "h" || pts[2] == "h" {
fmt.Println("Hit h")
}
switch pts[0] {
case "set":
setReg(pts[1], pts[2])
case "sub":
subReg(pts[1], pts[2])
case "mul":
mulCount++
mulReg(pts[1], pts[2])
case "jnz":
i += jumpNotZero(pts[1], pts[2])
}
}
fmt.Println("Result (H):", reg["h"])
}
func setReg(key string, val string) {
reg[key] = getValue(val)
}
func subReg(key string, val string) {
reg[key] = getValue(key) - getValue(val)
}
func mulReg(key string, val string) {
reg[key] = getValue(key) * getValue(val)
}
func jumpNotZero(test string, jump string) int {
var ret int
if getValue(test) != 0 {
ret = getValue(jump) - 1
}
return ret
}
func getValue(key string) int {
var ret int
var ok bool
var err error
if ret, err = strconv.Atoi(key); err != nil {
// It's not a number
if ret, ok = reg[key]; !ok {
// The register is empty
reg[key] = 0
ret = 0
}
}
return ret
}
func PrintRegisters() {
fmt.Print("[ ")
for i := 0; i < 26; i++ {
getReg := string('a' + i)
fmt.Printf("%s:%d ", getReg, getValue(getReg))
}
fmt.Print("]\n")
}
func printInstructions(inp []string, pos int) {
PrintRegisters()
for i := range inp {
if i == pos {
fmt.Print("> ", inp[i], "\n")
} else {
fmt.Print(" ", inp[i], "\n")
}
}
}
func ClearScreen() {
fmt.Print("\033[H\033[2J")
}
func StdinToStrings() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}

54
2017/day23/day23-part2.go Normal file
View File

@@ -0,0 +1,54 @@
package main
import (
"fmt"
"math"
)
func main() {
var a, b, c, d, e, f, g, h int
// Suppress go warnings
_, _, _, _, _, _, _, _ = a, b, c, d, e, f, g, h
// Derived from test.go
b = 109900
c = 126900
// Unoptimized:
/*
for ; b < c+1; b += 17 {
f = 1
for d = 2; d < b+1; d++ {
for e = 2; e < b+1; e++ {
if d*e == b {
f = 0
}
}
}
// When is f 0?
// when d*e == b for "certain values" of b
fmt.Println(d, e, b)
if f == 0 {
h++
}
// b = 109900
}
*/
// Optimized
for ; b < c+1; b += 17 {
if !IsPrime(b) {
h++
}
}
fmt.Println(h)
}
func IsPrime(value int) bool {
for i := 2; i <= int(math.Floor(float64(value)/2)); i++ {
if value%i == 0 {
return false
}
}
return value > 1
}

32
2017/day23/input Normal file
View File

@@ -0,0 +1,32 @@
set b 99
set c b
jnz a 2
jnz 1 5
mul b 100
sub b -100000
set c b
sub c -17000
set f 1
set d 2
set e 2
set g d
mul g e
sub g b
jnz g 2
set f 0
sub e -1
set g e
sub g b
jnz g -8
sub d -1
set g d
sub g b
jnz g -13
jnz f 2
sub h -1
set g b
sub g c
jnz g 0
jnz 1 3
sub b -17
jnz 1 -23

View File

@@ -0,0 +1,52 @@
b = 99
c = b
if a != 0 {
b = b * 100
b = b + 100000
c = b
c = c + 17000
}
// b = 109900
// c = 126900
for {
f = 1
d = 2
for {
e = 2
g = d
for {
g = g * e
g = g - b
if g == 0 {
f = 0
}
e = e + 1
g = e
g = g - b
if g != 0 {
break
}
}
d = d + 1
g = d
g = g - b
if g != 0 {
break
}
}
if f == 0 {
h = h + 1
}
g = b
g = g - c
if g == 0 {
break
}
b = b + 17
}

39
2017/day23/input-loops Normal file
View File

@@ -0,0 +1,39 @@
set b 99
set c b
jnz a 2
jnz 1 5
mul b 100
sub b -100000
set c b
sub c -17000
set f 1
set d 2
set e 2
set g d
mul g e
sub g b
jnz g 2
set f 0
sub e -1
set g e
sub g b
jnz g -8
sub d -1
set g d
sub g b
jnz g -13
jnz f 2
sub h -1
set g b
sub g c
jnz g 0
jnz 1 3
sub b -17
jnz 1 -23

15
2017/day23/test.go Normal file
View File

@@ -0,0 +1,15 @@
package main
import "fmt"
// Logic before any loops (starting values)
func main() {
var a, b, c int
b = 99
c = b
b = b * 100
b = b + 100000
c = b
c = c + 17000
fmt.Println(a, b, c)
}