some days were forgotten

This commit is contained in:
Yanick Champoux 2017-12-28 12:15:15 -05:00
parent 2176b98b37
commit 458f8734fd
16 changed files with 2358 additions and 6 deletions

View File

@ -1,14 +1,13 @@
use 5.20.0;
my @maze = <>;
my $position = 0;
my $steps = 0;
my $p;
while( $position < @maze ) {
$steps++;
my $i = $position;
$position += $maze[$position];
$maze[$i] += $maze[$i] > 2 ? -1 : 1;
$position += $p = $maze[$position];
$maze[$i] += $p > 2 ? -1 : 1;
}
say $steps;
print $steps;

36
2017/10/2.pl Normal file
View File

@ -0,0 +1,36 @@
use 5.20.0;
use List::AllUtils qw/ reduce part /;
my $e = <>;
chomp $e;
my @commands = map { ord } split '', $e;
#pop @commands;
push @commands, 17, 31, 73, 47, 23;
my $skip = 0;
my $i = 0;
my @array = 0..255;
for ( 1..64 ) {
for my $c ( @commands ) {
@array[0..$c-1] = @array[ reverse 0..$c-1];
push @array, shift @array for 1..$c + $skip++;
$i += $c + $skip-1;
}
}
unshift @array, pop @array for 1..($i%@array);
say "@array";
my $j;
my @grouped = map {
reduce { $a ^ $b } @$_
} part { $j++ / 16 } @array;
use DDP;
p @grouped;
say map { sprintf "%02x", $_ } @grouped;

1
2017/10/a Normal file
View File

@ -0,0 +1 @@
AoC 2017

1
2017/10/test Normal file
View File

@ -0,0 +1 @@
3,4,1,5

50
2017/11/2.pl Normal file
View File

@ -0,0 +1,50 @@
use 5.20.0;
use DDP;
use experimental 'postderef', 'signatures';
use List::AllUtils qw/ reduce min max /;
my $path = <>;
chomp $path;
my %path;
$path{$_}++ for split ',', $path;
my %d = (
n => [ 0,1],
s => [ 0,-1],
e => [ 1, 0],
w => [ -1,0],
);
for my $y ( qw/ n s / ){
for my $e ( qw/ e w / ){
$d{"$y$e"} = [ map { $_/2 } add( $d{$y}, $d{$e} )->@* ];
}
}
my $position = [0,0];
my $max_distance;
$position = reduce {
my $p = add($a,$d{$b});
$max_distance = max $max_distance, total_distance(@$p);
$p;
} $position, split ',', $path;
say $max_distance;
sub total_distance ( @position ) {
my @position = map { abs } @position;
my $i = min @position;
return $i + 2 * ( $position[0] - $i) + $position[1];
}
sub add($x,$y) {
[ map { $x->[$_] + $y->[$_] } 0..1 ]
}

31
2017/12/1.pl Normal file
View File

@ -0,0 +1,31 @@
use 5.20.0;
use List::AllUtils qw/ pairmap /;
use experimental 'postderef';
my %pipe = pairmap { $a => [ split ', ', $b] } map { split ' <-> ' }
map { chomp; $_ }<>;
use DDP;
for my $from (keys %pipe ) {
my @i = $pipe{$from}->@*;
for my $i ( @i ) {
push $pipe{$i}->@*, $from;
}
}
my @group = ( 0 );
my @unprocessed = $pipe{0}->@*;
my %seen = ( 0 => 1 );
while( @unprocessed ) {
my $next = shift @unprocessed;
warn $next;
next if $seen{$next}++;
warn join " ", $pipe{$next}->@*;
push @unprocessed, $pipe{$next}->@*;
}
say join " ", keys %seen;
say scalar keys %seen;

34
2017/12/2.pl Normal file
View File

@ -0,0 +1,34 @@
use 5.20.0;
use List::AllUtils qw/ pairmap /;
use experimental 'postderef';
my %pipe = pairmap { $a => [ split ', ', $b] } map { split ' <-> ' }
map { chomp; $_ }<>;
use DDP;
for my $from (keys %pipe ) {
my @i = $pipe{$from}->@*;
for my $i ( @i ) {
push $pipe{$i}->@*, $from;
}
}
my $nbr_groups = 0;
while ( keys %pipe ) {
$nbr_groups++;
my ( $first ) = keys %pipe;
my @unprocessed = ( $first );
my @group;
my %seen;
while( @unprocessed ) {
my $next = shift @unprocessed;
my $p = delete $pipe{$next} or next;
push @unprocessed, $p->@*;
}
}
say $nbr_groups;

2000
2017/12/input.txt Normal file

File diff suppressed because it is too large Load Diff

19
2017/16/1.pl Normal file
View File

@ -0,0 +1,19 @@
use 5.20.0;
use List::AllUtils qw/ first_index indexes /;
my @d = 'a'..'p';
for ( split ',', <> ) {
if( /s(\d+)/ ) {
splice @d, 0, 0, splice @d, -$1;
}
elsif( m#x(\d+)/(\d+)# ) {
@d[$1,$2] = @d[$2,$1];
}
elsif( m#p(.)/(.)# ) {
my @i= indexes { $_ ~~ [ $1, $2 ] } @d;
@d[@i] = @d[reverse@i];
}
}
say @d;

41
2017/16/2.pl Normal file
View File

@ -0,0 +1,41 @@
use 5.20.0;
use List::AllUtils qw/ first_index indexes /;
use experimental qw/ smartmatch /;
my @d = 'a'..'p';
my @moves = split ',', <>;
my @round = ( "@d" );
my $total = 1_000_000_000;
while( $total-- ) {
for ( @moves ) {
if( /s(\d+)/ ) {
splice @d, 0, 0, splice @d, -$1;
}
elsif( m#x(\d+)/(\d+)# ) {
@d[$1,$2] = @d[$2,$1];
}
elsif( m#p(.)/(.)# ) {
my @i= indexes { $_ ~~ [ $1, $2 ] } @d;
@d[@i] = @d[reverse@i];
}
}
if( "@d" ~~ @round ) {
warn "@d";
@round = splice @round, first_index { $_ eq "@d" } @round;
last;
}
else {
push @round, "@d";
}
}
$total %= @round;
say $round[$total] =~ s/ //gr;

1
2017/16/input.txt Normal file

File diff suppressed because one or more lines are too long

View File

@ -8,7 +8,8 @@ my $p = 0;
use DDP;
for(1..2017) {
$p = ( 1 + $p + $i ) % @v;
splice @v, $p, 0, $_;
if($p) { splice @v, $p, 0, $_; }
else { push @v, $_; $p = $#v };
}
p @v;

16
2017/17/2.pl Normal file
View File

@ -0,0 +1,16 @@
use 5.20.0;
my $i = 380;
my $p = 0;
my $x;
use DDP;
for(1..50_000_000) {
say $_ unless $_ % 100_000;
$p = ( 1 + $p + $i ) % $_;
$x = $_ if $p == 1;
$p ||= $_;
}
say $x;

7
2017/18/test Normal file
View File

@ -0,0 +1,7 @@
snd 1
snd 2
snd p
rcv a
rcv b
rcv c
rcv d

79
2017/21/2.pl Normal file
View File

@ -0,0 +1,79 @@
use 5.20.0;
use Grid::Transform;
my %transform;
my $i = 0;
while(<>) {
chomp;
s/\///g;
my ($from,$to) = split ' => ';
my @from = split '', $from;
my $gt = Grid::Transform->new( \@from, rows => sqrt @from );
for my $t (
map { $_, $_->copy->rotate90, $_->copy->rotate180, $_->copy->rotate270 }
map { $_, $_->copy->flip_vertical } $gt ) {
$transform{ join '', $t->grid} = $to;
}
}
use DDP;
#p %transform;
my @grid = ([qw/ . # . /], [qw/ . . # /], [qw/ # # # /] );
for( 1..18 ) {
my @new_grid;
if( not @grid % 2 ) {
for my $x ( 0..$#grid/2 ) {
for my $y ( 0..$#grid/2 ) {
transform_2(\@grid,\@new_grid,$x,$y);
}
}
}
else {
for my $x ( 0..$#grid/3 ) {
for my $y ( 0..$#grid/3 ) {
transform_3(\@grid,\@new_grid,$x,$y);
}
}
}
@grid = @new_grid;
}
say scalar grep { $_ eq '#' } map { @$_ } @grid;
sub transform_2 {
my ( $grid, $n, $x, $y ) = @_;
my $k;
for my $i ( 0..1 ) {
for my $j ( 0..1 ) {
$k .= $grid->[2*$x+$i][2*$y+$j];
}
}
my @new = split '', $transform{$k};
my ($r,$c) = ($x,$y);
for my $k ( 0..2 ) {
$n->[3*$r+$k] ||= [];
$n->[3*$r+$k][3*$c+$_] = shift @new for 0..2;
}
}
sub transform_3 {
my ( $grid, $n, $x, $y ) = @_;
my $k;
for my $i ( 0..2 ) {
for my $j ( 0..2 ) {
$k .= $grid->[3*$x+$i][3*$y+$j];
}
}
my @new = split '', $transform{$k};
my ($r,$c) = ($x,$y);
for my $k ( 0..3 ) {
$n->[4*$x+$k] ||= [];
$n->[4*$x+$k][4*$c+$_] = shift @new for 0..3;
}
}

36
2017/24/2.pl Normal file
View File

@ -0,0 +1,36 @@
use 5.20.0;
use experimental qw/ signatures postderef smartmatch /;
use List::AllUtils qw/ sum max first_index min/;
my @elements = map { chomp; [ split '/' ] } <>;
use DDP;
my $inv_length = 1E99;
my $max = 0;
build_bridge(0,0,@elements);
say $max;
sub build_bridge( $sofar = 0, $connector = 0, @left ) {
for my $i ( 0..$#left ) {
next unless $connector ~~ $left[$i]->@*;
my @copy = @left;
my $i = splice @copy, $i, 1;
my $next_con = $i->[ 1 - first_index { $_ == $connector } @$i ];
build_bridge( $sofar + sum( @$i ), $next_con, @copy );
}
return if @left > $inv_length;
if( @left == $inv_length ) {
$max = max $max, $sofar;
}
else {
$inv_length = @left;
$max = $sofar;
}
}