lots of days

main
Yanick Champoux 2017-12-12 19:55:17 -05:00
parent f5996b47cd
commit 2b88e2660e
12 changed files with 523 additions and 0 deletions

57
2015/18/1.pl Normal file
View File

@ -0,0 +1,57 @@
use 5.20.0;
use List::AllUtils qw/ min sum /;
my @grid = map { [ split '' ] } map { chomp; $_ } <>;
use DDP;
@grid = iteration(@grid) for 1..100;
say sum map { $_ eq '#' } map { @$_ } @grid;
sub iteration {
my @grid = @_;
#say join ' ', @$_ for @grid;
#say;
my $size = @grid-1;
my @new_grid;
for my $x ( 0..$size ) {
for my $y ( 0..$size ) {
my $neig = neighbors(\@grid,$x,$y);
if( $grid[$x][$y] eq '#' ) {
$new_grid[$x][$y] = $neig ~~ [ 2,3] ? '#' : '.';
}
else {
$new_grid[$x][$y] = $neig == 3 ? '#' : '.';
}
}
}
return @new_grid;
}
sub neighbors {
my( $grid, $x, $y ) = @_;
my $sum;
for my $i ( -1..1 ) {
for my $j ( -1..1 ) {
next if !$i and !$j;
my($m,$n) = ($x+$i,$y+$j);
next if 0 > min $m, $n;
$sum += $grid->[$m][$n] eq '#';
}
}
return $sum;
}

57
2015/18/2.pl Normal file
View File

@ -0,0 +1,57 @@
use 5.20.0;
use List::AllUtils qw/ min sum /;
my @grid = map { [ split '' ] } map { chomp; $_ } <>;
use DDP;
@grid = iteration(@grid) for 1..100;
say sum map { $_ eq '#' } map { @$_ } @grid;
sub iteration {
my @grid = @_;
#say join ' ', @$_ for @grid;
#say;
my $size = @grid-1;
my @new_grid;
for my $x ( 0..$size ) {
for my $y ( 0..$size ) {
my $neig = neighbors(\@grid,$x,$y);
if( $grid[$x][$y] eq '#' ) {
$new_grid[$x][$y] = $neig ~~ [ 2,3] ? '#' : '.';
}
else {
$new_grid[$x][$y] = $neig == 3 ? '#' : '.';
}
}
}
$new_grid[0][0] = $new_grid[$size][0] = $new_grid[0][$size] = $new_grid[$size][$size] = '#';
return @new_grid;
}
sub neighbors {
my( $grid, $x, $y ) = @_;
my $sum;
for my $i ( -1..1 ) {
for my $j ( -1..1 ) {
next if !$i and !$j;
my($m,$n) = ($x+$i,$y+$j);
next if 0 > min $m, $n;
$sum += $grid->[$m][$n] eq '#';
}
}
return $sum;
}

100
2015/18/input.txt Normal file
View File

