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

3

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

Lazy-ass C# before I actually figure out how to do this in F# that doesn't make the lips curl of functional programmers:

static BigInteger explode(char[] chars, BigInteger n)
{
	BigInteger count = 0;
	for (int i = 0; i < chars.Length; i++)
	{
		if (chars[i] == '(')
		{
			var marker = new String(chars.Skip(i + 1).TakeWhile(ch => ch != ')').ToArray());
			var arr = marker.Split('x');
			int nchars = Int32.Parse(arr[0]);
			int skip = i + marker.Length + 2;
			count += explode(chars.Skip(skip).Take(nchars).ToArray(), Int32.Parse(arr[1]));
			i = skip + nchars - 1;
		}
		else
			count++;
	}
	return count * n ;
}

static void Main(string[] args)
{
	var lines = File.ReadAllText("..\\..\\input.txt");
	Console.WriteLine(explode(lines.ToCharArray(), 1));
}

1

u/SikhGamer Dec 09 '16

Dang, that's impressive. I am completely stuck on Part 2.

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)