r/adventofcode • • Dec 12 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 12 Solutions -🎄-

--- Day 12: Subterranean Sustainability ---


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 12

Transcript:

On the twelfth day of AoC / My compiler spewed at me / Twelve ___


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 00:27:42!

22 Upvotes

257 comments sorted by

View all comments

-1

u/[deleted] Dec 12 '18

What an annoying problem today...

  1. It is not said if only one rule can apply per generation, if any rule can apply, if rules in one generation have effects on other rules in one generation
  2. It is very unclear how to find the position of the "number 0" pot
  3. You cannot test with the example because it is incomplete

I wonder why so many people got to a solution this fast. I think the problem is not too hard if you know what to do....

2

u/abnew123 Dec 12 '18

"Your puzzle input contains a list of pots from 0 to the right"

Does this not mean the 0 pot is always the first character of the given input string?

Also, like two lines down, "#..##.... indicates that pots 0, 3, and 4 currently contain plants"

1

u/[deleted] Dec 12 '18

I still don't get it So if my resulting string is this:

###..#...##..............#...##...................................#.....#......#.#.........###.#.#.

What is the sum? should it be 877? (If the first character has number 0)

2

u/abnew123 Dec 12 '18

if the first character in your string is the number 0 pot, the sum is 877 yes.

1

u/[deleted] Dec 12 '18

Apparently not:

That's not the right answer. If you're stuck, there are some general tips on the about page, or you can ask for hints on the subreddit. Because you have guessed incorrectly 9 times on this puzzle, please wait 10 minutes before trying again. (You guessed 877.)

How do I know which is my number 0 pot? In the example it is the 4th pot from the left, but it is not explained why.

0

u/[deleted] Dec 12 '18

[deleted] — view deleted comment

1

u/[deleted] Dec 12 '18

I need to expand? In which scenario would that happen? At least it is not in the example

4

u/ThezeeZ Dec 12 '18

In the example, in generation 3, a plant grows in pot -1

1

u/[deleted] Dec 12 '18

I see :-) THX

3

u/abnew123 Dec 12 '18

The pots don't move. So your starting 0 pot is still the 0 pot. Like, if "#..#" was your string, and # is your starting 0 pot, you can represent your pots as whatever you want (".#..#", "..#..#", ".#..#....."), but that first # is still your pot 0. At the end, whatever pot corresponds to the initial first # is still pot 0.

2

u/[deleted] Dec 12 '18

So I have to track the left-most pot from the starting string and this position will be my 0? Is that correct?

2

u/branfili Dec 12 '18

Yes

It's easier if you make a list of characters 500 long, and insert your string starting at char 250

Then just print (sol - count('#')*250) (minus offset)

2

u/[deleted] Dec 12 '18

Yeah :-) Or some kind of defaultdict that automatically "expands". You basically only have to store the indices of "#" pots and assume everything else is ".". I think this is the cleanest solution, but now I am too lazy and tired. Might try that tomorrow

2

u/abnew123 Dec 12 '18

yeah. If you set say index 100 in a list to your left most pot in the starting string, that at the end index 100 will still be the left most pot. Obviously no need to use index 100 specifically, it just happened to be the one I used.

2

u/branfili Dec 12 '18

on the start, the first pot is 0

if you've expanded to the left then you have some negative pots

1

u/[deleted] Dec 12 '18

My initial state was:

##.#..#.#..#.####.#########.#...#.#.#......##.#.#...##.....#...#...#.##.#...##...#.####.##..#.#..#.

So i did not expand to the left, right?

4

u/Dosamer Dec 12 '18

First. This is only for solutions

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.

Second: for your first concern:

Based on the notes, for each generation of plants, a given pot has or does not have a plant based on whether that pot (and the two pots on either side of it) had a plant in the last generation.

So it is clear that you only look after the last generation and such nothing changes. It is similar to Conway's game of life.

for your second concern:

Your puzzle input contains a list of pots from 0 to the right

It is clear that the first # is the "number 0" pot

for your third concern: The example was very helpful and once you can reliably generate the last line, it should be trivial to count the number of #'s

I wonder why so many people got to a solution this fast. I think the problem is not too hard if you know what to do....

Once you read the text it is clear what to do and can solve this task quickly.

3

u/abnew123 Dec 12 '18

Also, the example gives all positive transforms (aka to pot). So you need to add like one check (Something like if(string.notmatchtranforms) place = nopot.) in your code. My code works for both the example and the given data set, and that part really didn't take long.

2

u/[deleted] Dec 12 '18

Okay, I am obviously too stupid for this problem. Do you know what I am doing wrong?

def find_match(rule, pots):
	for i in range(len(pots)-5):
		if pots[i:i+5] == rule:
			return i+2
	return None


with open('input') as f:
	lines = f.read().splitlines()
	pots = lines[0].split(": ")[1]
		
	rules = {}
	for rule in lines[2:]:
		condition, replacement = rule.split(" => ")
		rules[condition] = replacement

	for g in range(20):
		newpots = list(pots[:])
		for condition, replacement in rules.items():			
			match = find_match(condition, pots)
			if match:
				newpots[match] = replacement
		pots = "".join(newpots)

	s = 0
	for i,c in enumerate(pots):
		if c == "#":
			s += i
	print(s)

4

u/LampStrike Dec 12 '18

So, there are a couple things that need some work in this code.

  1. You are not considering the ends of your pot line. For example, to determine whether pot 0 has a plant in the next generation, you need to consider the plant state of pots -2, -1, 0, 1, and 2 in this generation. To get the initial negative number pot states, you should assume that the negatives start with no plant (This is somewhere in the problem statement). The way your code is currently set up, you can't actually change the value of any pot with number < 2 or within 2 of the last value in the line. The easiest way to fix this in my opinion is to append a set of 5 '.' to both the front and back of your string prior to searching for matches during each round of the for loop.
  2. Remember that each rule can be applied several times to the pot line. For example, the state '..#..' can appear twice and whatever rule is in your input for that series should be applied every time it shows up in the line. Two ways to fix this.

    1. find_match needs to return a list of all indices for which the rule applies in the given pot list.
    2. Identify each 5 pot subsequence that exists in the current generation and check if each one matches a rule. If it does (and it should since all possible subsequences are defined to a rule in your input), then follow that rule to determine whether the middle pot of the subsequence has a plant in the next generation.

And another thing, in your main logic loop, you are building a list newpots and converting it to a string. But, then in the very first line of the loop, you convert that string back into a list. For this problem, this inefficiency isn't devastating but at-scale, this can become really problematic. Either work with strings fully, or work with lists fully. Don't constantly switch back and forth in your logic for no reason.

3

u/GeneralYouri Dec 12 '18

Straight away in your find_match function you seem to be returning the current state instead of the right half of the input rule.

1

u/[deleted] Dec 12 '18

Yes, I return the position of the index I want to replace. But I have solved it completely differntly now