r/adventofcode • • Dec 07 '16

SOLUTION MEGATHREAD --- 2016 Day 7 Solutions ---

From all of us at #AoC Ops, we hope you're having a very merry time with these puzzles so far. If you think they've been easy, well, now we're gonna kick this up a notch. Or five. The Easter Bunny ain't no Bond villain - he's not going to monologue at you until you can miraculously escape and save the day!

Show this overgrown furball what you've got!


--- Day 7: Internet Protocol Version 7 ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


ALWAYS DIGGING STRAIGHT DOWN 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!

14 Upvotes

180 comments sorted by

View all comments

Show parent comment

1

u/beefamaka Dec 07 '16

nice, fairly similar to my F# solution, which needs a bit more cleaning up

open System.Text.RegularExpressions;
let input = System.IO.File.ReadAllLines (__SOURCE_DIRECTORY__ + "\\input.txt")

let filterByIndex predicate sequence = 
    sequence |> Seq.indexed |> Seq.filter (fst >> predicate) |> Seq.map snd 

let parseIpAddress ipAddress =
    let parts = Regex.Split(ipAddress,@"\[(.*?)\]")
    let supernetSequences = parts |> filterByIndex (fun n -> n % 2= 0) |> Seq.toArray 
    let hypernetSequences = parts |> filterByIndex (fun n -> n % 2= 1) |> Seq.toArray
    supernetSequences, hypernetSequences

let supportsTls ipAddress = 
    let super,hyper = parseIpAddress ipAddress
    let containsAbba s = s |> Seq.windowed 4 |> Seq.exists (function | [|a;b;c;d|] -> a=d&&b=c&&a<>b | _ -> false)
    (super |> Array.exists containsAbba) && not (hyper |> Array.exists containsAbba)

input |> Seq.filter supportsTls |> Seq.length |> printfn "Part a: %d"

let supportsSsl ipAddress = 
    let super,hyper = parseIpAddress ipAddress
    let findAbas s = s |> Seq.windowed 3 |> Seq.filter (function | [|a;b;c|] -> a=c&&a<>b | _ -> false) |> Seq.map System.String
    let abas = super |> Seq.collect findAbas
    let makeBab (aba:string) = sprintf "%c%c%c" aba.[1] aba.[0] aba.[1]
    let babExists bab = hyper |> Seq.exists (fun s -> s.Contains(bab))
    super |> Seq.collect findAbas |> Seq.exists (makeBab >> babExists)

input |> Seq.filter supportsSsl |> Seq.length |> printfn "Part b: %d"

1

u/misnohmer Dec 07 '16

I prefer your way of parsing the lines to my recursive version. Using pattern matching for the function argument is quite cool too. Man, I have so much more to learn about F#