@ -0,0 +1,100 @@
#..####.##..#...#..#...#...###.#.#.#..#....#.##..#...##...#..#.....##..#####....#.##..##....##.#....
.#..#..#..#.###...##..#.##.....#...#..##....#####.##............####.#..######..#.#.##.#...#..#...##
#.....##.##.##.#..##.#..###...#.#.#..##..###.####.####.#.####.#...##.#..###.........#.###...#....###
#.###..#######..##..#.....##.#.#.###.#.##..#.##..##.##.#.##...###.#...#.#####.#.##..#.#####..#.#####
#.##.##.###.##..###.#.##.##...##.#.#..##..###.########.#.####..####...#####...#..#...##....##.##.##.
..#.#.#.#..#.#.###....###...#...#.##..####.###.....#.####.###.###.#......#.#.###..#..#.#....#.#####.
...#.###.#....#.###...#.#.#...#...#.#####....#....#...#####..#..#.#..######..#.##.#.##.#..###.#...##
.###...#...#.#..#.#.####.#...#.....##...###.#....#..##.###....#.##....###..#.#####...###.#.##.####..
#.#....##.#.....#####.#.##..#######.#.####..###.##.#####.##.#...###...#.#...###..#...#.#.###.###.###
...##.##.....##..#.##...#.#...#...#.#####.#...#.#.#.#####.##.#...#.#..##.##..#...#....####..###.###.
#..#....######...#...###.#....#####....#.#.#....#....#.#######.#####..#....#....#.##..#.##.###..#...
#####.#.######.#.#####.#..##..##..####..#....#...#######....##..##.#..###..###.###..###...#...######
#...##..##...###....##..##.##..#.#.#.#....##.#.......###..###..###...###..##.##.##.#.#.#..#.#..#..#.
..###....##.###..#.#..########...###...##..#######....##..###..#####.##.#....###..##.##.##.#...##.#.
###..#.#..#.#.##.##...##.....#..###.#..##.##.#....##.#.######..##..#.#.##.###...#..####...#.#..#.###
.######....#..##..#.####.##..#.#..#.#..#....#..##.#..#.#...####..#....#.####.#.###.#...####.#...#.#.
#.######.##..###.###..#..###.#...#..#...#...###.##....#.#......#...#.##.#.###..#.#####.#.#..###..#.#
...#..#...####..###.########.....###.###.#..##.##....######..#..#.....#.##.##.#..##..#..##...#..#..#
#..#..##..#.#.########.##.#.####..#.#####.#.###.##....###..##..#.#.###..#.##..##.##.####...######.##
.######.###....#...##...#..#....##..#.#...###.######.##...#....##.##.#.#.##..#...###.###.#....#..##.
####.#.##..##.##.###...#.###.##..##....###..####.##..#.#.##..###.#..##...####...#..####.#.#..##...#.
.#.#..#.....##...#..#...#.#...#.#.##..#....#..#......#####.#######....#.#..#..###..##.#.########..##
.##.#..#..##..#..####.#...####...#...#..##.#..###.#..######..#.#...###.##...#..#####..##.#..##.#.##.
.###..##.##.##....###.###..#.#...##.#.#...#.#######.####..#..###.#######.#...#.#...#.##...#..####..#
##.########..#..#....#.###..##.##.#.##.#..#......####..##.##.#..####..#####..#.....#####.###..#.#.#.
.#..####..##.#.#..#####.##..#..#.#....#.#####.#####...######........##.##..##.#.#.###..#.#.#.#..##.#
.##..##..#.######..###....#.#.###.#........#..###..#.########.....#.##...#.....#..#...##...#..#.###.
##.##.#..####....####.#######.....#.#.#...#.######.#.....####.####...###..####.##.##....###..#..#...
#.#..####...#......#...###...##....##.#######..#.###.#...###.##.##...####..#.####..#......##..#####.
.#.#...##...#....#.####.##.....#....#.#.#######..###.#.....#.....####...##...#.#.##.####..##.###.#.#
####.#.#.####...#...####.#.....#.#######.#.......####......###..###.#...######..#.##.#.##..#..##..##
..##.###..#..####..####.......######.##..#.....##.##...##.##......#.###..###...#.##.#####.#.######.#
.###..####.###..#..#.......#.##...##...##.######.....#..####.#......#.#...#...#...###...#.#.##..####
.####....##.##.#.....##.###.####.#.......#.......#.#..#.#.#.....###.#.#####.#..#.#.#####.#####.###.#
.##.#.###.#####..#..#....###.#.#.#..#..###..##..####..##.###....#..####.####.#..###.#..######.######
####.#.....##..###....#.....#.##.#.##..##..########.#####..###.####....##.....######.#.#.##.......#.
#.#.##.....#.....##.###.#..#.##.##....#..##....##.#.###.##.#..#..##.##.###.#..##.###...##..###.#####
#.###.#.#.#.#.#.#.#...#..#.###..####.##...#..####.###....#..#..##.#....####..##.##....#.#.##.##....#
...######....#..####...#.#..#.#.#..#.##.#.#.......#..#......##..#...#..#..##...##.#...#.#.#...##.##.
.#####..#...####....#..###..##....#####..###.#.#...###..###.###..##...#......#...#...#.#.#...#.##..#
......#####.#...#.#.#.##..#.###..##..#.#...###..###....##..#####..#######.#..#.###....###...##.#..#.
..##.########.##..#....##.#...##.##.#.#..#.##..#.#.#.##....#.#.#.#.##....##....#....#####.##..#.##.#
####...#....##.#.###......##.##.#..##...#..#####..#.#....##..#####...#.#.##...#.####.####..##.######
.##.###.##.#...#.#....###.#######...##...##..#..##.###.#.####..#..###......#.#.##.#.#....#..##...#..
.#.###.#.###.###.#.##.#..#......####.##...#..##.#..####.....#...#.###.##.##.#..#.##..#.###......#..#
...##.####......#.#.#..###..#....###....#.##.#####..#..#..#...#.#.###...#.#.#.##....###.####..###.#.
##..#.#.#.#....####...#.##.###..####....#..#####.######..#.##.##..#####.#.....#.#...##.#.##.##.#.#..
#..##.#.#.#.###.#.#.###...#.#...##..#..#.#.#.##..###...#..##.#..#.#.#..#.....#.######.#.###..###.#..
....#.#.##.###.##...#.##.#....#..##.#..##...#...#.##.####...##..####.#.........#..##..#...#...##.#..
.##.......##...###.##.#.##.###.##.#..#..#..####...#...#....#####...###..##..#..#..##...#....#..#####
..####..#...#...#..###....##.#.#####..#..#.....#......#...#.......##....####...##....##.##.#.#####.#
##.#.#.#..##..##..#.####.##.##.###.#...###.#....#.....#.###...#######..###.####.###.####.##...##.#..
..#.#...##.#....#..#..##.####.....#.#.#...#..#..###.#..###.#####.#.#####.#.#.#.#.###.##.###..#....##
.###.#...#....###..#...####....####..#.##..#..##.###..#.#.#.#..#...###.#.#...#......#...#.##.##.#...
..####.####.##.#.##....#...##....#..#....#..###..#...#..###.#####.....#####..##.#.#.#.#.#.##.####...
...##.#.##.####..##.###..#.#.#.#.#.#.#..###...#.##..#.####.##...#.#.##......###..#...###....#.#.###.
##...##..#.#.##..#.#.#....#.####.......#.#.#######.#..#....#.###.#...###.##....###.#.#..#.#.##.####.
...##.......######.....##....#...#..#.##.###.#..#.##.###.#.###.#.#.#...#.#...##.##.##..#.##########.
###..#....#.#.....#....###.#...##.......##.#.#..#.#...########......###..##.#..#..####.##..####...#.
......##.###.#.###.....#..#...#.#......##....#....#........#..#...##.##.....#...##.##.........##....
.##.##.#.#...#....######..##....##..##.#.#.##.#.##..##...#..###......##......#.#....#.#.#.......###.
.......#.##..##.#...#.##..#..#####.#..#.######.........###.#####.####.#...##...........##...##..####
#......#.#..#...#...##..#.#.###.##.##.#.#..#.###.##.#..###..#.###..#...###.##..###..#...#..###...#..
####.##..#####..####.#...#..#..###..##.#.#...#...#...#.##.####.##.###....###...#.#.#..####.######.##
.....#..####...#.#.#.####..####..##.###......#.....########.#...#.#..#..#...#.###..##.#####..###.###
.#######.#.##..###.#...###.#####............##.###...#.##.#.##..##.#.#..#.######..######..#..#..####
...##..#.####...#..#.#.##.#....#.####..#..###.###..#.#...#....##.##.#......##..##..#.#.#.###..#..#..
........#...#.##.#.#..#....####....#.##...###..####...###.#.#..######..###..##.#####.###.###.#.#...#
##......##.#..###.####.##.#.###.#.......#.##..####..#.###.##..##..##...##...#.###...#.#..#..#.#####.
##..#.#.....##.####.#..##.#.##.#.#...#...#.#...####.#.#.##...##....##.###..###.####.#...#.###..#####
.#####.####.####.####.#.##.##......###....###.####...###...#...#..#.##.#.#####.###..##.#..###...##..
.#...#..##...##...#....#.#.#..##..#.##..#.###.#.###..###.#.#.###.#....#######.####.##..#..#...####..
..##.##..#.##..#.#.###..#.##.########...####.#.###.##..#..###.###...##..##.#..#.######.##.#....###.#
##.#####.###.##.#.##.##.##.###..##..##..#.#.#.#.####..#......#.#.#.#.#.#.##...#####.####...#.#...#.#
.#..###..##.#####.#.##.#..##...##..##...#####.#.####..#...##.....######.#.#...##.#..#######.###.###.
#.#..##.#.#####.#.#.....###.###.#..##.#####....#.###.##.##.#.#..##..#.#....#######.###.#.#.....#.###
....###...#.###.####....###.....##....#####.##.###.###.##.##.##.#..###..######...####.#.#..####..#..
###.....#..####..#.####..#..#...##.##..##.######.####.....#...##....#..#.##.#####..###.##.#.####...#
.##.##.#...#..####...##.##.###...#...#..#.#.#####.....####...#.#.#..#.####...####.#...###.#......###
###.##....#.#.#...#.###....####..##...##.##.##.#..#...####..#..#..##...#####.####.####...##.#..###.#
..####.....##..###.#.#.###.########..#...#.##..#.#.#.......#.##.#..#...####.##.#..#.######..#.#...#.
#.#.##.#.#.##.#....##......##......#######.#..#.##...##..#.#.###...#.#..#..###...#..###.....##.....#
..#.##.#.##.#.##..##.....#.#..#.#..#...##..#..#.#....###.#####....####.####..#####.##.###...#..###.#
#....#.###..#..########.###..#.#.#.##...##.#..##.###..#..#..#.#.##..###...###.#.##..#.##.#..#.#.####
#.......#######......#...#...##.##...###.#....##.#..#....####.#.##.###...#.#####...##.###........##.
.##.####.....###.##......####.###.########..#.####..#.##.#.####.....#...#.##....#######.##..#......#
#.#.##.##....##..##.#.###..#.##.#..#..#.#..##.....###..###.##.##.####.##.#.#.##...####..#.#..##.#.#.
...##.#.#.#...###.#.......#.#.....#.#...##....##.##.##.####...#.#..#..#..#.#.##.#..#.#.#....###..#.#
....#.#.###.#####.##..###..##..#...#.##.#......##.####.#..####.#.##..####.#.#...##..#####..##.#.#...
..###.#.##..#....#..#.#.....##.#####..##....#.#...#.##..##.#.#..#...##.##..##..##....#...#..#..#..##
##.#.##.#...#.###.##.##.##.##..##.##...#..##.#..#######.#..#...#.#.##..#....##.#..####.###........#.
.##.#..#.....#####..##.#.#.#.#..###.#######.###.###....##....#.#.#.###....###.#..#.#....#.#..###...#
...###.#.#.###..#...#..###.######..##.#.#..#...####.#####.##..#..###...#..#..#..###..##.#.#...#.###.
#......#.#..#..##.##.#.##.#.###.#.##.#.#..#....#.##..#..##..##.#.#.#....##.###.###.####.#.#####...##
...#.##..#.######.......#.#.###.....#####....##.#.#.###........#.#.###.#.#########.##.##.#..##..#...
##..###..###....####.##.##..##.###....####..##...####.####..####..###.####..##.#...###.#####.##.##.#
###...##.#.#.#####..#..#####...##.#...#.#.###.#..##..###.##.#.#.....####.##.#..##.###.#...##.##...##
...#.#.##.##..##....#..#.#####.##.###..#.#.#........####.###.##....##....####..#.#....#.#.#.###..#.#
..#.#.#.#.###...#....##..######.##....#.#.##..###..#.#.###..#.##..#.#.###......#..#..#.####..#...##.
.....####.#.....###.#.##.#..##.#..###.#####.#..##...###.#..###..#..##....###.#..##.#..#.##.#..#...##

