-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.pl
executable file
·102 lines (82 loc) · 1.99 KB
/
day08.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
#!/usr/bin/env perl
#
# Note: Had a little trouble solving this because the 'U' looks like a 'V'
# to me!
#
use strict;
use warnings;
use utf8;
use Path::Tiny;
{ package Image;
sub count_digits {
my ($str, $digit) = @_;
my $count = 0;
for my $c (split '', $str) {
$count++ if ($c == $digit);
}
return $count;
}
sub check_layers {
my ($self) = @_;
my $max = length( $self->{ layers }[0] );
my $layer;
for my $l (@{ $self->{ layers } }) {
my $zero_count = count_digits( $l, 0 );
if ($zero_count < $max) {
$max = $zero_count;
$layer = $l;
}
}
return count_digits( $layer, 1 ) * count_digits( $layer, 2 );
}
sub render_pixel {
my ($self, $x, $y) = @_;
my $index = $self->{ width } * $y + $x;
my $value = 2;
my $layer = 0;
while ($value == 2 && $layer < @{ $self->{ layers } }) {
$value = substr( $self->{ layers }[$layer], $index, 1 );
$layer++;
}
return $value;
}
sub render {
my ($self) = @_;
for my $y (0 .. $self->{ height } - 1) {
my $row = '';
for my $x (0 .. $self->{ width } - 1) {
$row .= $self->render_pixel( $x, $y ) == 0 ? 'X' : ' ';
}
print "$row\n";
}
}
sub new {
my ($class, $input, $width, $height) = @_;
my $self = {
layers => [],
width => $width,
height => $height,
};
my $index = 0;
my $len = $width * $height;
while ($index < length( $input )) {
push @{ $self->{ layers } }, substr( $input, $index, $len );
$index += $len;
}
bless $self, $class;
return $self;
}
}
#
# Note: For some reason, slurp is adding a line feed at the end of the
# input.
#
my $input_file = $ARGV[0] || 'input08.txt';
my $input = Path::Tiny::path( $input_file )->slurp_utf8();
$input =~ s/\n$//;
my $width = 25;
my $height = 6;
my $image = Image->new( $input, $width, $height );
print "The checksum is ", $image->check_layers(), "\n";
$image->render();
exit;