r/adventofcode • • Dec 18 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 18 Solutions -๐ŸŽ„-

--- Day 18: Duet ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hints^ยค?

Spoiler


[Update @ 00:04] First silver

  • Welcome to the final week of Advent of Code 2017. The puzzles are only going to get more challenging from here on out. Adventspeed, sirs and madames!

[Update @ 00:10] First gold, 44 silver

  • We just had to rescue /u/topaz2078 with an industrial-strength paper bag to blow into. I'm real glad I bought all that stock in PBCO (Paper Bag Company) two years ago >_>

[Update @ 00:12] Still 1 gold, silver cap

[Update @ 00:31] 53 gold, silver cap

  • *mind blown*
  • During their famous kicklines, the Rockettes are not actually holding each others' backs like I thought they were all this time.
  • They're actually hoverhanding each other.
  • In retrospect, it makes sense, they'd overbalance themselves and each other if they did, but still...
  • *mind blown so hard*

[Update @ 00:41] Leaderboard cap!

  • I think I enjoyed the duplicating Santas entirely too much...
  • It may also be the wine.
  • Either way, good night (for us), see you all same time tomorrow, yes?

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

11 Upvotes

227 comments sorted by

View all comments

9

u/etherealflaim Dec 18 '17

I've heard it said that sometimes Python feels like cheating, well today Go felt like cheating ;). Part 2 just screams "coroutines".

package main_test

import (
    "fmt"
    "strconv"
    "strings"
    "testing"
    "time"
)

func part2(input string, id int, out, in chan int) (ret int) {
    registers := map[string]int{
        "p": id,
    }

    get := func(s string) int {
        if strings.IndexAny(s, "0123456789") != -1 {
            v, err := strconv.Atoi(s)
            if err != nil {
                panic(err)
            }
            return v
        }
        return registers[s]
    }

    commands := strings.Split(input, "\n")
    var instr int
    for count := 0; instr >= 0 && instr < len(commands); count++ {
        if count > 100000 {
            panic(count)
        }

        command := strings.TrimSpace(commands[instr])
        if command == "" {
            instr++
            continue
        }

        args := strings.Fields(commands[instr])
        switch cmd, argv := args[0], args[1:]; cmd {
        case "set":
            registers[argv[0]] = get(argv[1])
        case "add":
            registers[argv[0]] += get(argv[1])
        case "mul":
            registers[argv[0]] *= get(argv[1])
        case "mod":
            registers[argv[0]] %= get(argv[1])
        case "snd":
            select {
            case out <- get(argv[0]):
                fmt.Println("send", get(argv[0]), "from", id)
                ret++
            case <-time.After(1 * time.Second):
                return
            }
        case "rcv":
            select {
            case registers[argv[0]] = <-in:
                fmt.Println("got", get(argv[0]), "at", id)

            case <-time.After(1 * time.Second):
                return
            }
        case "jgz":
            if get(argv[0]) <= 0 {
                break
            }
            instr += get(argv[1])
            continue
        default:
            panic(command)
        }
        instr++
    }
    panic("exit")
}

func TestPart2(t *testing.T) {
    tests := []struct {
        name string
        in   string
        want int
    }{
        {"part2 example", `
            snd 1
            snd 2
            snd p
            rcv a
            rcv b
            rcv c
            rcv d`, 3},
        {"part2", `<input omitted for brevity>`, -1},
    }

    for _, test := range tests {
        t.Run(test.name, func(t *testing.T) {
            c01 := make(chan int, 10000)
            c10 := make(chan int, 10000)

            go part2(test.in, 0, c01, c10)

            if got, want := part2(test.in, 1, c10, c01), test.want; got != want {
                t.Errorf("part2(%#v) = %#v, want %#v", test.in, got, want)
            }
        })
    }
}

(edit: formatting)

3

u/rdc12 Dec 18 '17

My first thought was call/cc in scheme (or continuations in general) would be a fairly nice way of solveing it. (Went with an utter hack instead myself)

1

u/BumpitySnook Dec 18 '17

You could use a nested function in Python to embed all of the machine state[0], but rather than fighting the scoping rules / blah for that, I just passed machine state around explicitly.

[0] Something like this:

def create_vm(pid):
  mystate = collections.defaultdict(int)
  mypid = pid
  def run_vm(inputs):
    # do something with mystate
    return (Blocked, outputs)

  return run_vm

A = create_vm(0)
B = create_vm(1)
...

3

u/[deleted] Dec 18 '17 edited Dec 18 '17

Super frustrating. I did something very similar to yours, but mine never completes. Been trying to find the bug for an hour.

EDIT: OMFG there's a jgz 1 3 line. EDIT: Found another bug. I'm reading the wrong return value, because initially I thought the programs were 1 and 2, instead of 0 and 1.

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"strconv"
	"strings"
	"time"
)

func main() {
	buf, err := ioutil.ReadFile("input")
	if err != nil {
		log.Fatal(err)
	}
	input := string(buf)
	fmt.Println(puzzle2(input))
}

func puzzle2(input string) int {
	ch1 := make(chan int, 10000)
	ch2 := make(chan int, 10000)
	ret1 := make(chan int, 10000)
	ret2 := make(chan int, 10000)
	go prog(input, 0, ch1, ch2, ret1)
	go prog(input, 1, ch2, ch1, ret2)
	select {
	case ret := <-ret1:
		return ret
	case <-time.After(time.Second * 15):
		return 0
	}
}

func prog(input string, id int, in chan int, out chan int, ret chan int) {
	regs := make(map[string]int)
	regs["p"] = id
	count := 0
	lines := strings.Split(strings.TrimSpace(input), "\n")
	i := 0
	for {
		if i < 0 || i >= len(lines) {
			ret <- count
			return
		}

		line := lines[i]
		fields := strings.Fields(line)
		inst := fields[0]
		reg := fields[1]
		var y int
		if len(fields) > 2 {
			var err error
			y, err = strconv.Atoi(fields[2])
			if err != nil {
				y = regs[fields[2]]
			}
		}
		switch inst {
		case "snd":
			count++
			out <- regs[reg]
		case "rcv":
			select {
			case regs[reg] = <-in:
			case <-time.After(time.Second * 3):
				ret <- count
				return
			}
		case "set":
			regs[reg] = y
		case "add":
			regs[reg] += y
		case "mul":
			regs[reg] *= y
		case "mod":
			regs[reg] %= y
		case "jgz":
			if regs[reg] > 0 {
				i += y
				continue
			}
		}
		i++
	}
}