6
2015/18/test Normal file
View File

@ -0,0 +1,6 @@
.#.#.#
...##.
#....#
..#...
#.#..#
####..

22
2015/19/1.pl Normal file
View File

@ -0,0 +1,22 @@
use 5.20.0;
my @lines = <>;
chomp for @lines;
my $sequence = pop @lines;
pop @lines;
my %molecules;
for my $s ( @lines ) {
my( $from, $to ) = split ' => ', $s;
while( $sequence =~ /\G(.*?)($from)/g ) {
$molecules{
$` . $1 . $to . $'
} = 1;
}
}
say scalar keys %molecules;

53
2015/19/2.pl Normal file
View File

@ -0,0 +1,53 @@
use 5.20.0;
use List::AllUtils qw/ uniq /;
my @lines = <>;
chomp for @lines;
my $sequence = pop @lines;
pop @lines;
my %mutation = reverse map { split ' => ' } @lines;
my $steps = 0;
my @possible = ( $sequence );
my %s = ( $sequence => 0 );
while( not $s{e} ) {
use DDP;
warn scalar @possible;
my $n = shift @possible;
warn $n;
my @new = mutate($n);
for(@new) {
next if exists $s{$_};
$s{$_} = $s{$n}+1;
push @possible, $_;
}
@possible = sort { length $a <=> length $b } @possible;
}
say $s{e};
use experimental 'signatures';
sub mutate($sequence) {
my @molecules;
while( my( $from, $to ) = each %mutation ) {
# warn $from;
while( $sequence =~ /\G(.*?)($from)/g ) {
push @molecules, $` . $1 . $to . $'
}
}
return @molecules;
}

