From 2b88e2660e8d4a889cf719bffa035a723bca9d2d Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 12 Dec 2017 19:55:17 -0500 Subject: [PATCH] lots of days --- 2015/18/1.pl | 57 ++++++++++++++++++++++++++ 2015/18/2.pl | 57 ++++++++++++++++++++++++++ 2015/18/input.txt | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 2015/18/test | 6 +++ 2015/19/1.pl | 22 ++++++++++ 2015/19/2.pl | 53 ++++++++++++++++++++++++ 2015/19/input.txt | 45 +++++++++++++++++++++ 2015/20/1.pl | 18 +++++++++ 2015/20/2.pl | 18 +++++++++ 2015/21/1.pl | 72 +++++++++++++++++++++++++++++++++ 2015/21/2.pl | 72 +++++++++++++++++++++++++++++++++ 2015/21/input.txt | 3 ++ 12 files changed, 523 insertions(+) create mode 100644 2015/18/1.pl create mode 100644 2015/18/2.pl create mode 100644 2015/18/input.txt create mode 100644 2015/18/test create mode 100644 2015/19/1.pl create mode 100644 2015/19/2.pl create mode 100644 2015/19/input.txt create mode 100644 2015/20/1.pl create mode 100644 2015/20/2.pl create mode 100644 2015/21/1.pl create mode 100644 2015/21/2.pl create mode 100644 2015/21/input.txt diff --git a/2015/18/1.pl b/2015/18/1.pl new file mode 100644 index 0000000..52aae39 --- /dev/null +++ b/2015/18/1.pl @@ -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; +} + diff --git a/2015/18/2.pl b/2015/18/2.pl new file mode 100644 index 0000000..e276037 --- /dev/null +++ b/2015/18/2.pl @@ -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; +} + diff --git a/2015/18/input.txt b/2015/18/input.txt new file mode 100644 index 0000000..83f3cb2 --- /dev/null +++ b/2015/18/input.txt @@ -0,0 +1,100 @@ +#..####.##..#...#..#...#...###.#.#.#..#....#.##..#...##...#..#.....##..#####....#.##..##....##.#.... +.#..#..#..#.###...##..#.##.....#...#..##....#####.##............####.#..######..#.#.##.#...#..#...## +#.....##.##.##.#..##.#..###...#.#.#..##..###.####.####.#.####.#...##.#..###.........#.###...#....### +#.###..#######..##..#.....##.#.#.###.#.##..#.##..##.##.#.##...###.#...#.#####.#.##..#.#####..#.##### +#.##.##.###.##..###.#.##.##...##.#.#..##..###.########.#.####..####...#####...#..#...##....##.##.##. +..#.#.#.#..#.#.###....###...#...#.##..####.###.....#.####.###.###.#......#.#.###..#..#.#....#.#####. +...#.###.#....#.###...#.#.#...#...#.#####....#....#...#####..#..#.#..######..#.##.#.##.#..###.#...## +.###...#...#.#..#.#.####.#...#.....##...###.#....#..##.###....#.##....###..#.#####...###.#.##.####.. +#.#....##.#.....#####.#.##..#######.#.####..###.##.#####.##.#...###...#.#...###..#...#.#.###.###.### +...##.##.....##..#.##...#.#...#...#.#####.#...#.#.#.#####.##.#...#.#..##.##..#...#....####..###.###. +#..#....######...#...###.#....#####....#.#.#....#....#.#######.#####..#....#....#.##..#.##.###..#... +#####.#.######.#.#####.#..##..##..####..#....#...#######....##..##.#..###..###.###..###...#...###### +#...##..##...###....##..##.##..#.#.#.#....##.#.......###..###..###...###..##.##.##.#.#.#..#.#..#..#. +..###....##.###..#.#..########...###...##..#######....##..###..#####.##.#....###..##.##.##.#...##.#. +###..#.#..#.#.##.##...##.....#..###.#..##.##.#....##.#.######..##..#.#.##.###...#..####...#.#..#.### +.######....#..##..#.####.##..#.#..#.#..#....#..##.#..#.#...####..#....#.####.#.###.#...####.#...#.#. +#.######.##..###.###..#..###.#...#..#...#...###.##....#.#......#...#.##.#.###..#.#####.#.#..###..#.# +...#..#...####..###.########.....###.###.#..##.##....######..#..#.....#.##.##.#..##..#..##...#..#..# +#..#..##..#.#.########.##.#.####..#.#####.#.###.##....###..##..#.#.###..#.##..##.##.####...######.## +.######.###....#...##...#..#....##..#.#...###.######.##...#....##.##.#.#.##..#...###.###.#....#..##. +####.#.##..##.##.###...#.###.##..##....###..####.##..#.#.##..###.#..##...####...#..####.#.#..##...#. +.#.#..#.....##...#..#...#.#...#.#.##..#....#..#......#####.#######....#.#..#..###..##.#.########..## +.##.#..#..##..#..####.#...####...#...#..##.#..###.#..######..#.#...###.##...#..#####..##.#..##.#.##. +.###..##.##.##....###.###..#.#...##.#.#...#.#######.####..#..###.#######.#...#.#...#.##...#..####..# +##.########..#..#....#.###..##.##.#.##.#..#......####..##.##.#..####..#####..#.....#####.###..#.#.#. +.#..####..##.#.#..#####.##..#..#.#....#.#####.#####...######........##.##..##.#.#.###..#.#.#.#..##.# +.##..##..#.######..###....#.#.###.#........#..###..#.########.....#.##...#.....#..#...##...#..#.###. +##.##.#..####....####.#######.....#.#.#...#.######.#.....####.####...###..####.##.##....###..#..#... +#.#..####...#......#...###...##....##.#######..#.###.#...###.##.##...####..#.####..#......##..#####. +.#.#...##...#....#.####.##.....#....#.#.#######..###.#.....#.....####...##...#.#.##.####..##.###.#.# +####.#.#.####...#...####.#.....#.#######.#.......####......###..###.#...######..#.##.#.##..#..##..## +..##.###..#..####..####.......######.##..#.....##.##...##.##......#.###..###...#.##.#####.#.######.# +.###..####.###..#..#.......#.##...##...##.######.....#..####.#......#.#...#...#...###...#.#.##..#### +.####....##.##.#.....##.###.####.#.......#.......#.#..#.#.#.....###.#.#####.#..#.#.#####.#####.###.# +.##.#.###.#####..#..#....###.#.#.#..#..###..##..####..##.###....#..####.####.#..###.#..######.###### +####.#.....##..###....#.....#.##.#.##..##..########.#####..###.####....##.....######.#.#.##.......#. +#.#.##.....#.....##.###.#..#.##.##....#..##....##.#.###.##.#..#..##.##.###.#..##.###...##..###.##### +#.###.#.#.#.#.#.#.#...#..#.###..####.##...#..####.###....#..#..##.#....####..##.##....#.#.##.##....# +...######....#..####...#.#..#.#.#..#.##.#.#.......#..#......##..#...#..#..##...##.#...#.#.#...##.##. +.#####..#...####....#..###..##....#####..###.#.#...###..###.###..##...#......#...#...#.#.#...#.##..# +......#####.#...#.#.#.##..#.###..##..#.#...###..###....##..#####..#######.#..#.###....###...##.#..#. +..##.########.##..#....##.#...##.##.#.#..#.##..#.#.#.##....#.#.#.#.##....##....#....#####.##..#.##.# +####...#....##.#.###......##.##.#..##...#..#####..#.#....##..#####...#.#.##...#.####.####..##.###### +.##.###.##.#...#.#....###.#######...##...##..#..##.###.#.####..#..###......#.#.##.#.#....#..##...#.. +.#.###.#.###.###.#.##.#..#......####.##...#..##.#..####.....#...#.###.##.##.#..#.##..#.###......#..# +...##.####......#.#.#..###..#....###....#.##.#####..#..#..#...#.#.###...#.#.#.##....###.####..###.#. +##..#.#.#.#....####...#.##.###..####....#..#####.######..#.##.##..#####.#.....#.#...##.#.##.##.#.#.. +#..##.#.#.#.###.#.#.###...#.#...##..#..#.#.#.##..###...#..##.#..#.#.#..#.....#.######.#.###..###.#.. +....#.#.##.###.##...#.##.#....#..##.#..##...#...#.##.####...##..####.#.........#..##..#...#...##.#.. +.##.......##...###.##.#.##.###.##.#..#..#..####...#...#....#####...###..##..#..#..##...#....#..##### +..####..#...#...#..###....##.#.#####..#..#.....#......#...#.......##....####...##....##.##.#.#####.# +##.#.#.#..##..##..#.####.##.##.###.#...###.#....#.....#.###...#######..###.####.###.####.##...##.#.. +..#.#...##.#....#..#..##.####.....#.#.#...#..#..###.#..###.#####.#.#####.#.#.#.#.###.##.###..#....## +.###.#...#....###..#...####....####..#.##..#..##.###..#.#.#.#..#...###.#.#...#......#...#.##.##.#... +..####.####.##.#.##....#...##....#..#....#..###..#...#..###.#####.....#####..##.#.#.#.#.#.##.####... +...##.#.##.####..##.###..#.#.#.#.#.#.#..###...#.##..#.####.##...#.#.##......###..#...###....#.#.###. +##...##..#.#.##..#.#.#....#.####.......#.#.#######.#..#....#.###.#...###.##....###.#.#..#.#.##.####. +...##.......######.....##....#...#..#.##.###.#..#.##.###.#.###.#.#.#...#.#...##.##.##..#.##########. +###..#....#.#.....#....###.#...##.......##.#.#..#.#...########......###..##.#..#..####.##..####...#. +......##.###.#.###.....#..#...#.#......##....#....#........#..#...##.##.....#...##.##.........##.... +.##.##.#.#...#....######..##....##..##.#.#.##.#.##..##...#..###......##......#.#....#.#.#.......###. +.......#.##..##.#...#.##..#..#####.#..#.######.........###.#####.####.#...##...........##...##..#### +#......#.#..#...#...##..#.#.###.##.##.#.#..#.###.##.#..###..#.###..#...###.##..###..#...#..###...#.. +####.##..#####..####.#...#..#..###..##.#.#...#...#...#.##.####.##.###....###...#.#.#..####.######.## +.....#..####...#.#.#.####..####..##.###......#.....########.#...#.#..#..#...#.###..##.#####..###.### +.#######.#.##..###.#...###.#####............##.###...#.##.#.##..##.#.#..#.######..######..#..#..#### +...##..#.####...#..#.#.##.#....#.####..#..###.###..#.#...#....##.##.#......##..##..#.#.#.###..#..#.. +........#...#.##.#.#..#....####....#.##...###..####...###.#.#..######..###..##.#####.###.###.#.#...# +##......##.#..###.####.##.#.###.#.......#.##..####..#.###.##..##..##...##...#.###...#.#..#..#.#####. +##..#.#.....##.####.#..##.#.##.#.#...#...#.#...####.#.#.##...##....##.###..###.####.#...#.###..##### +.#####.####.####.####.#.##.##......###....###.####...###...#...#..#.##.#.#####.###..##.#..###...##.. +.#...#..##...##...#....#.#.#..##..#.##..#.###.#.###..###.#.#.###.#....#######.####.##..#..#...####.. +..##.##..#.##..#.#.###..#.##.########...####.#.###.##..#..###.###...##..##.#..#.######.##.#....###.# +##.#####.###.##.#.##.##.##.###..##..##..#.#.#.#.####..#......#.#.#.#.#.#.##...#####.####...#.#...#.# +.#..###..##.#####.#.##.#..##...##..##...#####.#.####..#...##.....######.#.#...##.#..#######.###.###. +#.#..##.#.#####.#.#.....###.###.#..##.#####....#.###.##.##.#.#..##..#.#....#######.###.#.#.....#.### +....###...#.###.####....###.....##....#####.##.###.###.##.##.##.#..###..######...####.#.#..####..#.. +###.....#..####..#.####..#..#...##.##..##.######.####.....#...##....#..#.##.#####..###.##.#.####...# +.##.##.#...#..####...##.##.###...#...#..#.#.#####.....####...#.#.#..#.####...####.#...###.#......### +###.##....#.#.#...#.###....####..##...##.##.##.#..#...####..#..#..##...#####.####.####...##.#..###.# +..####.....##..###.#.#.###.########..#...#.##..#.#.#.......#.##.#..#...####.##.#..#.######..#.#...#. +#.#.##.#.#.##.#....##......##......#######.#..#.##...##..#.#.###...#.#..#..###...#..###.....##.....# +..#.##.#.##.#.##..##.....#.#..#.#..#...##..#..#.#....###.#####....####.####..#####.##.###...#..###.# +#....#.###..#..########.###..#.#.#.##...##.#..##.###..#..#..#.#.##..###...###.#.##..#.##.#..#.#.#### +#.......#######......#...#...##.##...###.#....##.#..#....####.#.##.###...#.#####...##.###........##. +.##.####.....###.##......####.###.########..#.####..#.##.#.####.....#...#.##....#######.##..#......# +#.#.##.##....##..##.#.###..#.##.#..#..#.#..##.....###..###.##.##.####.##.#.#.##...####..#.#..##.#.#. +...##.#.#.#...###.#.......#.#.....#.#...##....##.##.##.####...#.#..#..#..#.#.##.#..#.#.#....###..#.# +....#.#.###.#####.##..###..##..#...#.##.#......##.####.#..####.#.##..####.#.#...##..#####..##.#.#... +..###.#.##..#....#..#.#.....##.#####..##....#.#...#.##..##.#.#..#...##.##..##..##....#...#..#..#..## +##.#.##.#...#.###.##.##.##.##..##.##...#..##.#..#######.#..#...#.#.##..#....##.#..####.###........#. +.##.#..#.....#####..##.#.#.#.#..###.#######.###.###....##....#.#.#.###....###.#..#.#....#.#..###...# +...###.#.#.###..#...#..###.######..##.#.#..#...####.#####.##..#..###...#..#..#..###..##.#.#...#.###. +#......#.#..#..##.##.#.##.#.###.#.##.#.#..#....#.##..#..##..##.#.#.#....##.###.###.####.#.#####...## +...#.##..#.######.......#.#.###.....#####....##.#.#.###........#.#.###.#.#########.##.##.#..##..#... +##..###..###....####.##.##..##.###....####..##...####.####..####..###.####..##.#...###.#####.##.##.# +###...##.#.#.#####..#..#####...##.#...#.#.###.#..##..###.##.#.#.....####.##.#..##.###.#...##.##...## +...#.#.##.##..##....#..#.#####.##.###..#.#.#........####.###.##....##....####..#.#....#.#.#.###..#.# +..#.#.#.#.###...#....##..######.##....#.#.##..###..#.#.###..#.##..#.#.###......#..#..#.####..#...##. +.....####.#.....###.#.##.#..##.#..###.#####.#..##...###.#..###..#..##....###.#..##.#..#.##.#..#...## diff --git a/2015/18/test b/2015/18/test new file mode 100644 index 0000000..6ddce72 --- /dev/null +++ b/2015/18/test @@ -0,0 +1,6 @@ +.#.#.# +...##. +#....# +..#... +#.#..# +####.. diff --git a/2015/19/1.pl b/2015/19/1.pl new file mode 100644 index 0000000..7a90ae4 --- /dev/null +++ b/2015/19/1.pl @@ -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; + + diff --git a/2015/19/2.pl b/2015/19/2.pl new file mode 100644 index 0000000..7136886 --- /dev/null +++ b/2015/19/2.pl @@ -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; +} + + + diff --git a/2015/19/input.txt b/2015/19/input.txt new file mode 100644 index 0000000..8bb6fa8 --- /dev/null +++ b/2015/19/input.txt @@ -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 diff --git a/2015/20/1.pl b/2015/20/1.pl new file mode 100644 index 0000000..21570d9 --- /dev/null +++ b/2015/20/1.pl @@ -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); +} diff --git a/2015/20/2.pl b/2015/20/2.pl new file mode 100644 index 0000000..9a53890 --- /dev/null +++ b/2015/20/2.pl @@ -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); +} diff --git a/2015/21/1.pl b/2015/21/1.pl new file mode 100644 index 0000000..54cb2d9 --- /dev/null +++ b/2015/21/1.pl @@ -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 @_ ); +} + diff --git a/2015/21/2.pl b/2015/21/2.pl new file mode 100644 index 0000000..4069d2a --- /dev/null +++ b/2015/21/2.pl @@ -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 @_ ); +} + diff --git a/2015/21/input.txt b/2015/21/input.txt new file mode 100644 index 0000000..c5f2409 --- /dev/null +++ b/2015/21/input.txt @@ -0,0 +1,3 @@ +Hit Points: 109 +Damage: 8 +Armor: 2