Last active
December 8, 2015 11:34
-
-
Save martinlindhe/b7de57cbe2a37f664d98 to your computer and use it in GitHub Desktop.
aoc day 7.1
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
package aoc | |
import ( | |
"fmt" | |
"regexp" | |
"strconv" | |
"strings" | |
) | |
func makeCircuit(s string) map[string]string { | |
m := make(map[string]string) | |
arr := strings.Split(s, "\n") | |
r1 := regexp.MustCompile("([0-9a-zA-Z ]*) -> ([a-z]*)") | |
for _, val := range arr { | |
if len(val) == 0 { | |
continue | |
} | |
// split (ANYTHING) -> [a-z] | |
matches := r1.FindStringSubmatch(val) | |
m[matches[2]] = matches[1] | |
} | |
return m | |
} | |
func resolveValue(m map[string]string, wire string) (uint16, map[string]string) { | |
// fmt.Printf("processing %s\n", wire) | |
var res uint16 | |
if len(wire) == 0 { | |
fmt.Printf("XXX should not happen, empty input!\n") | |
return 0, m | |
} | |
tokens := strings.Split(m[wire], " ") | |
// fmt.Println(tokens) | |
u, err := strconv.ParseUint(wire, 0, 16) | |
if err == nil { | |
// if number, return directly | |
return uint16(u), m | |
} | |
if len(tokens) == 1 { | |
res, m = resolveValue(m, tokens[0]) | |
m[wire] = fmt.Sprintf("%d", res) | |
return res, m | |
} | |
if len(tokens) == 2 { | |
// NOT | |
//fmt.Printf("NOT %s\n", tokens[1]) | |
res, m = resolveValue(m, tokens[1]) | |
res = 65535 - res | |
m[wire] = fmt.Sprintf("%d", res) | |
return res, m | |
} | |
op := tokens[1] | |
a, m := resolveValue(m, tokens[0]) | |
b, m := resolveValue(m, tokens[2]) | |
switch op { | |
case "LSHIFT": | |
res = a << b | |
m[wire] = fmt.Sprintf("%d", res) | |
break | |
case "RSHIFT": | |
res = a >> b | |
m[wire] = fmt.Sprintf("%d", res) | |
break | |
case "AND": | |
res = a & b | |
m[wire] = fmt.Sprintf("%d", res) | |
break | |
case "OR": | |
res = a | b | |
m[wire] = fmt.Sprintf("%d", res) | |
break | |
} | |
return res, m | |
} |
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
package aoc | |
import ( | |
"io/ioutil" | |
"testing" | |
"github.com/stretchr/testify/assert" | |
) | |
func TestMakeCircuit(t *testing.T) { | |
c := makeCircuit( | |
"123 -> x\n" + | |
"456 -> y\n" + | |
"x AND y -> d\n" + | |
"x OR y -> e\n" + | |
"x LSHIFT 2 -> f\n" + | |
"y RSHIFT 2 -> g\n" + | |
"NOT x -> h\n" + | |
"NOT y -> i\n") | |
res, c := resolveValue(c, "i") | |
assert.Equal(t, uint16(65079), res) | |
} | |
func TestDay7Questions(t *testing.T) { | |
data, err := ioutil.ReadFile("../day7_input") | |
if err != nil { | |
t.Fatal("Error ", err) | |
} | |
s := string(data) | |
circuit := makeCircuit(s) | |
a, _ := resolveValue(circuit, "a") | |
// part 1 | |
assert.Equal(t, uint16(46065), a) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment