-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.pl
117 lines (97 loc) · 2.61 KB
/
day18.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
use strict;
use warnings;
use feature 'say';
use lib './';
use assertions;
open(my $file, "<", "input/day18.txt") or die "Can't open input file";
my @lines = ();
while (my $line = <$file>) {
chomp($line);
push(@lines, $line);
}
close $file;
my %scan = ();
foreach my $line (@lines) {
# say $line;
$scan{$line} = 1;
}
sub adjacentPositions {
my ($x, $y, $z) = split /,/, (shift @_);
my @adjs = ();
push(@adjs, sprintf("%d,%d,%d", $x-1, $y , $z ));
push(@adjs, sprintf("%d,%d,%d", $x+1, $y , $z ));
push(@adjs, sprintf("%d,%d,%d", $x , $y-1, $z ));
push(@adjs, sprintf("%d,%d,%d", $x , $y+1, $z ));
push(@adjs, sprintf("%d,%d,%d", $x , $y , $z-1));
push(@adjs, sprintf("%d,%d,%d", $x , $y , $z+1));
return @adjs;
}
sub adjacentPositions2 {
my ($x, $y, $z) = split /,/, (shift @_);
my @adjs = ();
if ($x >= 0) {
push(@adjs, sprintf("%d,%d,%d", $x-1, $y , $z ));
}
if ($x <= 20) {
push(@adjs, sprintf("%d,%d,%d", $x+1, $y , $z ));
}
if ($y >= 0) {
push(@adjs, sprintf("%d,%d,%d", $x , $y-1, $z ));
}
if ($y <= 20) {
push(@adjs, sprintf("%d,%d,%d", $x , $y+1, $z ));
}
if ($z >= 0) {
push(@adjs, sprintf("%d,%d,%d", $x , $y , $z-1));
}
if ($z <= 20) {
push(@adjs, sprintf("%d,%d,%d", $x , $y , $z+1));
}
return @adjs;
}
no warnings 'recursion';
sub floodFill {
my $pos = shift @_;
if (defined $scan{$pos}) {
return;
}
$scan{$pos} = 2;
foreach my $adj (adjacentPositions2 $pos) {
floodFill($adj);
}
}
floodFill("0,0,0");
my $totalSides1 = 0;
my $totalSides2 = 0;
for (my $x = 0; $x < 20; $x++) {
for (my $y = 0; $y < 20; $y++) {
for (my $z = 0; $z < 20; $z++) {
my $pos = "$x,$y,$z";
my $sides1 = 0;
my $sides2 = 0;
if (defined $scan{$pos} && $scan{$pos} == 1) {
# say "$x, $y, $z";
$sides1 = 6;
$sides2 = 6;
foreach my $adj (adjacentPositions $pos) {
if ($adj eq "2,2,5") {
#say $scan{$adj};
#die;
}
if (defined $scan{$adj} && $scan{$adj} == 1) {
$sides1--;
$sides2--;
} elsif (!defined $scan{$adj}) {
$sides2--;
}
}
}
$totalSides1 += $sides1;
$totalSides2 += $sides2;
}
}
}
# Part 1
say $totalSides1;
# Part 2
say $totalSides2;