-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-16-star-2.pl
executable file
·168 lines (147 loc) · 5.16 KB
/
day-16-star-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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/perl
use warnings;
use 5.036;
chomp(my @lines = <>);
my @map = map { [ split(//, $_) ] } @lines;
my $max = 0;
for my $i (0..$#{$map[0]}) {
my $count = &countTiles($i, 0, 0b0010);
$max = $max > $count ? $max : $count;
$count = &countTiles($i, $#map, 0b1000);
$max = $max > $count ? $max : $count;
}
for my $i (0..$#map) {
my $count = &countTiles(0, $i, 0b0001);
$max = $max > $count ? $max : $count;
$count = &countTiles($#{$map[0]}, $i, 0b0100);
$max = $max > $count ? $max : $count;
}
say $max;
sub countTiles($startX, $startY, $startDir) {
my @energyMap = map { [ map { 0 } @{$_} ] } @map;
my %beamsStarted;
# say "@{$map[0]}";
# say "@{$energyMap[0]}";
# beam: { x, y, dir }
# right = 0001,
# down = 0010,
# left = 0100,
# up = 1000
my @beams = ( { 'x' => $startX, 'y' => $startY, 'dir' => $startDir } );
while (@beams) {
my %beam = %{shift @beams};
# say "New beam - x: $beam{'x'}, y: $beam{'y'}, dir: $beam{'dir'}";
# foreach my $b (@beams) {
# say "x: %{$b}{'x'}, y: %{$b}{'y'}, dir: %{$b}{'dir'}";
# }
while (1) {
my $nextTile = $map[$beam{'y'}][$beam{'x'}];
last unless $beam{'y'} >= 0 && $beam{'x'} >= 0;
last unless $nextTile;
# say "x: $beam{'x'}, y: $beam{'y'}, dir: $beam{'dir'}, next tile: $nextTile";
if ($nextTile eq '.') {
$energyMap[$beam{'y'}][$beam{'x'}] = 1;
(my $x, my $y) = &moveDir(\%beam);
$beam{'x'} = $x;
$beam{'y'} = $y;
} elsif ($nextTile eq '/') {
$energyMap[$beam{'y'}][$beam{'x'}] = 1;
$beam{'dir'} = &mirrorDir($beam{'dir'}, '/');
(my $x, my $y) = &moveDir(\%beam);
$beam{'x'} = $x;
$beam{'y'} = $y;
} elsif ($nextTile eq '\\') {
$energyMap[$beam{'y'}][$beam{'x'}] = 1;
$beam{'dir'} = &mirrorDir($beam{'dir'}, '\\');
(my $x, my $y) = &moveDir(\%beam);
$beam{'x'} = $x;
$beam{'y'} = $y;
} elsif ($nextTile eq '|') {
if ($beam{'dir'} == 0b0010 || $beam{'dir'} == 0b1000) {
$energyMap[$beam{'y'}][$beam{'x'}] = 1;
(my $x, my $y) = &moveDir(\%beam);
$beam{'x'} = $x;
$beam{'y'} = $y;
} elsif ($beam{'dir'} == 0b0100 || $beam{'dir'} == 0b0001) {
$energyMap[$beam{'y'}][$beam{'x'}] = 1;
&tryStartBeam(\@beams, \%beamsStarted, $beam{'x'}, $beam{'y'} - 1, 0b1000);
&tryStartBeam(\@beams, \%beamsStarted, $beam{'x'}, $beam{'y'} + 1, 0b0010);
last;
}
} elsif ($nextTile eq '-') {
if ($beam{'dir'} == 0b0010 || $beam{'dir'} == 0b1000) {
$energyMap[$beam{'y'}][$beam{'x'}] = 1;
&tryStartBeam(\@beams, \%beamsStarted, $beam{'x'} + 1, $beam{'y'}, 0b0001);
&tryStartBeam(\@beams, \%beamsStarted, $beam{'x'} - 1, $beam{'y'}, 0b0100);
last;
} elsif ($beam{'dir'} == 0b0100 || $beam{'dir'} == 0b0001) {
$energyMap[$beam{'y'}][$beam{'x'}] = 1;
(my $x, my $y) = &moveDir(\%beam);
$beam{'x'} = $x;
$beam{'y'} = $y;
}
} else {
die "Unknown tile: $nextTile";
}
}
}
my $sum = 0;
foreach my $lineRef (@energyMap) {
# say "@{$lineRef}";
foreach my $v (@{$lineRef}) {
$sum += $v;
}
}
return $sum;
# say $sum;
}
sub tryStartBeam($beamsRef, $beamsStartedRef, $x, $y, $dir) {
my $hash = $x . $y . $dir;
unless ($$beamsStartedRef{$hash}) {
$$beamsStartedRef{$hash} = 1;
push(@{$beamsRef}, { 'x' => $x, 'y' => $y, 'dir' => $dir });
}
}
sub moveDir($beamRef) {
(my $x, my $y, my $dir) = (${$beamRef}{'x'}, ${$beamRef}{'y'}, ${$beamRef}{'dir'});
if ($dir == 0b0001) {
return ($x + 1, $y);
} elsif ($dir == 0b0010) {
return ($x, $y + 1);
} elsif ($dir == 0b0100) {
return ($x - 1, $y);
} elsif ($dir == 0b1000) {
return ($x, $y - 1);
} else {
die "Unknown dir: $dir";
}
}
sub mirrorDir($dir, $mirror) {
if ($mirror eq '/') {
if ($dir == 0b0001) {
return 0b1000;
} elsif ($dir == 0b0010) {
return 0b0100;
} elsif ($dir == 0b0100) {
return 0b0010;
} elsif ($dir == 0b1000) {
return 0b0001;
} else {
die "Unknown dir: $dir in mirror: $mirror";
}
} elsif ($mirror eq '\\') {
if ($dir == 0b0001) {
return 0b0010;
} elsif ($dir == 0b0010) {
return 0b0001;
} elsif ($dir == 0b0100) {
return 0b1000;
} elsif ($dir == 0b1000) {
return 0b0100;
} else {
die "Unknown dir: $dir in mirror: $mirror";
}
} else {
die "Unknown mirror: $mirror";
}
}