]> www.fi.muni.cz Git - aoc.git/blob - 2017/43.pl
Day 25: examining the input
[aoc.git] / 2017 / 43.pl
1 #!/usr/bin/perl
2
3 use v5.30;
4 use strict;
5
6 my %map;
7 $; = ',';
8 my $y = 0;
9 my $px;
10 while (<>) {
11         my $x = 0;
12         chomp;
13         for (split //) {
14                 $map{$x,$y} = $_ eq '#' ? 1 : 0;
15                 $x++;
16         }
17         $px = ($x-1)/2;
18         $y++;
19 }
20 my $py = ($y-1)/2;
21 my $dir = 0; # up right down left
22
23 my $sum;
24
25 for (1 .. 10_000) {
26         my $state = $map{$px,$py};
27         if ($state) {
28                 $dir = 0 if ++$dir > 3;
29         } else {        
30                 $dir = 3 if --$dir < 0;
31                 $sum++;
32         }
33         $map{$px,$py} = !$state;
34         $py-- if $dir == 0;
35         $px++ if $dir == 1;
36         $py++ if $dir == 2;
37         $px-- if $dir == 3;
38 }
39
40 say $sum;