Skip to content

Commit

Permalink
Implementation for 16a
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Lowe committed Dec 19, 2018
1 parent 7e06004 commit cb66768
Show file tree
Hide file tree
Showing 7 changed files with 4,543 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aoc2018.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/nlowe/aoc2018/day13"
"github.com/nlowe/aoc2018/day14"
"github.com/nlowe/aoc2018/day15"
"github.com/nlowe/aoc2018/day16"
"github.com/nlowe/aoc2018/day2"
"github.com/nlowe/aoc2018/day3"
"github.com/nlowe/aoc2018/day4"
Expand Down Expand Up @@ -74,6 +75,7 @@ func main() {
day13.A, day13.B,
day14.A, day14.B,
day15.A, day15.B,
day16.A,
)

flags := rootCmd.PersistentFlags()
Expand Down
69 changes: 69 additions & 0 deletions day16/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package day16

import (
"fmt"
"strings"

"github.com/nlowe/aoc2018/util"
"github.com/spf13/cobra"
)

var A = &cobra.Command{
Use: "16a",
Short: "Day 16, Problem A",
Run: func(_ *cobra.Command, _ []string) {
fmt.Printf("Answer: %d\n", a(util.ReadInput()))
},
}

func readInput(input *util.ChallengeInput) (snapshots []snapshot, instructions []instruction) {
c := input.Lines()

for line := range c {
line = strings.TrimSpace(line)
if line == "" {
continue
}

if strings.HasPrefix(line, "Before: ") {
line = line[9 : len(line)-1]
beforeParts := strings.Split(line, ", ")

line = strings.TrimSpace(<-c)
parts := strings.Split(line, " ")

line = strings.TrimSpace(<-c)
line = line[9 : len(line)-1]
afterParts := strings.Split(line, ", ")

snapshots = append(snapshots, snapshot{
before: [4]int{util.AtoiOrPanic(beforeParts[0]), util.AtoiOrPanic(beforeParts[1]), util.AtoiOrPanic(beforeParts[2]), util.AtoiOrPanic(beforeParts[3])},
i: instruction{
opcode: util.AtoiOrPanic(parts[0]),
a: util.AtoiOrPanic(parts[1]),
b: util.AtoiOrPanic(parts[2]),
c: util.AtoiOrPanic(parts[3]),
},
after: [4]int{util.AtoiOrPanic(afterParts[0]), util.AtoiOrPanic(afterParts[1]), util.AtoiOrPanic(afterParts[2]), util.AtoiOrPanic(afterParts[3])},
})
} else {
parts := strings.Split(line, " ")

instructions = append(instructions, instruction{
opcode: util.AtoiOrPanic(parts[0]),
a: util.AtoiOrPanic(parts[1]),
b: util.AtoiOrPanic(parts[2]),
c: util.AtoiOrPanic(parts[3]),
})
}
}

return
}

func a(input *util.ChallengeInput) int {
c := cpu{}

snapshots, _ := readInput(input)
return c.train(snapshots)
}
16 changes: 16 additions & 0 deletions day16/a_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package day16

import (
"testing"

"github.com/nlowe/aoc2018/util"
"github.com/stretchr/testify/require"
)

func TestA(t *testing.T) {
input := util.TestInput(`Before: [3, 2, 1, 1]
9 2 1 2
After: [3, 2, 2, 1]`)

require.Equal(t, 1, a(input))
}
53 changes: 53 additions & 0 deletions day16/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package day16

type instruction struct {
opcode int

a int
b int
c int
}

type snapshot struct {
before [4]int
i instruction
after [4]int
}

type cpu struct {
registers [4]int
opcodes [16]instructionImplementation

ip int
program []instruction
}

func (c *cpu) train(ss []snapshot) (threeOrMore int) {
for _, s := range ss {
count := 0
for _, i := range implementations {
if i.candidate(&s.i, s.before, s.after) {
count++
}
}

if count >= 3 {
threeOrMore++
}
}

// TODO: Determine implementation
return
}

func (c *cpu) step() {
i := c.program[c.ip]
c.opcodes[i.opcode].exec(&i, c)
c.ip++
}

func (c *cpu) run() {
for c.ip < len(c.program) {
c.step()
}
}
Loading

0 comments on commit cb66768

Please sign in to comment.