45
2015/19/input.txt Normal file
View File

@ -0,0 +1,45 @@
Al => ThF
Al => ThRnFAr
B => BCa
B => TiB
B => TiRnFAr
Ca => CaCa
Ca => PB
Ca => PRnFAr
Ca => SiRnFYFAr
Ca => SiRnMgAr
Ca => SiTh
F => CaF
F => PMg
F => SiAl
H => CRnAlAr
H => CRnFYFYFAr
H => CRnFYMgAr
H => CRnMgYFAr
H => HCa
H => NRnFYFAr
H => NRnMgAr
H => NTh
H => OB
H => ORnFAr
Mg => BF
Mg => TiMg
N => CRnFAr
N => HSi
O => CRnFYFAr
O => CRnMgAr
O => HP
O => NRnFAr
O => OTi
P => CaP
P => PTi
P => SiRnFAr
Si => CaSi
Th => ThCa
Ti => BP
Ti => TiTi
e => HF
e => NAl
e => OMg
ORnPBPMgArCaCaCaSiThCaCaSiThCaCaPBSiRnFArRnFArCaCaSiThCaCaSiThCaCaCaCaCaCaSiRnFYFArSiRnMgArCaSiRnPTiTiBFYPBFArSiRnCaSiRnTiRnFArSiAlArPTiBPTiRnCaSiAlArCaPTiTiBPMgYFArPTiRnFArSiRnCaCaFArRnCaFArCaSiRnSiRnMgArFYCaSiRnMgArCaCaSiThPRnFArPBCaSiRnMgArCaCaSiThCaSiRnTiMgArFArSiThSiThCaCaSiRnMgArCaCaSiRnFArTiBPTiRnCaSiAlArCaPTiRnFArPBPBCaCaSiThCaPBSiThPRnFArSiThCaSiThCaSiThCaPTiBSiRnFYFArCaCaPRnFArPBCaCaPBSiRnTiRnFArCaPRnFArSiRnCaCaCaSiThCaRnCaFArYCaSiRnFArBCaCaCaSiThFArPBFArCaSiRnFArRnCaCaCaFArSiRnFArTiRnPMgArF

