diff options
author | Christian Segundo | 2024-12-08 11:21:53 +0100 |
---|---|---|
committer | Christian Segundo | 2024-12-08 11:21:53 +0100 |
commit | 293348441811e26b4b885d57608f796b72974bda (patch) | |
tree | 0fdfb21939c53a65e1ca6a1a1db5db9c1d2065b6 /6.pl | |
parent | 25f7a5e5bc80c699d9538aed0d3f31b9406e0a74 (diff) | |
download | advent-of-code-2024-293348441811e26b4b885d57608f796b72974bda.tar.gz |
Diffstat (limited to '6.pl')
-rw-r--r-- | 6.pl | 96 |
1 files changed, 96 insertions, 0 deletions
@@ -0,0 +1,96 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +my @original_grid; +my @grid; +my @pos = (0,0); +my @start_pos = (0,0); +my %seen_positions; + +open(my $fh, "6-input.txt") or die; +while (<$fh>) { chomp; push @original_grid, [split //]; } +close($fh); + +@grid = map { [@$_] } @original_grid; +for (my $i=0; $i<scalar @grid; $i++) { + for (my $j=0; $j<scalar @{$grid[$i]}; $j++) { + if ($grid[$i][$j] eq '^') { + $start_pos[0] = $i; + $start_pos[1] = $j; + } + } +} + +$pos[0] = $start_pos[0]; +$pos[1] = $start_pos[1]; + +sub tick { + my ($position, $grid) = @_; + my $direction = $grid[$pos[0]][$pos[1]]; + if ($direction eq '^') { + if ($pos[0]-1 < 0) { $grid[$pos[0]][$pos[1]] = 'X'; } + elsif ($pos[0]-1 >= 0 and ($grid[$pos[0]-1][$pos[1]] eq '.' or $grid[$pos[0]-1][$pos[1]] eq 'X')) { + $grid[$pos[0]][$pos[1]] = 'X'; + $pos[0]--; + $grid[$pos[0]][$pos[1]] = '^'; + } + else { $grid[$pos[0]][$pos[1]] = '>'; } + } elsif ($direction eq '>') { + if ($pos[1]+1 >= scalar @{$grid[$pos[0]]}) { $grid[$pos[0]][$pos[1]] = 'X'; } + elsif ($pos[1]+1 < scalar @{$grid[$pos[0]]} and ($grid[$pos[0]][$pos[1]+1] eq '.' or $grid[$pos[0]][$pos[1]+1] eq 'X')) { + $grid[$pos[0]][$pos[1]] = 'X'; + $pos[1]++; + $grid[$pos[0]][$pos[1]] = '>'; + } + else { $grid[$pos[0]][$pos[1]] = 'V'; } + } elsif ($direction eq 'V') { + if ($pos[0]+1 >= scalar @grid) { $grid[$pos[0]][$pos[1]] = 'X'; } + elsif ($pos[0]+1 < scalar @grid and ($grid[$pos[0]+1][$pos[1]] eq '.' or $grid[$pos[0]+1][$pos[1]] eq 'X')) { + $grid[$pos[0]][$pos[1]] = 'X'; + $pos[0]++; + $grid[$pos[0]][$pos[1]] = 'V'; + } + else { $grid[$pos[0]][$pos[1]] = '<'; } + } elsif ($direction eq '<') { + if ($pos[1]-1 < 0) { $grid[$pos[0]][$pos[1]] = 'X'; } + elsif ($pos[1]-1 >= 0 and ($grid[$pos[0]][$pos[1]-1] eq '.' or $grid[$pos[0]][$pos[1]-1] eq 'X')) { + $grid[$pos[0]][$pos[1]] = 'X'; + $pos[1]--; + $grid[$pos[0]][$pos[1]] = '<'; + } + else { $grid[$pos[0]][$pos[1]] = '^'; } + } else { return 0; } + return 1; +} + +while (tick(\@pos, \@grid)) { $seen_positions{"$pos[0],$pos[1]"} = 1; } +my $pone = 0; +foreach(@grid) { foreach (@$_) { $pone++ if $_ eq 'X'; } } +print "puzzle 1: ", $pone, "\n"; + +my $loops = 0; +for (my $i=0; $i<scalar @original_grid; $i++) { + for (my $j=0; $j<scalar @{$original_grid[$i]}; $j++) { + if ($original_grid[$i][$j] eq '.' and defined $seen_positions{"$i,$j"}) { + @grid = map { [@$_] } @original_grid; + $pos[0] = $start_pos[0]; + $pos[1] = $start_pos[1]; + $grid[$i][$j] = 'O'; + my %seen_position; + LOOP: while (tick(\@pos, \@grid)) { + my $direction = $grid[$pos[0]][$pos[1]]; + if (not defined $seen_position{"$pos[0],$pos[1]"}) { + $seen_position{"$pos[0],$pos[1]"} = [$direction]; + next; + } + foreach (@{ $seen_position{"$pos[0],$pos[1]"} }) { + if ($_ eq $direction) { $loops++; last LOOP; } + } + push @{ $seen_position{"$pos[0],$pos[1]"} }, $direction; + } + } + } +} + +print "puzzle 2: ", $loops, "\n"; |