-
Notifications
You must be signed in to change notification settings - Fork 0
/
day05.pl
97 lines (78 loc) · 2.04 KB
/
day05.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
use strict;
use warnings;
use feature 'say';
use String::Util 'trim';
use lib './';
use assertions;
open(my $file, "<", "input/day05.txt") or die "Can't open input file";
sub sayArray {
my (@array) = @_;
say join(';', @array)
}
my @stateLines = ();
my @movesLines = ();
my $hadEmptyLine = 0;
while (my $line = <$file>) {
chomp($line);
if (length($line) == 0) {
$hadEmptyLine = 1;
next;
}
if ($hadEmptyLine) {
push(@movesLines, $line);
} else {
push(@stateLines, $line);
}
}
close $file;
my @stackIndices = split(/\s*/, $stateLines[-1]);
my $stacksCount = $stackIndices[-1];
sub initStacks {
my @stacks = ();
my $row = 0;
foreach my $stateLine (reverse(@stateLines[0..$#stateLines-1])) {
# say $stateLine;
for (my $si = 0; $si <= $stacksCount; $si++) {
my @tmp = split(//, $stateLine);
my $idx = 1 + 4 * $si;
if ($idx <= $#tmp) {
my $c = $tmp[1 + 4 * $si];
if (length(trim($c)) > 0) {
# say $c;
$stacks[$si] .= $c;
}
}
}
$row++;
}
# sayArray @stacks;
return @stacks;
}
sub rearrangeStacks {
my ($retainSliceOrder, @stacks) = @_;
foreach my $move (@movesLines) {
$move =~ /move (\d+) from (\d+) to (\d+)/;
my ($count, $from, $to) = ($1, $2 - 1, $3 - 1);
my $sliceToMove = substr(reverse($stacks[$from]), 0, $count);
if ($retainSliceOrder) {
# re-reverse back
$sliceToMove = reverse($sliceToMove);
}
$stacks[$from] = substr($stacks[$from], 0, length($stacks[$from]) - $count);
$stacks[$to] .= $sliceToMove;
# say $move;
# sayArray @stacks;
}
return @stacks;
}
sub printTopCrates {
my (@stacks) = @_;
foreach my $stack (@stacks) {
print substr($stack, -1);
}
say "";
}
# Part 1
printTopCrates rearrangeStacks 0, initStacks;
# Part 2
printTopCrates rearrangeStacks 1, initStacks;