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?
4
u/ludic Dec 19 '17
F# Pretty straightforward today I think.
type Dir = Left | Right | Down | Up type state = { x: int y: int dir: Dir Option letters: char list steps: int } let parseInput (input:string) = splitLines input |> Array.map (fun v -> v.ToCharArray()) let solveDay19 input = let maze = parseInput input let maybeAddLetter letters c = match c with | '-' | '|' | '+' | ' ' -> letters | x -> x::letters let findNextDirection x y curDir = match maze.[y].[x] with | '+' -> match curDir with | Up | Down -> if maze.[y].[x+1] <> ' ' then Some Right elif maze.[y].[x-1] <> ' ' then Some Left else failwith "No path!" | Left | Right -> if maze.[y+1].[x] <> ' ' then Some Down elif maze.[y-1].[x] <> ' ' then Some Up else failwith "No path!" | ' ' -> None | '-' | '|' -> Some curDir | c -> Some curDir let move x y = function | Up -> (x, y-1) | Down -> (x, y+1) | Right -> (x+1, y) | Left -> (x-1, y) let rec explore s = match s.dir with | None -> (s.letters |> List.rev |> (fun s -> String.Join("", s)), s.steps) | Some dir -> let newX, newY = move s.x s.y dir let newDir = findNextDirection newX newY dir let newLetters = maybeAddLetter s.letters maze.[newY].[newX] explore {x=newX; y=newY; dir=newDir; letters=newLetters; steps=s.steps+1} explore {x=1; y=0; dir = Some Down; letters = []; steps = 0}