CARVIEW |
Select Language
HTTP/2 200
date: Sun, 12 Oct 2025 00:38:30 GMT
content-type: text/html; charset=UTF-8
server: cloudflare
x-frame-options: DENY
x-content-type-options: nosniff
x-xss-protection: 1;mode=block
vary: accept-encoding
cf-cache-status: DYNAMIC
content-encoding: gzip
set-cookie: _csrf-frontend=25a318437e705d5459246e476fcf327ededee6a4feca5840a6f60bdd187feed3a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22s8Af3m1K4IhydOpMCxWJdYcA5c8u3hds%22%3B%7D; HttpOnly; Path=/
cf-ray: 98d288e3ffdbc195-BLR
AoC 2024, day 9, part 2 (Perl) - Pastebin.com
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- my @input = map { chomp; split // } <>;
- # Read in disk
- my @disk;
- my $id = 0;
- while (@input) {
- my $size = shift @input;
- my $gap = shift @input;
- push( @disk, {id => $id++, size => int $size} );
- push( @disk, {id => -1, size => int $gap } ) if ($gap);
- }
- # Defragment disk
- my $idx = $#disk;
- for (my $file_id = $id - 1; $file_id > 0; $file_id--) {
- $idx-- while ($idx >= 0 and $disk[$idx]{id} != $file_id);
- my $span = 0;
- $span++ while ($span < $idx and ($disk[$span]{id} != -1
- or $disk[$span]{size} < $disk[$idx]{size}));
- next if ($span >= $idx);
- my %copy = $disk[$idx]->%*;
- $disk[$idx]{id} = -1;
- splice( @disk, $span, 1, (\%copy, {size => $disk[$span]{size} - $disk[$idx]{size},
- id => -1}) );
- }
- # Calculate checksum
- my $part2 = 0;
- my $pos = 0;
- foreach my $b (@disk) {
- if ($b->{id} != -1 and $b->{size} > 0) {
- for (my $i = 0; $i < $b->{size}; $i++) {
- $part2 += $b->{id} * ($pos + $i);
- }
- }
- $pos += $b->{size};
- }
- say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment
-
π MAKE $2500 IN 15 MIN β
Working
JavaScript | 3 sec ago | 0.24 KB
-
π΅ Make 3000$ in 20 minutes π΅
JavaScript | 12 sec ago | 0.24 KB
-
Free Crypto Method (NEVER SEEN BEFORE)ββ 3
JavaScript | 16 sec ago | 0.25 KB
-
π Swapzone +37% glitch
JavaScript | 21 sec ago | 0.24 KB
-
ββ
Swapzone Glitch β
Working ββ G
JavaScript | 25 sec ago | 0.25 KB
-
β
Make $2500 in 20 minutesβββ U
JavaScript | 34 sec ago | 0.25 KB
-
π Exchange profit method
JavaScript | 36 sec ago | 0.24 KB
-
π Swapzone +37% glitch β W
JavaScript | 42 sec ago | 0.25 KB
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand