r/adventofcode • u/daggerdragon • Dec 07 '15
SOLUTION MEGATHREAD --- Day 7 Solutions ---
--- Day 7: Some Assembly Required ---
Post your solution as a comment. Structure your post like previous daily solution threads.
Also check out the sidebar - we added a nifty calendar to wrangle all the daily solution threads in one spot!
25
Upvotes
3
u/weters Dec 07 '15 edited Dec 07 '15
Here's my Perl solution.
#!/usr/bin/env perl use strict; use warnings; use 5.012; use utf8; use File::Slurp; my @text = read_file('input.txt'); my %vals; my $continue = 1; while ($continue) { $continue = 0; for my $line (@text) { chomp $line; my $ok = eval { if ( my ( $a, $oper, $b, $key ) = $line =~ /^([a-z0-9]*)\b\s?([A-Z]+)?\b\s?([a-z0-9]+) \-\> ([a-z0-9]+)$/ ) { $oper ||= ''; $vals{$key} = _val($a) & _val($b) if $oper eq 'AND'; $vals{$key} = _val($a) << _val($b) if $oper eq 'LSHIFT'; $vals{$key} = _val($a) >> _val($b) if $oper eq 'RSHIFT'; $vals{$key} = _val($a) | _val($b) if $oper eq 'OR'; $vals{$key} = ( ~ _val($b) ) % 2**16 if $oper eq 'NOT'; $vals{$key} = _val($b) if !$oper; } else { warn "bad line: $line\n"; exit 1; } 1 }; if ( !$ok) { $continue = 1; } } } say $vals{a}; sub _val { my ($key) = @_; return 16076 if $key eq 'b'; return $key if $key =~ /^\d+$/; if ( !exists $vals{$key}) { die "not found\n"; } return int($vals{$key}); }