-
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
Nathan Lowe
committed
Dec 19, 2018
1 parent
7e06004
commit cb66768
Showing
7 changed files
with
4,543 additions
and
0 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,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) | ||
} |
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,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)) | ||
} |
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,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() | ||
} | ||
} |
Oops, something went wrong.