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

View File

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