Skip to content

Instantly share code, notes, and snippets.

@pmenzel
Last active December 18, 2022 11:13
Show Gist options
  • Save pmenzel/c0d4dbd9f71e0ec5e4b60a0b11d39778 to your computer and use it in GitHub Desktop.
Save pmenzel/c0d4dbd9f71e0ec5e4b60a0b11d39778 to your computer and use it in GitHub Desktop.
Advent of Code 2022 in mostly Perl
===========================
Days 10 ...
===========================
see https://github.com/pmenzel/advent-of-code/tree/master/2022
===========================
Day 9
===========================
Part 1:
no one-liner today! :-/
see https://github.com/pmenzel/advent-of-code/blob/master/2022/09/part1.pl
actually could do this, using the idea from https://www.reddit.com/r/adventofcode/comments/zgwhh1/2022_day_9_part_2_cant_believe_i_didnt_see_that/izj9ade/
perl -lne 'BEGIN{@H=(0,0);@T=(0,0)} /(\w) (\d+)/; for $i (1..$2) { @pH = @H; if($1 eq "R") {$H[1]++}; if($1 eq "L") {$H[1]--}; if($1 eq "U") {$H[0]++}; if($1 eq "D") {$H[0]--}; if(abs($H[0]-$T[0])>1 or abs($H[1]-$T[1])>1){ @T=@pH } $h{"@T"}=1 } }{ print scalar keys %h;' input.txt
===========================
Day 8
===========================
Part 1:
perl -M'List::MoreUtils qw/none/' -F'' -lane 'push(@m,[@F]); }{ for $i (0..$#m){ for $j (0..$#m){ $v=$m[$i][$j]; $c++ if none { $_ >= $v } map $m[$i][$_], 0..$j-1 or none { $_ >= $v } map $m[$i][$_], $j+1..$#m or none { $_ >= $v } map $m[$_][$j], 0..$i-1 or none { $_ >= $v } map $m[$_][$j], $i+1..$#m }} print $c' input.txt
Part 2:
perl -M'List::MoreUtils qw/firstidx/' -F'' -lane 'push(@m,[@F]); }{ for $i (0..$#m){ for $j (0..$#m){$c=1; $v=$m[$i][$j]; $x = firstidx {$_>=$v} map $m[$i][$_],reverse 0..$j-1; $c *= ($x==-1)?$j:$x+1; $x = firstidx {$_>=$v} map $m[$i][$_],$j+1..$#m; $c *= ($x==-1)?$#m-$j:$x+1; $x = firstidx {$_>=$v} map $m[$_][$j],reverse 0..$i-1; $c *= ($x==-1)?$i:$x+1; $x = firstidx {$_>=$v} map $m[$_][$j],$i+1..$#m; $c *= ($x==-1)?$#m-$i:$x+1; print $c; }}' input.txt | sort -n | tail -n1
===========================
Day 7
===========================
Part 1:
perl -lne 'if(/cd (.*)/) { $1 eq ".." ? pop(@n) : push(@n,$1) } else{ if(/^\d+/){ map {$s{"@n[0..$_]"}+=$&} 0..$#n;} } }{ map {$sum += $s{$_} if ($s{$_} <= 1e5)} keys %s; print "sum=$sum"' input.txt
Part 2:
perl -lne 'if(/cd (.*)/) { $1 eq ".." ? pop(@n) : push(@n,$1) } else{ if(/^\d+/){ map {$s{"@n[0..$_]"}+=$&} 0..$#n;} } }{ $t=3e7-(7e7-$s{"/"}); for $k (sort {$s{$a}<=>$s{$b}} keys %s) { if($s{$k}>=$t) { print $s{$k}; last;}}' input.txt
===========================
Day 6
===========================
Part 1:
perl -lne '/(\w)((?!\1).)(?!\1)((?!\2).)(?!\1)(?!\2)((?!\3).)/; print "$1$2$3$4 $+[0]"' input.txt
Part 2:
perl -M'List::MoreUtils qw/uniq/' -lne 'print; $i=0; while(1) { if(14 == uniq(split(//,substr($_,++$i,14)))) {print $i+14; last;} }' input.txt
===========================
Day 5
===========================
Task 1:
perl -lne 'if(/\[/){ @s=split(//,$_,-1);@c=map 4*$_+1, 0..8; map {$h{$_}.=$s[$_]} @c;} elsif(/^m[^\d]*(\d+).*(\d).*(\d)/){ unshift(@{$m{$3}},reverse splice(@{$m{$2}},0,$1)); } elsif(/^ 1/){ %m=map{$h{$_}=~s/ //g;++$z=>[split(//,$h{$_})]} sort {$a<=>$b} keys %h; } }{ map { print "$_=",@{$m{$_}}} sort keys %m;' input.txt
Task 2:
perl -lne 'if(/\[/){ @s=split(//,$_,-1);@c=map 4*$_+1, 0..8; map {$h{$_}.=$s[$_]} @c;} elsif(/^m[^\d]*(\d+).*(\d).*(\d)/){ unshift(@{$m{$3}},splice(@{$m{$2}},0,$1)); } elsif(/^ 1/){ %m=map{$h{$_}=~s/ //g;++$z=>[split(//,$h{$_})]} sort {$a<=>$b} keys %h; } }{ map { print "$_=",@{$m{$_}}} sort keys %m;' input.txt
The output shows the final stacks.
===========================
Day 4
===========================
Task 1:
perl -lne '/(\d+)-(\d+),(\d+)-(\d+)/; @a1=$1..$2; @a2=$3..$4; %h1=map{$_=>1} @a1; %h2=map{$_=>1} @a2; @i1=grep($h1{$_},@a2); @i2=grep($h2{$_},@a1); print if("@i1" eq "@a2" or "@i2" eq "@a1")' input.txt | wc -l
#alternative using just the interval bounds:
perl -lne '/(\d+)-(\d+),(\d+)-(\d+)/; $c++ if($1>=$3 && $2<=$4 || $3>=$1 && $4<=$2) }{ print $c' input.txt
Task 2:
perl -lne '/(\d+)-(\d+),(\d+)-(\d+)/; @a1=$1..$2; @a2=$3..$4; %h1=map{$_=>1} @a1; %h2=map{$_=>1} @a2; @i1=grep($h1{$_},@a2); @i2=grep($h2{$_},@a1); print if(@i1 + @i2)' input.txt | wc -l
#alternative using just the interval bounds counting overlaps:
perl -lne '/(\d+)-(\d+),(\d+)-(\d+)/; $c++ if($1>=$3 && $2<=$4 || $3>=$1 && $4<=$2 || $1<=$3 && $2>=$3 || $3<=$1 && $4>=$1 ) }{ print $c' input.txt
# counting non-overlaps:
perl -lne '/(\d+)-(\d+),(\d+)-(\d+)/; $c++ unless ($3 > $2 || $4 < $1) }{ print $c' input.txt
===========================
Day 3
===========================
Task 1:
perl -lne '$l=length;$c1=substr($_,0,$l/2); substr($_,$l/2) =~ /([$c1])/; $i=ord($1); $s += $i>96 ? $i-96 : $i-38 }{ print $s' input.txt
Task 2:
sed '0~3G' input.txt | perl -00 -ne '/(\w)[^\1]*\n([^\1]*\1[^\1]*\n){2}/; $i=ord($1); $s += $i > 96 ? $i-96 : $i-38 }{ print $s'
===========================
Day 2
===========================
Task 1:
perl -lne '%m=qw/AX 4 AY 8 AZ 3 BX 1 BY 5 BZ 9 CX 7 CY 2 CZ 6/; s/ //; $s += $m{$_}; }{ print $;' input.txt
Task 2:
perl -lne '%m=qw/AX 3 AY 4 AZ 8 BX 1 BY 5 BZ 9 CX 2 CY 6 CZ 7/; s/ //; $s += $m{$_}; }{ print $;' input.txt
===========================
Day 1
===========================
cat input.txt | perl -00 -F'\n' -lane '$s=0; map {$s += $_} @F; print $s' | sort -n
and then use last value for task 1 and the sum of the last 3 values for task 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment