Skip to content

Instantly share code, notes, and snippets.

@saucecode
Created December 8, 2017 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saucecode/60c08c3ce127ead44a8b8ca586c5ec23 to your computer and use it in GitHub Desktop.
Save saucecode/60c08c3ce127ead44a8b8ca586c5ec23 to your computer and use it in GitHub Desktop.
AoC day 8 feat. the ugliest code I've ever written -- script generates scripts that generate solutions to both parts
# generates a python script which runs the challenge input as code then prints out
# the largest integer in `locals()` and the largest value seen
with open('day8challengeinput','r') as f:
lines = f.read().split('\n')
def generate_code_from_input():
headcode = 'largest = 0'
excode = ''
symbols = []
for line in lines:
p = ' '.join(line.split(' ')[3:]) + ':\n\t' + ' '.join(line.split(' ')[:3])
symbol = p.split(' ')
symbol[1] = '_' + symbol[1]
other_symbol = '_' + p.split('\t')[1].split(' ')[0]
symbols.append(symbol[1])
excode += ' '.join(symbol).replace('\t','\t_') + '\n\tif ' + other_symbol + '>largest:\n\t\tlargest = ' + other_symbol + '\n\t\tprint("' + other_symbol + '",largest)\n'
excode = excode.replace(' inc', ' +=').replace(' dec', ' -=')
declarecode = '\n'.join( x + ' = 0' for x in symbols )
compcode = '''locale = dict(locals())
del locale['largest']
print( 'largest value at the end:', max( [locale[j] for j in locale if type(locale[j]) == int] ) )
print( 'largest value seen:', largest )'''
with open('aoc8_generated.py','w') as f:
f.write(headcode + '\n\n' + declarecode + '\n\n' + excode + '\n\n' + compcode)
generate_code_from_input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment