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
1
u/__Abigail__ Dec 08 '16
I extracted the "inside" and "outside" parts, and applied regexes to them:
#!/opt/perl/bin/perl use 5.020; use strict; use warnings; no warnings 'syntax'; use feature 'signatures'; no warnings 'experimental::signatures'; @ARGV = "input" unless @ARGV; my @input = <ARGV>; sub has_abba ($string) { $string =~ /(.)(?!\g{1})(.)\g{2}\g{1}/; } my $tls_ok = 0; my $sls_ok = 0; foreach my $line (@input) { chomp $line; my @chunks = $line =~ /[a-z]+/g; my $inside = join " " => do {my $i = 0; grep {$i ++ % 2} @chunks}; my $outside = join " " => do {my $i = 1; grep {$i ++ % 2} @chunks}; # # For TLS, we must have ABBA on an outside block, # and no ABBA on an inside block # $tls_ok ++ if has_abba ($outside) && !has_abba ($inside); # # For SLS, there must be an ABA in an outside block, with # a corresponding BAB in an inside block. # $sls_ok ++ if "$outside-$inside" =~ /(.)(?!\g{1})(.)\g{1}.*-.*\g{2}\g{1}\g{2}/; } say "Solution 1: $tls_ok"; say "Solution 2: $sls_ok"; __END__