-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pl
184 lines (146 loc) · 4.56 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use strict;
use warnings;
use feature 'switch';
no warnings 'recursion';
no warnings 'experimental';
sub parse_file_to_2d_array {
my ($filename) = @_;
my @data;
open(my $fh, '<', $filename) or die "Cannot open file $filename";
while (my $line = <$fh>) {
chomp $line;
push @data, [split //, $line];
}
close($fh);
return \@data;
}
sub print_2d_array {
my ($array_ref) = @_;
foreach my $row (@$array_ref) {
foreach my $elem (@$row) {
print $elem;
}
print "\n";
}
}
sub create_zero_filled_2d_array {
my ($array_ref) = @_;
my @zero_array;
for my $row (@$array_ref) {
push @zero_array, [(0) x scalar @$row];
}
return \@zero_array;
}
sub count_ones_in_2d_array {
my ($array_ref) = @_;
my $count = 0;
foreach my $row (@$array_ref) {
foreach my $elem (@$row) {
$count++ if $elem == 1;
}
}
return $count;
}
sub apply_dir {
my ($pos, $dir) = @_;
my ($row, $col) = @$pos;
my ($row_dir, $col_dir) = @$dir;
return [$row + $row_dir, $col + $col_dir];
}
sub run_beam_c {
my ($cache, $array_ref, $energized_array, $pos, $dir) = @_;
my ($row, $col) = @$pos;
my ($row_dir, $col_dir) = @$dir;
my $key = $row . ',' . $col . ',' . $row_dir . ',' . $col_dir;
if (exists $cache->{$key}) {
return;
}
$cache->{$key} = 1;
run_beam($cache, $array_ref, $energized_array, $pos, $dir);
}
sub run_beam {
my ($cache, $array_ref, $energized_array, $pos, $dir) = @_;
my ($row, $col) = @$pos;
if ($row < 0 || $row >= @$array_ref || $col < 0 || $col >= @{$array_ref->[$row]}) {
return;
}
my $value = $array_ref->[$row][$col];
$energized_array->[$row][$col] = 1;
my ($row_dir, $col_dir) = @$dir;
given ($value) {
when (".") {
run_beam_c($cache, $array_ref, $energized_array, apply_dir($pos, $dir), $dir);
}
when ("/") {
my $newDir = [-$col_dir, -$row_dir];
run_beam_c($cache, $array_ref, $energized_array, apply_dir($pos, $newDir), $newDir);
}
when ("\\") {
my $newDir = [$col_dir, $row_dir];
run_beam_c($cache, $array_ref, $energized_array, apply_dir($pos, $newDir), $newDir);
}
when ("-") {
if ($row_dir == 0) {
run_beam_c($cache, $array_ref, $energized_array, apply_dir($pos, $dir), $dir);
} else {
my $newDir = [$col_dir, $row_dir];
run_beam_c($cache, $array_ref, $energized_array, apply_dir($pos, $newDir), $newDir);
my $newDir2 = [$col_dir, -$row_dir];
run_beam_c($cache, $array_ref, $energized_array, apply_dir($pos, $newDir2), $newDir2);
}
}
when ("|") {
if ($col_dir == 0) {
run_beam_c($cache, $array_ref, $energized_array, apply_dir($pos, $dir), $dir);
} else {
my $newDir = [$col_dir, $row_dir];
run_beam_c($cache, $array_ref, $energized_array, apply_dir($pos, $newDir), $newDir);
my $newDir2 = [-$col_dir, $row_dir];
run_beam_c($cache, $array_ref, $energized_array, apply_dir($pos, $newDir2), $newDir2);
}
}
}
}
sub calc_energized_cells {
my ($array_ref, $start_pos, $start_dir) = @_;
my $energized_array = create_zero_filled_2d_array($array_ref);
run_beam({}, $array_ref, $energized_array, $start_pos, $start_dir);
my $energized_cells = count_ones_in_2d_array($energized_array);
}
sub solve {
my ($filename) = @_;
my $array_ref = parse_file_to_2d_array($filename);
my $energized_cells = calc_energized_cells($array_ref, [0,0], [0,1]);
print $energized_cells . "\n";
}
sub solve2 {
my ($filename) = @_;
my $array_ref = parse_file_to_2d_array($filename);
my $best_value = 0;
my $last_col = @{$array_ref->[0]};
my $last_row = @$array_ref;
for (my $col = 0; $col < $last_col; $col++) {
my $energized_cells = calc_energized_cells($array_ref, [0,$col], [1,0]);
if ($energized_cells > $best_value) {
$best_value = $energized_cells;
}
$energized_cells = calc_energized_cells($array_ref, [$last_row - 1,$col], [-1,0]);
if ($energized_cells > $best_value) {
$best_value = $energized_cells;
}
}
for (my $row = 0; $row < $last_row; $row++) {
my $energized_cells = calc_energized_cells($array_ref, [$row,0], [0,1]);
if ($energized_cells > $best_value) {
$best_value = $energized_cells;
}
$energized_cells = calc_energized_cells($array_ref, [$row, $last_col - 1], [0,-1]);
if ($energized_cells > $best_value) {
$best_value = $energized_cells;
}
}
print $best_value . "\n";
}
my $filename = 'input';
solve($filename);
solve2($filename);