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!

18 Upvotes

257 comments sorted by

View all comments

Show parent comment

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