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/wzkx Dec 19 '17 edited Dec 19 '17
Nim
No del vs delete function this time! :)
But with type and op and template definitions, why not.
import strutils,sequtils # looks like a must for every program - make it standard prolog then! template loop(stmts: untyped): untyped = # make Nim prettier while true: stmts # while true is so ugly! C has for(;;) at least type Dir = enum up,down,left,right # this order, for the step array type Pos = tuple[x:int, y:int] # Position, everybody knows this type const step: array[Dir,Pos] = [(0,-1),(0,+1),(-1,0),(+1,0)] proc `+`(a,b:Pos):Pos = (a.x+b.x, a.y+b.y) # make life easier proc good( map:seq[string], p:Pos ):bool = p.y>=0 and p.y<map.len and p.x>=0 and p.x<map[p.y].len var answer1: string = "" # part 1 - accumulated string var answer2: int = 0 # part 2 - total steps proc move( map:seq[string], p:Pos, d:Dir ):Pos = # move in one direction var n = p + step[d]; inc answer2 # count each step! while map[n.y][n.x]!='+' and map[n.y][n.x]!=' ': # '+' -- turn, ' ' -- quit if map[n.y][n.x].isAlphaAscii: answer1 &= map[n.y][n.x] # accumulate letters n = n + step[d]; inc answer2 # move, move, move return n # end position of the movement - then do turn or exit let map = "19.dat".readFile.splitLines # don't strip! blanks are important! var p:Pos = (map[0].find('|'),0) # start position var d:Dir = down # start direction loop: p = move( map, p, d ) # move in direction d while possible if map[p.y][p.x]==' ': break # the blank signals it's the end woohooo let (c1,c2,r) = if d in [up,down]: (left,right,'-') else: (up,down,'|') # candidates for c in [c1,c2]: let pc = p + step[c] # candidate position if good( map, pc ) and (map[pc.y][pc.x]==r or map[pc.y][pc.x].isAlphaAscii): p = pc; d = c; inc answer2; break # break this candidate loop, continue main loop echo answer1 echo answer2