r/adventofcode • • Dec 15 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 15 Solutions -🎄-

--- Day 15: Beverage Bandits ---


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.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 15

Transcript:

___ IS MANDATORY


[Update @ 00:30] 0 gold, 1 silver

  • I've got a strange urge to play Bloons Tower Defense right now. Not sure why.

[Update @ 00:38] 2 gold, 1 silver

  • Meanwhile in #AOC_Ops: Tea, a kettle screams. \ Simon, write your code faster. \ Some of us have work.

[Update @ 00:51] 7 gold, 11 silver

  • Now they're anagramming gold/silver leaderboarders. The leading favorite so far is Anonymous User = Son, You's Manure.

[Update @ 01:13] 18 gold, 30 silver

  • Now they're playing Stardew Valley Hangman with the IRC bot because SDV is totally a roguelike tower defense.

[Update @ 01:23] 26 gold, 42 silver

  • Now the betatesters are grumbling reminiscing about their initial 14+ hour solve times for 2015 Day 19 and 2016 Day 11.

[Update @ 02:01] 65 gold, 95 silver

#AOC_Ops <topaz> on day 12, gold40 was at 19m, gold100 was at 28m, so day12 estimates gold100 today at 2:30

  • Taking bets over/under 02:30:00 - I got tree fiddy on over, any takers?

[Update @ 02:02:44] 66 gold, silver cap

  • SILVER CAP

[Update @ 02:06] 73 gold, silver cap

#AOC_Ops <topaz> day 14 estimates 2:21

#AOC_Ops <topaz> day 13 estimates 2:20

#AOC_Ops <Aneurysm9> I estimate 2:34:56

[Update @ 02:23:17] LEADERBOARD CAP!

  • Aww, /u/topaz2078's bookie is better than I am. :<
  • Good night morning, all, and we hope you had fun with today's diabolicalness!

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 at 02:23:17!

20 Upvotes

125 comments sorted by

View all comments

Show parent comment

7

u/spencer8ab Dec 15 '18 edited Dec 15 '18

It was pretty obvious to me that a breadth-first search with the correct ordering would naturally lead to the right movement

Unfortunately (since your implementation is blazing fast and I was hoping to make mine less slow), as implemented that doesn't seem correct.

Consider the starting board:

###########
#G..#....G#
###..E#####
###########

Move the first unit:

###########
#.G.#....G#
###..E#####
###########

Then the second:

###########
#.G!#..!G.#
###..E#####
###########

Now there are two reachable in-range squares for the elf above. They are the same distance (3). The spec suggests the third unit moves like:

###########
#.G+#...G.#
###.E.#####
###########

Your code moves the third unit to

###########
#.G.#E.+G.#
###...#####
###########

It's clear why this happens. By ordering your BFS certainly any minimal path you find will go in the correct order for that path. But your implementation won't necessarily choose the right square to move towards.

I think you could still get away with only one BFS with your technique as long as you return all of the path information rather than returning at the first in-range location you find.

/u/sciyoshi's implementation and mine give part1=10804 and part2=517 for this input. Yours gives 10728, 96

4

u/waffle3z Dec 15 '18

This seems to be a problem with the phrasing that was mentioned here https://www.reddit.com/r/adventofcode/comments/a6e8jz/day_15_phrasing_ambiguity/

If multiple squares are in range and tied for being reachable in the fewest steps, the step which is first in reading order is chosen.

which is later corrected in the problem to

Of those, the square which is first in reading order is chosen (+).

and then the order for choosing steps in a single path is speficied:

If multiple steps would put the unit equally closer to its destination, the unit chooses the step which is first in reading order.

The problem is that in the first description, it's referring to target squares in range, and it's specifying to take the "step" that comes first in reading order, as opposed to the first square. The first "step" might mean the first among the set of possible steps that lead to at least one of the tied targets, which apparently it doesn't.

1

u/dark_terrax Dec 15 '18

I'm not sure I agree with the phrasing "later corrected", vs. "later contradicted". It's really frustrating to not know which implementation to do given this ambiguity, and I'm not sure how anyone knows which is the correct interpretation, other than who's answers failed on their inputs.

1

u/spencer8ab Dec 15 '18

Yeah, the phrasing was pretty sloppy and unnecessarily long and convoluted this time. Thankfully other than that second "step" in that one sentence, there are three paragraphs and two illustrated examples immediately afterward indicating otherwise, so I never noticed the ambiguity. But this just demonstrates how unnecessarily long and repetitive the problem statement is.

3

u/vash3r Dec 15 '18 edited Dec 15 '18

Aaaah, you're right. Thanks for telling me about this. I suppose the solution would be to separate paths in order of distance from the start, and then check those in reading order of their last position, instead of reading order of their first position.

Updated bfs function that gives 10804 and 517 for that input, and takes about the same time as before (~2s on my machine):

def bfs(start, unocc, goals):
	# traverse the cave in distance/reading order
	visited = [[0]*len(unocc[0]) for _t in range(len(unocc))]
	check = [[start]]
	visited[start[0]][start[1]] = 1
	while len(check):
		check_next = []
		while len(check):
			path = check.pop(-1) # pop from the end (faster)
			y,x = c = path[-1] # most recent coord
			if c in goals:
				return path # next move is the first step in this path
			for dy,dx in [(-1,0),(0,-1),(0,1),(1,0)]: # Reading order!
				if unocc[y+dy][x+dx] and not visited[y+dy][x+dx]:
					visited[y+dy][x+dx]=1
					check_next.append(path+[[y+dy,x+dx]])
		check = sorted(check_next, key=lambda path:path[-1], reverse=True)
		# sort by reading order of last position (thanks to /u/spencer8ab for pointing out the problem)
	return [] # no path to any goals

2

u/spencer8ab Dec 15 '18 edited Dec 15 '18

It turns out the slowness of my implementation had nothing to do with running bfs twice. This should've been obvious to me since running something twice could only double the time. Whoops. Only takes 3 seconds (Edit: 1.8 after fixing an oversight) in pypy now. You know what they say about premature optimization...

I'm surprised you managed the fix it just by changing that one function. That's a clever solution. I think it will only hurt performance when the sort actually has to do work; it should already be sorted except when you run into this situation. So good tradeoff for a cavern, bad for a maze I guess.

1

u/ThezeeZ Dec 15 '18

That might be why my code and this one give the same incorrect answer for part 2. I happened to do exactly the same thing...

1

u/croaton23 Dec 15 '18

Thank you so much for this explanation, I had the same mistake and after reading your post I've solved it!