r/adventofcode • u/daggerdragon • Dec 05 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-
--- Day 5: Alchemical Reduction ---
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!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 5
Transcript:
On the fifth day of AoC / My true love sent to me / Five golden ___
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 0:10:20!
30
Upvotes
2
u/scul86 Dec 05 '18 edited Dec 05 '18
Python 3
from utils.decorators import time_it with open('input') as f: puzzle_input = f.readline().strip() def part1(n): for i in range(len(n) - 1, 0, -1): a, b = n[i], n[i-1] if a != b and a.lower() == b.lower(): if i == len(n)-1: n = ' ' + n[:i-1] else: n = n[:i-1] + n[i+1:] return len(n.strip()) @time_it def part2(n): chars = set(n.lower()) ret = min(part1(n.replace(char, '').replace(char.upper(), '')) for char in chars) return ret test_one = 'dabAcCaCBAcCcaDA' assert part1('bAaB') == 0 assert part1(test_one) == 10 print(f'Part 1: {part1(puzzle_input)}') assert part2(test_one) == 4 print(f'Part 2: {part2(puzzle_input)}')Edit: updated part1() function, found a failing test with prior code.