CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 17:38:59 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=2988bd11ef5d54e575d5e38bf9411bfdc90ad32d79286ba2290da8a1fba670e4a%3A2%3A%7Bi%3A0%3Bs%3A14%3A%22_csrf-frontend%22%3Bi%3A1%3Bs%3A32%3A%22_xIME7rwkCk0NCWkrropahmatX8g4MtZ%22%3B%7D; HttpOnly; Path=/
cf-ray: 98d0226118868cf1-BLR
AoC 2024, day 20, Perl version of Smalltalk sol'n - Pastebin.com
SHARE
TWEET

AoC 2024, day 20, Perl version of Smalltalk sol'n
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- $| = 1;
- # Read grid:
- my @Grid = map { [split(//)] } <>;
- sub grid_at ($) { my $p = shift; return ($Grid[$p->[0]][$p->[1]]) }
- # Vector stuff:
- 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]] }
- sub vec_eq ($$) { my ($v,$w) = @_; return (($v->[0] == $w->[0]) and ($v->[1] == $w->[1])) }
- sub dist ($$) { my ($v,$w) = @_; return (abs($v->[0] - $w->[0]) + abs($v->[1] - $w->[1])) }
- # Find start and end:
- 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');
- }
- }
- # Get path of race course
- my @path = ($start);
- my $pos = $start;
- my $back = -1;
- while (!&vec_eq( $pos, $end )) {
- DIR:
- for (my $d = 0; $d < @Dirs; $d++) {
- my $move = &vec_sum( $pos, $Dirs[$d] );
- if (grid_at($move) ne '#' and $back != $d) {
- $back = ($d + 2) % 4;
- $pos = $move;
- push( @path, $move );
- last DIR;
- }
- }
- }
- # Done with grid, now using just the path:
- my $part1 = 0;
- my $part2 = 0;
- my $targ = 100;
- # Compare points that are far enough apart on the path to potentially gain target.
- foreach (my $i = 0; $i <= $#path - ($targ + 2); $i++) {
- print ::stderr "[$i/$#path] $part2\r" if ($i % 250 == 0);
- foreach (my $j = $i + ($targ + 2); $j <= $#path; $j++) {
- my $dist = &dist( $path[$i], $path[$j] );
- # Check allowed distance, and if we didn't lose too much time.
- if (($dist <= 20) and ($j - $i - $dist >= $targ)) {
- $part2++;
- $part1++ if ($dist == 2);
- }
- }
- }
- say "\nPart 1: $part1";
- say "Part 2: $part2";
Advertisement
Add Comment
Please, Sign In to add comment
-
⭐⭐⭐Instant Profit Method⭐⭐
Java | 3 sec ago | 0.10 KB
-
⭐✅ Online Marketplace Exploit ✅ NEVER SEEN BE...
JavaScript | 4 sec ago | 0.24 KB
-
⭐✅ Trading Profit Method ✅ NEVER SEEN BEFORE...
JavaScript | 13 sec ago | 0.24 KB
-
⭐ Instant BTC Profit Method ✅ NEVER SEEN BEFO...
JavaScript | 22 sec ago | 0.24 KB
-
⭐⭐⭐ChangeNOW Exploit⭐⭐
Java | 30 sec ago | 0.10 KB
-
⭐✅ MAKE $2000 INSTANTLY ✅ NEVER SEEN BEFORE ⭐...
JavaScript | 31 sec ago | 0.24 KB
-
⭐ Free Crypto Method ✅ NEVER SEEN BEFORE ⭐⭐⭐
JavaScript | 40 sec ago | 0.24 KB
-
✅⭐ Make huge profits on trading ✅ NEVER SEEN...
JavaScript | 49 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