Skip to content

Instantly share code, notes, and snippets.

@bluebear94
Created December 15, 2015 14:24
Show Gist options
  • Save bluebear94/e3c10f8de8c289ba48bb to your computer and use it in GitHub Desktop.
Save bluebear94/e3c10f8de8c289ba48bb to your computer and use it in GitHub Desktop.
Advent of Code Day 15
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;
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