/day15-1.pl6 Secret
Created
December 15, 2015 14:24
Advent of Code Day 15
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
my @ingredients; | |
for "day15-data.txt".IO.lines { | |
if / [\w+] ": capacity " ('-'?\d+) ", durability " ('-'?\d+) ", flavor " ('-'?\d+) \ | |
", texture " ('-'?\d+) ", calories " ('-'?\d+) / { | |
my int @this = +$0, +$1, +$2, +$3, +$4; | |
@ingredients.push(@this.item); | |
} | |
} | |
@ingredients.say; | |
sub score(@stats) { | |
return [*] @stats.map({max(0, $^a)}); | |
} | |
sub compute(int @cumulative, int @stats, int $rem, int $leftmost) { | |
@cumulative.say; | |
#@stats.say; | |
loop (my int $i = 0; $i < $leftmost; $i = $i + 1) { | |
if @stats[$i] < 0 { | |
return @cumulative, @stats; | |
} | |
} | |
if $rem == 0 { | |
return @cumulative, @stats; | |
} else { | |
my Int $max = 0; | |
my $l = @cumulative.elems; | |
my int @max-ingredients; | |
my int @max-stats; | |
loop (my int $i = $leftmost; $i < $l; $i = $i + 1) { | |
my int @modified = @cumulative; | |
my int @newstats = @stats; | |
@newstats Z+= @ingredients[$i]; | |
++@modified[$i]; | |
my @result = compute(@modified, @newstats, $rem - 1, $i); | |
my int @opt-ingredients = @result[0]; | |
my int @opt-stats = @result[1]; | |
my $score = score(@opt-stats); | |
if $score > $max { | |
$max = $score; | |
@max-ingredients = @opt-ingredients; | |
@max-stats = @opt-stats; | |
} | |
} | |
return @max-ingredients, @max-stats; | |
} | |
} | |
my int @start = 0, 0, 0, 0; | |
say compute(@start, @start, 100, 0).perl; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use v6; | |
my @ingredients; | |
for "day15-data.txt".IO.lines { | |
if / [\w+] ": capacity " ('-'?\d+) ", durability " ('-'?\d+) ", flavor " ('-'?\d+) \ | |
", texture " ('-'?\d+) ", calories " ('-'?\d+) / { | |
my int @this = +$0, +$1, +$2, +$3, +$4; | |
@ingredients.push(@this.item); | |
} | |
} | |
@ingredients.say; | |
sub score(@stats) { | |
return [*] @stats.map({max(0, $^a)}); | |
} | |
sub compute(int @cumulative, int @stats, int $rem, int $leftmost) { | |
@cumulative.say; | |
#@stats.say; | |
loop (my int $i = 0; $i < $leftmost; $i = $i + 1) { | |
if @stats[$i] < 0 { | |
return @cumulative, @stats; | |
} | |
} | |
my int @null-res = 0 xx @stats.elems; | |
if $rem == 0 { | |
return @cumulative, @stats if @stats[4] == 500; | |
return @cumulative, @null-res; | |
} elsif @stats[4] > 500 { | |
return @cumulative, @null-res; | |
} else { | |
my Int $max = 0; | |
my $l = @cumulative.elems; | |
my int @max-ingredients; | |
my int @max-stats; | |
loop (my int $i = $leftmost; $i < $l; $i = $i + 1) { | |
my int @modified = @cumulative; | |
my int @newstats = @stats; | |
@newstats Z+= @ingredients[$i]; | |
++@modified[$i]; | |
my @result = compute(@modified, @newstats, $rem - 1, $i); | |
my int @opt-ingredients = @result[0]; | |
my int @opt-stats = @result[1]; | |
my $score = score(@opt-stats); | |
if $score > $max { | |
$max = $score; | |
@max-ingredients = @opt-ingredients; | |
@max-stats = @opt-stats; | |
} | |
} | |
return @max-ingredients, @max-stats; | |
} | |
} | |
my int @start = 0, 0, 0, 0; | |
my int @stat = 0, 0, 0, 0, 0; | |
say compute(@start, @stat, 100, 0).perl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment