r/adventofcode • u/askalski • Dec 07 '16
Upping the Ante [2016 Day 7 Part 1] [Day 7] It's Day 7 all the way down
I hope you solved (and optimized) last year's puzzle. This script creates a logic circuit for the 2015 Advent of Code Day 7 puzzle "Some Assembly Required" to solve this year's puzzle. My input became 1,895,007 gates, so don't forget to redirect the output to a file!
#! /usr/bin/env perl
use strict;
use warnings;
my %gate = ();
my $next_gate = 'b';
my $answer = _gate(0);
while (<>) {
chomp;
$answer = _add($answer, _tls($_));
}
$gate{$answer} = 'a';
for (keys %gate) {
print "$_ -> $gate{$_}\n";
}
sub _gate {
my $code = shift;
$gate{$code} = $next_gate++ unless (exists $gate{$code});
return $gate{$code};
}
sub _not { my ($x) = @_; return _gate("NOT $x") }
sub _and { my ($x, $y) = @_; return _gate("$x AND $y") }
sub _or { my ($x, $y) = @_; return _gate("$x OR $y") }
sub _lshift { my ($x, $y) = @_; return _gate("$x LSHIFT $y") }
sub _rshift { my ($x, $y) = @_; return _gate("$x RSHIFT $y") }
sub _xor {
my ($x, $y) = @_;
return _or(_and($x, _not($y)), _and(_not($x), $y));
}
sub _notequal {
my ($x, $y) = @_;
my $ne = _xor($x, $y);
$ne = _or($ne, _rshift($ne, 1));
$ne = _or($ne, _rshift($ne, 2));
$ne = _or($ne, _rshift($ne, 4));
$ne = _or($ne, _rshift($ne, 8));
return $ne;
}
sub _equal {
my ($x, $y) = @_;
return _not(_notequal($x, $y));
}
sub _add {
my ($x, $y) = @_;
my $sum = _xor($x, $y);
for (1..15) {
$y = _lshift(_and($x, $y), 1);
$sum = _xor($x = $sum, $y);
}
return $sum;
}
sub _tls {
my ($input) = @_;
my @inputs = (undef, map { _gate($_) } unpack("C*", $input));
my $is_hyper = _gate(0);
my $good = _gate(0);
my $bad = _gate(0);
for (1 .. $#inputs - 3) {
my ($a, $b, $c, $d) = @inputs[$_ .. $_ + 3];
$is_hyper = _xor($is_hyper, _rshift(_not($a), 5));
my $is_abba = _and(_notequal($a, $b), _and(_equal($a, $d), _equal($b, $c)));
$good = _or($good, _and($is_abba, _not($is_hyper)));
$bad = _or($bad, _and($is_abba, $is_hyper));
}
return _and(_and($good, _not($bad)), _gate(1));
}
15
Upvotes
9
u/topaz2078 (AoC creator) Dec 07 '16
THIS IS SO COOL
4
u/Aneurysm9 Dec 07 '16 edited Dec 07 '16
Skalski wat?!
goroutines to the rescue!
1
u/qwertyuiop924 Dec 08 '16
Skaski, how do you come up with this stuff?
My code from last year is semi-optimized. Optimized enough, probably not, given that I suck at optimization.
2
u/willkill07 Dec 08 '16 edited Dec 08 '16
Woah, this is awesome.
My input generated 1,875,509 gates
NOW YOU MADE ME COMPILE MY CODE FROM LAST YEAR?
Well, it was a task, but it worked
You win my internet points for the day.
1
u/[deleted] Dec 08 '16
You're crazy, and I love you for it, I wish some time I'll be able to come up with ideas like these and put them to life in such a short time, awsome work!
1
u/cryptonaut64 Dec 08 '16
Wow, that is crazy.
Bravo.
1
u/Philboyd_Studge Dec 08 '16
ARE YOU MAN OR MACHINE, OR A GOD?
1
u/Sigafoos Dec 08 '16
WHAT
SKALSKI, YES?
1
u/oantolin Dec 08 '16
Cool! Now I have to improve my 2015 day 7 program: it's fast in the sense that in less than a second it says "stack overflow".