18
2015/20/1.pl Normal file
View File

@ -0,0 +1,18 @@
use 5.20.0;
use List::AllUtils qw/ sum /;
use Math::Prime::Util ':all';
my $x = 3400000;
my $house = 1;
$house++ while( nbr_gifts($house) < $x );
say $house;
sub nbr_gifts {
my $n = shift;
sum divisors($n);
}

18
2015/20/2.pl Normal file
View File

@ -0,0 +1,18 @@
use 5.20.0;
use List::AllUtils qw/ sum /;
use Math::Prime::Util ':all';
my $x = 34000000;
my $house = 1;
$house++ while( nbr_gifts($house) < $x );
say $house;
sub nbr_gifts {
my $n = shift;
11 * sum grep { 50 * $_ >= $n } divisors($n);
}

72
2015/21/1.pl Normal file
View File

@ -0,0 +1,72 @@
use 5.20.0;
use Path::Tiny;
use Algorithm::Combinatorics qw/ combinations /;
use List::AllUtils qw/ sum max /;
use 5.20.0;
use experimental 'postderef';
my @weapons = (
#Weapons: Cost Damage Armor
[ qw/ Dagger 8 4 0 / ],
[ qw/ Shortsword 10 5 0/ ],
[ qw/ Warhammer 25 6 0/ ],
[ qw/ Longsword 40 7 0/ ],
[ qw/ Greataxe 74 8 0/ ],
);
my @armors = (
#Armor: Cost Damage Armor
[ qw/ Leather 13 0 1/ ],
[ qw/ Chainmail 31 0 2/ ],
[ qw/ Splintmail 53 0 3/ ],
[ qw/ Bandedmail 75 0 4/ ],
[ qw/ Platemail 102 0 5/ ],
);
my @rings = (
#Rings: Cost Damage Armor
[ qw/ Damage+1 25 1 0/ ],
[ qw/ Damage+2 50 2 0/ ],
[ qw/ Damage+3 100 3 0/ ],
[ qw/ Defense+1 20 0 1/ ],
[ qw/ Defense+2 40 0 2/ ],
[ qw/ Defense+3 80 0 3/ ],
);
my @boss = map { /(\d+)/ } path('input.txt')->lines;
use DDP;
p @boss;
my $min_price = 1E9;
for my $weapon ( @weapons ) {
for my $armor ( [], @armors ) {
for my $rings( [[]], ( map { [ $_ ] } @rings ), combinations( \@rings, 2 ) ) {
my $price = sum map { $_->[1] } $weapon, $armor,
$rings->@*;
next if $price > $min_price;
my @stats = ( 100, map { my $i = $_; sum map { $_->[$i] } $weapon, $armor, $rings->@* } 2,3 );
$min_price = $price if win_fight( \@stats, [ @boss ] );
}
}
}
say $min_price;
use experimental 'signatures';
sub win_fight( $me, $opponent ) {
p @_;
my $damage = max 1, $me->[1] - $opponent->[2];
$opponent->[0] -= $damage;
return 1 if $opponent->[0] <= 0;
return not win_fight( reverse @_ );
}

