Compare commits

..

2 Commits

2 changed files with 24 additions and 19 deletions

View File

@@ -12,19 +12,18 @@ func init() {
registry.Register("2015D7", ParseInput, PartOne, PartTwo)
}
func ParseInput(filepath string) []string {
func ParseInput(filepath string) map[string]string {
content, _ := os.ReadFile(filepath)
return strings.Split(string(content), "\n")
}
func PartOne(data []string) int {
instructions := make(map[string]string)
for _, line := range data {
for _, line := range strings.Split(string(content), "\n") {
if parts := strings.Split(line, " -> "); len(parts) == 2 {
instructions[parts[1]] = parts[0]
}
}
return instructions
}
func evaluateWire(wire string, instructions map[string]string) uint16 {
cache := make(map[string]uint16)
var evaluate func(string) uint16
evaluate = func(wire string) uint16 {
@@ -61,9 +60,13 @@ func PartOne(data []string) int {
cache[wire] = result
return result
}
return int(evaluate("a"))
return evaluate(wire)
}
func PartTwo(data []string) int {
func PartOne(instructions map[string]string) int {
return int(evaluateWire("a", instructions))
}
func PartTwo(instructions map[string]string) int {
return 0
}

View File

@@ -1,17 +1,19 @@
package dayseven
import "testing"
import (
"testing"
)
var testInput = []string{
"123 -> x",
"456 -> y",
"x AND y -> d",
"x OR y -> e",
"x LSHIFT 2 -> f",
"y RSHIFT 2 -> g",
"NOT x -> h",
"NOT y -> i",
"d -> a",
var testInput = map[string]string{
"x": "123",
"y": "456",
"d": "x AND y",
"e": "x OR y",
"f": "x LSHIFT 2",
"g": "y RSHIFT 2",
"h": "NOT x",
"i": "NOT y",
"a": "d",
}
func TestPartOne(t *testing.T) {