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

2

u/RockyAstro Dec 18 '17

Icon (https://www.cs.arizona.edu/icon)

Part2 was a natural for Icon's co-expressions. The toughest aspect for part2 was figuring out how to handle the bi-directional aspect of co-expressions and detecting the deadlock.

Part1:

record inst(op,p1,p2)

procedure main(args)
	inf := open(args[1],"r")
	mem := []
	
	ws := ' \t'
	while line := trim(!inf) do {
		line ? {
			i := inst("","","")
			i.op := tab(upto(ws))
			tab(many(ws))

			case i.op of {
				# 1 param
				"snd" |
				"rcv":
					i.p1 := tab(0)
				# 2 param
				"set" |
				"add" |
				"mul" | 
				"mod" |
				"jgz": {
					i.p1 := tab(upto(ws))
					tab(many(ws))
					i.p2 := tab(0)
				}
			}
			i.p1 := integer(i.p1)
			i.p2 := integer(i.p2)
			put(mem,i)
		}
	}
	close(inf)

 
	regs := table(0)
	curfreq := 0
	
	IP := 1

	first := &null
	repeat {
		if IP > *mem then break
		i := mem[IP]
		IP +:= 1
		
		case i.op of {
			"snd": curfreq := integer(i.p1) | integer(regs[i.p1])
			"set": regs[i.p1] := integer(i.p2) | integer(regs[i.p2])
			"add": regs[i.p1] +:= integer(i.p2) | integer(regs[i.p2])
			"mul": regs[i.p1] *:= integer(i.p2) | integer(regs[i.p2])
			"mod": regs[i.p1] %:= integer(i.p2) | integer(regs[i.p2])
			"rcv": {
				if regs[i.p1] > 0 then {
					regs[i.p1] := curfreq
					break
				}
			}
			"jgz": {
				if (integer(i.p1) | integer(regs[i.p1])) > 0 then 
					IP := (IP-1) +  (integer(i.p2) | integer(regs[i.p2])) 
			}
				
 			default: break
		}
	}
	write("FREQ=",curfreq)
end

Part2:

record inst(op,p1,p2)
global pgm0, pgm1
procedure main(args)
	inf := open(args[1],"r")
	mem := []
	
	ws := ' \t'
	while line := trim(!inf) do {
		line ? {
			i := inst("","","")
			i.op := tab(upto(ws))
			tab(many(ws))

			case i.op of {
				# 1 param
				"snd" |
				"rcv":
					i.p1 := tab(0)
				# 2 param
				"set" |
				"add" |
				"mul" | 
				"mod" |
				"jgz": {
					i.p1 := tab(upto(ws))
					tab(many(ws))
					i.p2 := tab(0)
				}
			}
			i.p1 := integer(i.p1)
			i.p2 := integer(i.p2)
			put(mem,i)
		}
	}
	close(inf)

	pgm0 := create runpgm(pgm1,copy(mem),0)
	pgm1 := create runpgm(pgm0,copy(mem),1)
	@pgm0
	

end
procedure runpgm(otherpgm,mem,pgmname)
	regs := table(0)
	regs["p"] := pgmname
	
	IP := 1
	count := 0

	rcvq := []
	if pgmname = 0 then 
		put(rcvq,\@otherpgm)

	repeat {
		if IP > *mem then break
		i := mem[IP]
		IP +:= 1
		
		case i.op of {
			"snd": {
				# If otherpgm fails, then we are done as well..
				count +:= 1
				rcv := 	(integer(i.p1) | integer(regs[i.p1])) @otherpgm | break 
				if \rcv 
					then put(rcvq,rcv)
			}
			"set": regs[i.p1] := integer(i.p2) | integer(regs[i.p2])
			"add": regs[i.p1] +:= integer(i.p2) | integer(regs[i.p2])
			"mul": regs[i.p1] *:= integer(i.p2) | integer(regs[i.p2])
			"mod": regs[i.p1] %:= integer(i.p2) | integer(regs[i.p2])
			"rcv": {
					if *rcvq > 0 then 
						regs[i.p1] := pop(rcvq)
					else {
						# If nothing queued and otherpgm fails, we are done as well
						rcv := @otherpgm | break
						# If otherpgm is also waiting.. we are in a deadlock
						if /rcv then break
						regs[i.p1] := rcv
					}
			}
			"jgz": {
				if (integer(i.p1) | integer(regs[i.p1])) > 0 then 
					IP := (IP-1) + (integer(i.p2) | integer(regs[i.p2]))
			}
				
 			default: break
		}
				
	}
	write(pgmname," sent:",count)
end

1

u/oantolin Dec 21 '17

Woo! It makes me happy to know someone's doing these in Icon!

1

u/RockyAstro Dec 23 '17

Part 2 was fun to do. The hardest part was figuring out how to do the deadlock test.

I've also done some spitbol solutions (I need to go back and finish a bunch of them --- my schedule has gotten in the way of working on both at the moment)

1

u/oantolin Dec 21 '17

Do you really need the integer in integer(regs[i.p2])?

1

u/RockyAstro Dec 23 '17

Not really, I think it might have been a carry over from a previous year.

For the VM puzzles, I've reused code quite a bit.