72
2015/21/2.pl Normal file
View File

@ -0,0 +1,72 @@
use 5.20.0;
use Path::Tiny;
use Algorithm::Combinatorics qw/ combinations /;
use List::AllUtils qw/ sum max min /;
use 5.20.0;
use experimental 'postderef';
my @weapons = (
#Weapons: Cost Damage Armor
[ qw/ Dagger 8 4 0 / ],
[ qw/ Shortsword 10 5 0/ ],
[ qw/ Warhammer 25 6 0/ ],
[ qw/ Longsword 40 7 0/ ],
[ qw/ Greataxe 74 8 0/ ],
);
my @armors = (
#Armor: Cost Damage Armor
[ qw/ Leather 13 0 1/ ],
[ qw/ Chainmail 31 0 2/ ],
[ qw/ Splintmail 53 0 3/ ],
[ qw/ Bandedmail 75 0 4/ ],
[ qw/ Platemail 102 0 5/ ],
);
my @rings = (
#Rings: Cost Damage Armor
[ qw/ Damage+1 25 1 0/ ],
[ qw/ Damage+2 50 2 0/ ],
[ qw/ Damage+3 100 3 0/ ],
[ qw/ Defense+1 20 0 1/ ],
[ qw/ Defense+2 40 0 2/ ],
[ qw/ Defense+3 80 0 3/ ],
);
my @boss = map { /(\d+)/ } path('input.txt')->lines;
use DDP;
p @boss;
my $max_price = 0;
for my $weapon ( @weapons ) {
for my $armor ( [], @armors ) {
for my $rings( [[]], ( map { [ $_ ] } @rings ), combinations( \@rings, 2 ) ) {
my $price = sum map { $_->[1] } $weapon, $armor,
$rings->@*;
next if $price < $max_price;
my @stats = ( 100, map { my $i = $_; sum map { $_->[$i] } $weapon, $armor, $rings->@* } 2,3 );
$max_price = $price unless win_fight( \@stats, [ @boss ] );
}
}
}
say $max_price;
use experimental 'signatures';
sub win_fight( $me, $opponent ) {
p @_;
my $damage = max 1, $me->[1] - $opponent->[2];
$opponent->[0] -= $damage;
return 1 if $opponent->[0] <= 0;
return not win_fight( reverse @_ );
}

3
2015/21/input.txt Normal file
View File

@ -0,0 +1,3 @@
Hit Points: 109
Damage: 8
Armor: 2