-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd02_1.pl
52 lines (46 loc) · 1.43 KB
/
d02_1.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
#!/usr/bin/perl
# Advent of Code 2016 Day 2 - part 1
# Problem link: http://adventofcode.com/2016/day/2
# Discussion: http://gerikson.com/blog/comp/Advent-of-Code-2016.html#d02
# License: http://gerikson.com/files/AoC2016/UNLICENSE
###########################################################
use 5.016; # implies strict, provides 'say'
use warnings;
use autodie;
#### INIT
my $testing = 0;
my @input;
my $file = $testing ? 'test.txt' : 'input.txt';
{
open( my $fh, '<', "$file" );
while (<$fh>) { chomp; s/\r//gm; push @input, $_; }
}
### CODE
my $keypad = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ];
my $next_move = { U => [ 0, 1 ],
D => [ 0, -1 ],
L => [ -1, 0 ],
R => [ 1, 0 ] };
# start with a Cartesian grid with the origin at 5:
# 1 2 3
# 4 5 6
# 7 8 9
my $key = [ 0, 0 ];
my $solution;
foreach my $line (@input) {
my @instructions = split( //, $line );
foreach my $move (@instructions) {
my $next = [ $key->[0] + $next_move->{$move}->[0],
$key->[1] + $next_move->{$move}->[1] ];
if ( abs( $next->[0] ) > 1 or abs( $next->[1] ) > 1 ) {
next;
} else {
$key = $next;
}
}
# To get the keys from the arrayref,
# rotate 90 degrees counter-clockwise: ( x , y ) -> ( -y, x )
# and translate [+1,+1]
$solution .= $keypad->[ -$key->[1] + 1 ]->[ $key->[0] + 1 ];
}
say $solution;