last 3 days

This commit is contained in:
Ben Charlton 2017-12-27 22:08:03 +00:00
parent acbd3aa664
commit c994c0ea41
5 changed files with 365 additions and 0 deletions

62
25/25.go Normal file
View file

@ -0,0 +1,62 @@
package main
import (
"fmt"
)
// go run 25.go
type instruction struct {
state string
condition int
write int
move int
newState string
}
var production = []instruction{
{"a", 0, 1, 1, "b"},
{"a", 1, 0, -1, "d"},
{"b", 0, 1, 1, "c"},
{"b", 1, 0, 1, "f"},
{"c", 0, 1, -1, "c"},
{"c", 1, 1, -1, "a"},
{"d", 0, 0, -1, "e"},
{"d", 1, 1, 1, "a"},
{"e", 0, 1, -1, "a"},
{"e", 1, 0, 1, "b"},
{"f", 0, 0, 1, "c"},
{"f", 1, 0, 1, "e"},
}
func main() {
fmt.Println(ProcessInstructions(production, 12317297))
}
func ProcessInstructions(instructions []instruction, count int) int {
pos := 0
state := instructions[0].state
tape := make(map[int]int)
for i := 0; i < count; i++ {
for r := range instructions {
inst := instructions[r]
if inst.state == state && inst.condition == tape[pos] {
state = inst.newState
tape[pos] = inst.write
pos += inst.move
break
}
}
}
cksum := 0
for _, v := range tape {
if v == 1 {
cksum++
}
}
return cksum
}

22
25/25_test.go Normal file
View file

@ -0,0 +1,22 @@
package main
import (
"testing"
)
var test = []instruction{
{"a", 0, 1, 1, "b"},
{"a", 1, 0, -1, "b"},
{"b", 0, 1, -1, "a"},
{"b", 1, 1, 1, "a"}}
func TestInstructions(t *testing.T) {
v := ProcessInstructions(test, 6)
if v != 3 {
t.Error(
"For", test,
"expected 3",
"got", v,
)
}
}