-
Notifications
You must be signed in to change notification settings - Fork 0
/
d22_2.pl
146 lines (131 loc) · 3.46 KB
/
d22_2.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#! /usr/bin/env perl
# Advent of Code 2017 Day 22 - part 2
# Problem link: http://adventofcode.com/2017/day/22
# Discussion: http://gerikson.com/blog/comp/Advent-of-Code-2017.html#d22
# License: http://gerikson.com/files/AoC2017/UNLICENSE
###########################################################
use 5.016;
use warnings;
use autodie;
#### INIT - load input data from file into array
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 $map;
sub pretty_print;
my $row = 0;
my $lastc = 0;
while (@input) {
my @line = split( //, shift @input );
foreach my $c ( 0 .. $#line ) {
$map->{$row}->{$c} = $line[$c];
}
$row++;
$lastc = scalar @line;
}
# inspection show midpoint
my $pos = $testing ? [ 1, 1 ] : [ 12, 12 ];
# our coordinate system is row/cols: "up" is negative 1st coord
my $dir = [ -1, 0 ];
my $limit = 10000000;
my $moves = 0;
my $infected = 0;
while ( $moves < $limit ) {
# does node exist? if not create it
my $state;
if ( exists $map->{ $pos->[0] }->{ $pos->[1] } ) {
$state = $map->{ $pos->[0] }->{ $pos->[1] };
}
else {
$map->{ $pos->[0] }->{ $pos->[1] } = '.';
$state = '.';
}
# inspect current node, turn, and act on node
if ( $state eq '#' ) {
$dir = turn_right($dir);
$map->{ $pos->[0] }->{ $pos->[1] } = 'F';
}
elsif ( $state eq 'W' ) {
# dir does not change
$map->{ $pos->[0] }->{ $pos->[1] } = '#';
$infected++;
}
elsif ( $state eq 'F' ) {
$dir = my_reverse($dir);
$map->{ $pos->[0] }->{ $pos->[1] } = '.';
}
else { # clean
$dir = turn_left($dir);
$map->{ $pos->[0] }->{ $pos->[1] } = 'W';
}
# move
$pos = [ $pos->[0] + $dir->[0], $pos->[1] + $dir->[1] ];
$moves++;
}
say $infected;
###############################################################################
sub turn_left {
my ($in) = @_;
my $out;
if ( $in->[0] == -1 and $in->[1] == 0 ) { #up
$out = [ 0, -1 ];
}
elsif ( $in->[0] == 0 and $in->[1] == -1 ) { #left
$out = [ 1, 0 ];
}
elsif ( $in->[0] == 1 and $in->[1] == 0 ) { #down
$out = [ 0, 1 ];
}
elsif ( $in->[0] == 0 and $in->[1] == 1 ) { #right
$out = [ -1, 0 ];
}
else {
die "can't parse direction: [ $in->[0], $in->[1] ]";
}
return $out;
}
sub turn_right {
my ($in) = @_;
my $out;
if ( $in->[0] == -1 and $in->[1] == 0 ) { #up
$out = [ 0, 1 ];
}
elsif ( $in->[0] == 0 and $in->[1] == -1 ) { #left
$out = [ -1, 0 ];
}
elsif ( $in->[0] == 1 and $in->[1] == 0 ) { #down
$out = [ 0, -1 ];
}
elsif ( $in->[0] == 0 and $in->[1] == 1 ) { #right
$out = [ 1, 0 ];
}
else {
die "can't parse direction: [ $in->[0], $in->[1] ]";
}
return $out;
}
sub my_reverse {
my ($in) = @_;
my $out;
for my $i ( 0, 1 ) {
$out->[$i] = $in->[$i] == 0 ? 0 : -1 * $in->[$i];
}
return $out;
}
sub pretty_print {
foreach my $r ( sort { $a <=> $b } keys %{$map} ) {
foreach my $c ( sort { $a <= $b } keys %{ $map->{$r} } ) {
if ( exists $map->{$r}->{$c} ) {
print $map->{$r}->{$c};
}
else {
print '.';
}
}
print "\n";
}
print "\n";
}