/day20-1b.pl6 Secret
Last active
December 20, 2015 20:50
Day 20 solution
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 $thresh = 3400000; | |
my @bases = 2, 3, 5, 7, 11, 13, 17, 19; | |
sub returnInvestment($base, $exponent) { | |
my $return = ($base ** ($exponent + 2) - 1) / ($base ** ($exponent + 1) - 1); | |
my $investment = $base; | |
return $return / $investment; | |
} | |
my $minHN = Inf; | |
my @minVals; | |
my @start = 0 xx @bases.elems; | |
my $sHN = 1; | |
my $sPres = 1; | |
while $sPres < $thresh / 10000 { | |
my @ris = (@bases Z @start).map({returnInvestment(|@^a)}); | |
my $maxVal = 0; | |
my $maxIndex; | |
loop (my $i = 0; $i < @ris.elems; ++$i) { | |
if @ris[$i] > $maxVal { | |
$maxIndex = $i; | |
$maxVal = @ris[$i]; | |
} | |
} | |
my $base = @bases[$maxIndex]; | |
my $exponent = @start[$maxIndex]; | |
$sPres /= ($base ** ($exponent + 1) - 1); | |
$sPres *= ($base ** ($exponent + 2) - 1); | |
$sHN *= $base; | |
++@start[$maxIndex]; | |
} | |
for ^10000 { | |
my @vals = @start; | |
my $hn = $sHN; | |
my $pres = $sPres; | |
while $pres < $thresh { | |
my $i = (^@bases.elems).roll; | |
my $base = @bases[$i]; | |
my $exponent = @vals[$i]; | |
$pres /= ($base ** ($exponent + 1) - 1); | |
$pres *= ($base ** ($exponent + 2) - 1); | |
$hn *= $base; | |
++@vals[$i]; | |
} | |
if $hn < $minHN { | |
$minHN = $hn; | |
@minVals = @vals; | |
say "$minHN gets $pres presents" | |
} | |
} | |
say $minHN; | |
say @minVals; |
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 $thresh = 34000000 div 11; | |
my @bases = 2, 3, 5, 7, 11, 13, 17, 19; | |
sub score(@vals, $hn) { | |
my @factors = [X*] (@bases Z @vals).map({@^a[0] «**« (0 ... @^a[1])}).List; | |
my $ct = [+] @factors.grep($hn <= 50 * *); | |
} | |
my $minHN = Inf; | |
my @minVals; | |
my @start = 0 xx @bases.elems; | |
my $sHN = 1; | |
my $sPres = 1; | |
my @facts = 1; | |
while $sPres < $thresh / 100 { | |
my $maxVal = 0; | |
my $maxIndex; | |
my @maxFacts; | |
loop (my $i = 0; $i < @bases.elems; ++$i) { | |
my $base = @bases[$i]; | |
my @mfacts = ((1, $base) X* @facts).flat.unique.grep($sHN * $base <= 50 * *); | |
my $mscore = ([+] @mfacts) / (@bases[$i] * $sPres); | |
if $mscore > $maxVal { | |
$maxIndex = $i; | |
$maxVal = $mscore; | |
@maxFacts = @mfacts; | |
} | |
} | |
my $base = @bases[$maxIndex]; | |
$sPres = [+] @maxFacts; | |
$sHN *= $base; | |
++@start[$maxIndex]; | |
@facts = @maxFacts; | |
say @facts; | |
say $sHN; | |
} | |
say "seed generated"; | |
for ^10000 { | |
my @vals = @start; | |
my $hn = $sHN; | |
my $pres = $sPres; | |
my @factors = @facts; | |
while $pres < $thresh { | |
my $i = (^@bases.elems).roll; | |
my $base = @bases[$i]; | |
@factors = ((1, @bases[$i]) X* @factors).flat.unique.grep($hn * $base <= 50 * *); | |
$pres = [+] @factors; | |
$hn *= $base; | |
++@vals[$i]; | |
} | |
if $hn < $minHN { | |
$minHN = $hn; | |
@minVals = @vals; | |
say "Iteration $_: $minHN gets $pres presents" | |
} | |
} | |
say $minHN; | |
say @minVals; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment