-
Notifications
You must be signed in to change notification settings - Fork 0
/
d07.pl
77 lines (70 loc) · 2.33 KB
/
d07.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/perl
# Advent of Code 2015 Day 7 - complete solution
# Problem link: http://adventofcode.com/2015/day/7
# Discussion: http://gerikson.com/blog/comp/adventofcode/Advent-of-Code-2015.html#d7
# License: http://gerikson.com/files/AoC2015/UNLICENSE
###########################################################
use strict;
use warnings;
my $file = 'input.txt';
open F, "<$file" or die "can't open $file: $!\n";
my @statements;
my %solutions;
while (<F>) {
chomp;
s/\r//gm;
my ( $lhs, $res ) = ( $_ =~ m/^(.*) -> (\S+)$/ );
push @statements, [ $res, $lhs ];
}
while ( !defined $solutions{'a'} ) {
foreach my $stmt (@statements) {
my $sought = $stmt->[0];
if ( $stmt->[1] =~ m/^(\S+) (AND|OR|LSHIFT|RSHIFT) (\S+)$/ ) {
my ( $a, $op, $b ) = ( $1, $2, $3 );
if ( $a =~ m/\d+/ ) {
} elsif ( $a =~ m/\S+/ ) {
$a = $solutions{$a};
} else {
$a = undef;
}
if ( $b =~ m/\d+/ ) {
} elsif ( $b =~ m/\S+/ ) {
$b = $solutions{$b};
} else {
$b = undef;
}
if ( $op eq 'AND' ) {
next unless ( defined $a and defined $b );
$solutions{$sought} = $a & $b;
} elsif ( $op eq 'OR' ) {
next unless ( defined $a and defined $b );
$solutions{$sought} = $a | $b;
} elsif ( $op eq 'LSHIFT' ) {
next unless ( defined $a and defined $b );
$solutions{$sought} = $a << $b;
} elsif ( $op eq 'RSHIFT' ) {
next unless ( defined $a and defined $b );
$solutions{$sought} = $a >> $b;
}
} elsif ( $stmt->[1] =~ m/^NOT (\S+)$/ ) {
my $b = $1;
if ( $b =~ m/\d+/ ) {
} elsif ( $b =~ m/\S+/ ) {
$b = $solutions{$b};
} else {
$b = undef;
}
$solutions{$sought} = ~$b if defined $b;
} else {
my $b = $stmt->[1];
if ( $b =~ m/\d+/ ) {
} elsif ( $b =~ m/\S+/ ) {
$b = $solutions{$b};
} else {
$b = undef;
}
$solutions{$sought} = $b if defined $b;
}
}
}
print $solutions{'a'}, "\n";