]> www.fi.muni.cz Git - aoc.git/blob - 2017/44.pl
Day 25: examining the input
[aoc.git] / 2017 / 44.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 '#' ? 2 : 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 .. 10000000) {
26         my $state = $map{$px,$py};
27         if (!$state) {
28                 $dir = 3 if --$dir < 0;
29         } elsif ($state == 1) {
30                 $sum++;
31         } elsif ($state == 2) {
32                 $dir = 0 if ++$dir > 3;
33         } elsif ($state == 3) {
34                 $dir ^= 2;
35         }
36         $map{$px,$py} = 0 if ++$map{$px,$py} > 3;
37         $py-- if $dir == 0;
38         $px++ if $dir == 1;
39         $py++ if $dir == 2;
40         $px-- if $dir == 3;
41 }
42
43 say $sum;