Skip to content

Commit 18ac5d3

Browse files
committed
day 13 add
1 parent 8fb97e0 commit 18ac5d3

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

d13_1.pl

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/perl
2+
# Advent of Code 2016 Day 13 - part 1
3+
# Problem link: http://adventofcode.com/2016/day/13
4+
# Discussion: http://gerikson.com/blog/comp/Advent-of-Code-2016.html#d13
5+
# License: http://gerikson.com/files/AoC2016/UNLICENSE
6+
###########################################################
7+
use 5.016; # implies strict, provides 'say'
8+
use warnings;
9+
use autodie;
10+
11+
#### INIT - load input data into array
12+
my $testing = 0;
13+
my ( $input, $target ) = ( 1358, [ 31, 39 ] );
14+
if ($testing) { $input = 10; $target = [ 7, 4 ]; }
15+
16+
### CODE
17+
my $maze;
18+
my $seen;
19+
20+
sub count_ones {
21+
22+
# http://docstore.mik.ua/orelly/perl/cookbook/ch02_05.htm
23+
my $str = unpack( "B32", pack( "N", shift ) );
24+
$str =~ s/^0+(?=\d)//;
25+
26+
my $count = 0;
27+
for my $c ( split( //, $str ) ) {
28+
$count++ if ( $c == 1 );
29+
}
30+
return $count;
31+
}
32+
33+
sub is_open {
34+
my ( $x, $y ) = @_;
35+
if ( $x < 0 or $y < 0 ) { return 0 }
36+
if ( exists $maze->{$x}->{$y} ) {
37+
return $maze->{$x}->{$y};
38+
}
39+
40+
my $fact = ( $x * $x + 3 * $x + 2 * $x * $y + $y + $y * $y );
41+
$fact += $input;
42+
my $ones = count_ones($fact);
43+
if ( $ones % 2 == 0 ) {
44+
$maze->{$x}->{$y} = 1;
45+
return 1;
46+
} else {
47+
$maze->{$x}->{$y} = 0;
48+
return 0;
49+
}
50+
}
51+
52+
my @states = ( [ 0, [ 1, 1 ] ] );
53+
LOOP: {
54+
while (@states) {
55+
my $move = shift @states;
56+
my $step = $move->[0];
57+
my ( $x, $y ) = @{ $move->[1] };
58+
59+
if ( exists $seen->{$x}->{$y} ) {
60+
next;
61+
} else {
62+
$seen->{$x}->{$y}++;
63+
}
64+
65+
# try to move
66+
$step += 1;
67+
my @new;
68+
push @new,
69+
( [ $x + 1, $y ], [ $x - 1, $y ],
70+
[ $x, $y + 1 ], [ $x, $y - 1 ] );
71+
72+
while (@new) {
73+
my $el = shift @new;
74+
my ( $new_x, $new_y ) = @$el;
75+
if ( is_open( $new_x, $new_y ) ) {
76+
if ( $new_x == $target->[0]
77+
and $new_y == $target->[1] )
78+
{
79+
80+
#break out reporting sucess
81+
say "steps: $step";
82+
last LOOP;
83+
}
84+
push @states, [ $step, $el ];
85+
}
86+
}
87+
88+
}
89+
}

d13_2.pl

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/perl
2+
# Advent of Code 2016 Day 13 - part 2
3+
# Problem link: http://adventofcode.com/2016/day/13
4+
# Discussion: http://gerikson.com/blog/comp/Advent-of-Code-2016.html#d13
5+
# License: http://gerikson.com/files/AoC2016/UNLICENSE
6+
###########################################################
7+
use 5.016; # implies strict, provides 'say'
8+
use warnings;
9+
use autodie;
10+
11+
#### INIT - load input data into array
12+
my $testing = 0;
13+
my ( $input, $target ) = ( 1358, [ 31, 39 ] );
14+
if ($testing) { $input = 10; $target = [ 7, 4 ]; }
15+
16+
### CODE
17+
my $maze;
18+
my $seen;
19+
20+
sub count_ones { # http://docstore.mik.ua/orelly/perl/cookbook/ch02_05.htm
21+
my $str = unpack( "B32", pack( "N", shift ) );
22+
$str =~ s/^0+(?=\d)//;
23+
my $count = 0;
24+
for my $c ( split( //, $str ) ) {
25+
$count++ if ( $c == 1 );
26+
}
27+
return $count;
28+
}
29+
30+
sub is_open {
31+
my ( $x, $y ) = @_;
32+
if ( $x < 0 or $y < 0 ) { return 0 }
33+
if ( exists $maze->{$x}->{$y} ) {
34+
return $maze->{$x}->{$y};
35+
}
36+
37+
my $fact = ( $x * $x + 3 * $x + 2 * $x * $y + $y + $y * $y );
38+
$fact += $input;
39+
my $ones = count_ones($fact);
40+
if ( $ones % 2 == 0 ) {
41+
$maze->{$x}->{$y} = 1;
42+
return 1;
43+
} else {
44+
$maze->{$x}->{$y} = 0;
45+
return 0;
46+
}
47+
}
48+
49+
my @states = ( [ 0, [ 1, 1 ] ] );
50+
51+
while (@states) {
52+
my $move = shift @states;
53+
my $step = $move->[0];
54+
my ( $x, $y ) = @{ $move->[1] };
55+
56+
if ( exists $seen->{$x}->{$y} ) {
57+
next;
58+
} else {
59+
$seen->{$x}->{$y}++;
60+
}
61+
62+
# try to move
63+
$step += 1;
64+
next if $step > 50;
65+
my @new;
66+
push @new,
67+
( [ $x + 1, $y ], [ $x - 1, $y ], [ $x, $y - 1 ], [ $x, $y + 1 ] );
68+
while (@new) {
69+
my $el = shift @new;
70+
my ( $new_x, $new_y ) = @$el;
71+
if ( is_open( $new_x, $new_y ) ) {
72+
push @states, [ $step, $el ];
73+
}
74+
}
75+
}
76+
77+
my $count = 0;
78+
for my $x ( keys %{$seen} ) {
79+
for my $y ( keys %{ $seen->{$x} } ) {
80+
$count++;
81+
}
82+
}
83+
say $count;

0 commit comments

Comments
 (0)