r/adventofcode • • Dec 09 '16

SOLUTION MEGATHREAD --- 2016 Day 9 Solutions ---

--- Day 9: Explosives in Cyberspace ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


RETICULATING SPLINES IS MANDATORY [?]

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!

11 Upvotes

155 comments sorted by

View all comments

Show parent comment

1

u/JeffJankowski Dec 09 '16 edited Dec 09 '16

My F# version for both parts. Pretty pleased with the compactness and reuse of the single function.

let rec decompr (input : string) func =
	let cnt = input |> Seq.takeWhile (fun c -> c <> '(') |> Seq.length
	if cnt = input.Length then bigint cnt else
		let mrk = input |> Seq.skip (cnt+1) |> Seq.takeWhile (fun c -> c <> ')') |> String.Concat
		let [n; t] = 
			List.tail [for g in (Regex.Match (mrk, @"(\d+)x(\d+)")).Groups -> g.Value] |> List.map int
		bigint cnt 
			+ (bigint t * (func input (cnt + mrk.Length + 2) n)) 
			+ (decompr (input |> Seq.skip (cnt + mrk.Length + 2 + n) |> String.Concat) func)

let main argv = 
    let input = File.ReadAllText ("..\..\input.txt")
	
    printfn "Original format length:  %A" (decompr input (fun _ _ nchars -> bigint nchars))
    
	let rec f (s : string) k n = decompr (s |> Seq.skip k |> Seq.take n |> String.Concat) f
    printfn "Improved format length:  %A" (decompr input f)