CARVIEW |
Select Language
HTTP/2 200
date: Sun, 12 Oct 2025 13:40:18 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=18b3bd274cc9c5504bd86b4899c0713a67c82a7fa0e619e5850b79d967da61fca%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22Jo34OxOaAz0IQu9xbGt4PJbriclGa6Bl%22%3B%7D; HttpOnly; Path=/
cf-ray: 98d7021da8faa9b7-BLR
AoC day 16, 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);
- use Array::Heap::PriorityQueue::Numeric;
- my @Dirs = ([0,1], [1,0], [0,-1], [-1,0]);
- sub vec_sum ($$) { my ($v,$w) = @_; return [$v->[0] + $w->[0], $v->[1] + $w->[1]] }
- my @Grid = map { chomp; [split(//)] } <>;
- sub grid_at ($) { my $p = shift; return ($Grid[$p->[0]][$p->[1]]) }
- sub as_str ($) { my $p = shift; return (join(',', @$p)) }
- sub print_grid { say "\t", join( '', @$_ ) foreach (@Grid); }
- my ($start, $end);
- foreach my $y (0 .. $#Grid) {
- foreach my $x (0 .. $Grid[0]->$#*) {
- $start = [$y,$x] if ($Grid[$y][$x] eq 'S');
- $end = [$y,$x] if ($Grid[$y][$x] eq 'E');
- }
- }
- my %paths; # collects all path elements
- my $min = ~0; # minium path seen
- my $time;
- my %visit;
- my $queue = new Array::Heap::PriorityQueue::Numeric;
- $queue->add( [$start, 0, 0, [], 0], 0 );
- QUEUE:
- while (my $state = $queue->get) {
- my ($pos, $face, $turn, $path, $moves) = @$state;
- next QUEUE if ($moves > $min);
- if (grid_at($pos) eq 'E') {
- say "Part 1: $moves " if ($moves < $min);
- $min = $moves;
- %paths = (%paths, map {as_str($_) => 1} @$path);
- next QUEUE;
- }
- print ::stderr "[$time] ",$queue->size," \r" if (++$time % 10000 == 0);
- next QUEUE if (($visit{@$pos, $face} // ~0) < $moves);
- $visit{@$pos, $face} = $moves;
- # Try straight if valid
- my $step = &vec_sum( $pos, $Dirs[$face] );
- if (grid_at($step) ne '#') {
- $queue->add( [$step, $face, 0, [@$path, $step], $moves + 1], $moves + 1 );
- }
- # Try turns, with an immediate step
- # ASSUME: Never want to turn twice, only place this would matter is a U-turn at the start
- if (!$turn) {
- foreach my $t (1,-1) {
- $queue->add( [$pos, ($face+$t) % 4, $t, $path, $moves + 1000], $moves + 1000 );
- }
- }
- }
- # +1 for missing End
- say "Part 2: ", 1 + keys %paths, " ";
Advertisement
Add Comment
Please, Sign In to add comment
-
⚡ Crypto Swap Glitch ✅ Working ⚡
JavaScript | 4 sec ago | 0.24 KB
-
⭐⭐⭐Free Giftcards Method⭐⭐
Java | 6 sec ago | 0.10 KB
-
✅ Make $2500 in 20 minutes⭐ I
JavaScript | 7 sec ago | 0.25 KB
-
💎 ChangeNOW Exploit
JavaScript | 15 sec ago | 0.24 KB
-
⭐⭐⭐Make $1500 in 20 minutes⭐⭐
Java | 17 sec ago | 0.10 KB
-
🚨 Free Crypto Method 🚨
JavaScript | 24 sec ago | 0.24 KB
-
⭐⭐⭐GMAIL Logs (2FA disabled)⭐⭐
Java | 28 sec ago | 0.10 KB
-
💡 EASY MONEY GUIDE ✅ Working
JavaScript | 33 sec ago | 0.24 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