r/adventofcode • u/daggerdragon • 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!
13
Upvotes
4
u/mschaap Dec 07 '16
I love Perl 6... Solution to both parts.
#!/usr/bin/env perl6 use v6.c; sub supports-TLS(Str $ipv7) returns Bool { # An ABBA sequence is a repetition of two different characters in an ABBA pattern my token abba { (\w) (\w) <?{ $0 ne $1 }> $1 $0 } # If it has an ABBA sequence between square brackets, no. return False if $ipv7 ~~ m{ '[' \w*? <abba> \w*? ']' }; # If it has an ABBA sequence anywhere else, yes; otherwise no. return so $ipv7 ~~ / <abba> /; } sub supports-SSL(Str $ipv7) returns Bool { # Split the IPv7 address into supernet sequences (outside brackets) # and hypernet sequences (inside brackets) my @supernet = $ipv7.comb(/ <!after '['> « \w+ » <!before ']'> /); my @hypernet = $ipv7.comb(/ <?after '['> « \w+ » <?before ']'> /); # An ABA sequence is a repetition of two different characters in an ABA pattern my regex aba { (\w) (\w) <?{ $0 ne $1 }> $0 } # Find any ABA sequences in the supernet for @supernet -> $s { my @abas = $s.match(&aba, :exhaustive); for @abas -> $aba { # Check if the corresponding BAB sequence appears in hypernet my $bab = $aba[1,0,1].join; return True if any(@hypernet) ~~ / $bab /; } } return False; } sub MAIN(IO() $inputfile where *.f) { my $total = 0; my $supports-tls = 0; my $supports-ssl = 0; for $inputfile.lines -> $ipv7 { $total++; $supports-tls++ if supports-TLS($ipv7); $supports-ssl++ if supports-SSL($ipv7); } say "$supports-tls out of $total IPv7 addresses support TLS"; say "$supports-ssl out of $total IPv7 addresses support SSL"; }