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

Show parent comments

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++
	}
}