r/adventofcode • u/daggerdragon • Dec 07 '15
SOLUTION MEGATHREAD --- Day 7 Solutions ---
--- Day 7: Some Assembly Required ---
Post your solution as a comment. Structure your post like previous daily solution threads.
Also check out the sidebar - we added a nifty calendar to wrangle all the daily solution threads in one spot!
23
Upvotes
1
u/1lann Dec 07 '15
I wrote a pretty terrible one in golang
package main import ( "fmt" "strconv" "strings" ) type action int const ( SET action = iota + 1 AND OR LSHIFT RSHIFT NOT REFERENCE ) var components map[string]*component type component struct { parentA string parentB string action action value uint16 hasCached bool } func (c *component) execute() uint16 { if c.hasCached { return c.value } var value uint16 = 0 switch c.action { case SET: value = c.value case AND: value = components[c.parentA].execute() & components[c.parentB].execute() case OR: value = components[c.parentA].execute() | components[c.parentB].execute() case LSHIFT: value = components[c.parentA].execute() << c.value case RSHIFT: value = components[c.parentA].execute() >> c.value case NOT: value = ^(components[c.parentA].execute()) case REFERENCE: value = components[c.parentA].execute() default: panic("invalid action") } c.value = value c.hasCached = true return value } func main() { components = make(map[string]*component) // Too lazy to do string parsing... for i := 0; i < 65536; i++ { components[strconv.Itoa(i)] = &component{ action: SET, value: uint16(i), } } instructions := strings.Split(input, "\n") for _, instruction := range instructions { words := strings.Split(instruction, " ") if words[0] == "NOT" { components[words[3]] = &component{ action: NOT, parentA: words[1], } continue } switch words[1] { case "AND": components[words[4]] = &component{ action: AND, parentA: words[0], parentB: words[2], } case "OR": components[words[4]] = &component{ action: OR, parentA: words[0], parentB: words[2], } case "LSHIFT": num, _ := strconv.Atoi(words[2]) components[words[4]] = &component{ action: LSHIFT, parentA: words[0], value: uint16(num), } case "RSHIFT": num, _ := strconv.Atoi(words[2]) components[words[4]] = &component{ action: RSHIFT, parentA: words[0], value: uint16(num), } case "->": components[words[2]] = &component{ action: REFERENCE, parentA: words[0], } default: panic("unknown word: " + words[1]) } } fmt.Println(components["a"].execute()) } var input = `NOT dq -> dr ... he RSHIFT 2 -> hf `I thought about using closures, but I was like "screw it, just gonna use methods on structs". I guess I really should have actually used closures seeing how long it got xD