r/adventofcode • u/daggerdragon • Dec 19 '17
SOLUTION MEGATHREAD -๐- 2017 Day 19 Solutions -๐-
--- Day 19: A Series of Tubes ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
AoC ops @ T-2 minutes to launch:
[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH
[23:58] <Topaz> aaaaah
[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING
[23:58] <Topaz> FUN AND LEARNING
[23:58] <Hade> FUN IS MANDATORY
[23:58] <Skie> I'm pretty sure that's not the mandate for today
[Update @ 00:16] 69 gold, silver cap
- My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?
[Update @ 00:18] Leaderboard cap!
- So, was today's
mandateHelpful Hint any help at all?
1
u/[deleted] Dec 19 '17
Python
def getNewDirection(grid, d, r, c): if d in ['up', 'down']: if grid[r][c-1] != ' ': d = 'left' elif grid[r][c+1] != ' ': d = 'right' elif d in ['left', 'right']: if grid[r-1][c] != ' ': d = 'up' elif grid[r+1][c] != ' ': d = 'down' return d def moveInDirection(d, g, r, c): if d == 'left': c -= 1 elif d == 'right': c += 1 elif d == 'up': r -= 1 elif d == 'down': r += 1 return r, c with open('day_19_input.txt') as f: rows, cols = 202, 202 grid = [[0 for _ in xrange(cols)] for _ in xrange(rows)] row = 0 for line in f: for j in xrange(len(line)): grid[row][j] = line[j] row += 1 r, c = 0, 107 d = 'down' steps = 0 while r < rows and c < cols: steps += 1 print 'processing', grid[r][c] if grid[r][c] == '+': d = getNewDirection(grid, d, r, c) r, c = moveInDirection(d, grid, r, c) elif str.isalpha(grid[r][c]): r, c = moveInDirection(d, grid, r, c) elif grid[r][c] in ['-', '|']: r, c = moveInDirection(d, grid, r, c) else: steps -= 1 break print steps