in reply to 1. Go compare! Guardian's algortithm riddle and mathematical proof
Before revealing your spoiler or following the link in the original post, here is my attempt to solve the puzzle.
I cannot prove this being optimal and I don't even have an idea about how to prove it. Tough stuff.
My solution needs 148 comparisons in any case.
#!/usr/bin/perl use v5.24; use warnings; use experimental qw(signatures); package HiddenDeck; use List::Util 'shuffle'; sub new ($class, $size) { bless {cards => [shuffle 1 .. $size], numcmp => 0, revealed => 0}, + $class; } sub cmp ($self, $i, $k) { die if $self->{revealed}; $self->{numcmp}++; $self->{cards}[$i] <=> $self->{cards}[$k]; } sub numcmp ($self) { $self->{numcmp}; } sub reveal($self, @i) { $self->{revealed} = 1; [$self->{cards}->@[@i]]; } package main; sub pair_join ($hd, $a, $b) { [$hd->cmp($a->[0], $b->[0]) > 0 ? $b->[0] : $a->[0], $hd->cmp($a->[1], $b->[1]) > 0 ? $a->[1] : $b->[1]]; } use constant N => 100; my $hd = HiddenDeck->new(N); my @pairs; for my $i (0 .. N/2 - 1) { push @pairs, $hd->cmp(2 * $i, 2 * $i + 1) > 0 ? [2 * $i + 1, 2 * $i] : [2 * $i, 2 * $i + 1]; } while (@pairs > 1) { my @pairs1; for my $i (0 .. @pairs/2 - 1) { push @pairs1, pair_join($hd, $pairs[2 * $i], $pairs[2 * $i + 1 +]); } if (@pairs % 2) { $pairs1[-1] = pair_join($hd, $pairs[-1], $pairs1[-1]); } @pairs = @pairs1; } say "@{$hd->reveal($pairs[0]->@*)}"; say "count = ", $hd->numcmp;
I'm curious to look at the hidden/linked resources.
Update: I had expected more information from following the link. Waiting for lanx's proof 😀.
Greetings,
🐻
|
---|