-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-5-star-2.pl
executable file
·95 lines (82 loc) · 3.29 KB
/
day-5-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
#!/usr/bin/perl
use warnings;
use 5.036;
use List::Util qw(min);
chomp(my @lines = <>);
# to ensure that the last set of ranges get processed correctly
if ($lines[-1] ne '') {
push(@lines, '');
}
my @seedRanges = $lines[0] =~ /(\d+)/g;
my @seeds;
# say "@seedRanges";
for my $i (0..$#seedRanges) {
next if ($i % 2 == 1);
my $start = $seedRanges[$i];
my $end = $seedRanges[$i] + $seedRanges[$i+1];
# say "Start: " . $start . " end: " . $end;
push(@seeds, { 'start' => $start, 'end' => $end });
}
my $gettingRanges = 0;
my $processingRanges = 0;
my @ranges;
my @processedSeeds;
# range is a hash of { start, end, offset }
for my $line (@lines[2..$#lines]) {
if ($line eq '') {
$gettingRanges = 0;
$processingRanges = 1;
}
if ($line =~ /\:/) {
$gettingRanges = 1;
next;
}
if ($gettingRanges) {
my ($destinationStart, $sourceStart, $rangeLength) = $line =~ /(\d+)/g;
push(@ranges, { 'start' => $sourceStart, 'end' => $sourceStart + $rangeLength, 'offset' => $destinationStart - $sourceStart });
# say $#ranges;
}
if ($processingRanges) {
@ranges = sort { $a->{'start'} <=> $b->{'start'} } @ranges;
push(@ranges, { 'start' => $ranges[-1]->{'end'}, 'end' => ~0, 'offset' => 0 });
for my $i (0..$#seeds) {
my $seedRangeRef = $seeds[$i];
# say "Seed range: $seedRangeRef->{'start'} - $seedRangeRef->{'end'}";
for my $rangeRef (@ranges) {
# say "Processing range: $rangeRef->{'start'} - $rangeRef->{'end'}";
if ($seedRangeRef->{'start'} > $rangeRef->{'end'}) {
next;
}
if ($seedRangeRef->{'start'} < $rangeRef->{'start'}) {
if ($seedRangeRef->{'end'} < $rangeRef->{'start'}) {
push(@processedSeeds, $seedRangeRef);
last;
} else {
my $newZeroRange = { 'start' => $seedRangeRef->{'start'}, 'end' => $rangeRef->{'start'} - 1 };
push(@processedSeeds, $newZeroRange);
$seedRangeRef->{'start'} = $rangeRef->{'start'};
redo;
}
}
if ($seedRangeRef->{'start'} >= $rangeRef->{'start'}) {
if ($seedRangeRef->{'end'} <= $rangeRef->{'end'}) {
my $newOffsetRange = { 'start' => $seedRangeRef->{'start'} + $rangeRef->{'offset'}, 'end' => $seedRangeRef->{'end'} + $rangeRef->{'offset'}};
push(@processedSeeds, $newOffsetRange);
last;
} else {
my $newOffsetRange = { 'start' => $seedRangeRef->{'start'} + $rangeRef->{'offset'}, 'end' => $rangeRef->{'end'} + $rangeRef->{'offset'}};
push(@processedSeeds, $newOffsetRange);
$seedRangeRef->{'start'} = $rangeRef->{'end'};
}
}
}
}
@seeds = @processedSeeds;
say "Number of seed ranges: ($#seeds + 1)";
@processedSeeds = ();
@ranges = ();
$processingRanges = 0;
}
}
my @sortedSeeds = sort { $a->{'start'} <=> $b->{'start'} } @seeds;
say $sortedSeeds[0]->{'start'};