Task 1: Kolakoski Sequence
"The Self-Describing Sequence: How Many Ones?"
This week's first task involves generating the Kolakoski sequence, a self-describing sequence where each term describes the run-length of the sequence itself. Given an integer n > 0, we need to generate the first n terms of the Kolakoski sequence and count how many 1s appear.
The Strategy: We build the sequence iteratively. Starting with [1, 2, 2], we use the sequence itself to determine run lengths. We read the next term, generate that many copies of the alternating value (1 or 2), then switch the value for the next run. We continue until we have at least n terms, then count the 1s.
sub kolakoski_ones ($n) {
($n) = $INT_CHECK->($n);
die 'Expected n > 3' if $n <= 3;
my @seq = ( 1, 2, 2 );
my $read_idx = 2;
my $next_val = 1;
while ( @seq < $n ) {
my $run = $seq[$read_idx++];
push @seq, ($next_val) x $run;
$next_val = $next_val == 1 ? 2 : 1;
}
splice @seq, $n if @seq > $n;
my $count = 0;
$count++ for grep { $_ == 1 } @seq;
return $count;
}
def kolakoski_ones(n: int) -> int:
"""Return count of 1s in first n terms of the Kolakoski sequence (n > 3)."""
if n <= 3:
raise ValueError("Expected n > 3")
seq: list[int] = [1, 2, 2]
read_idx = 2
next_val = 1
while len(seq) < n:
run = seq[read_idx]
read_idx += 1
seq.extend([next_val] * run)
next_val = 2 if next_val == 1 else 1
seq = seq[:n]
return sum(1 for x in seq if x == 1)
Task 2: Who Wins
"NFL Playoff Simulator: From Wild Card to Conference Champion!"
The second task simulates NFL playoff brackets. Given a 6-character string of 'H' (home) and 'A' (away) representing winners of each playoff game, we need to determine which two teams played in the conference final and who won.
The Strategy: We simulate the three-week playoff structure. First week: wild card games (2v7, 3v6, 4v5). Second week: divisional games (1 vs lowest seed, highest vs second seed). Third week: conference final. We track which teams advance based on the input string and determine the final matchup and winner.
sub who_wins ($results) {
($results) = $STR_CHECK->($results);
die 'Expected a 6-character string of H/A' if $results !~ /\A[HA]{6}\z/;
my @r = split //, $results;
# Week 1
my $w1 = $r[0] eq 'H' ? 2 : 7;
my $w2 = $r[1] eq 'H' ? 3 : 6;
my $w3 = $r[2] eq 'H' ? 4 : 5;
my @w = sort { $a <=> $b } ( $w1, $w2, $w3 );
# Week 2 game 4: Team 1 hosts the third seeded winner => max seed number
my $t4_away = $w[2];
my $w4 = $r[3] eq 'H' ? 1 : $t4_away;
my @rem = ( $w[0], $w[1] );
# Week 2 game 5: highest seeded winner hosts the second seeded winner
my ($host5, $away5) = $rem[0] < $rem[1] ? ( $rem[0], $rem[1] ) : ( $rem[1], $rem[0] );
my $w5 = $r[4] eq 'H' ? $host5 : $away5;
# Week 3 game 6: highest seeded winner hosts the other winner
my ($host6, $away6) = $w4 < $w5 ? ( $w4, $w5 ) : ( $w5, $w4 );
my $w6 = $r[5] eq 'H' ? $host6 : $away6;
return ( $host6, $away6, $w6 );
}
def who_wins(results: str) -> tuple[int, int, int]:
"""Return (home_team, away_team, winner_team) for the conference final."""
if len(results) != 6 or any(ch not in "HA" for ch in results):
raise ValueError("Expected a 6-character string of H/A")
r = list(results)
# Week 1 (wild card)
w1 = 2 if r[0] == "H" else 7
w2 = 3 if r[1] == "H" else 6
w3 = 4 if r[2] == "H" else 5
winners = sorted((w1, w2, w3))
# Week 2 game 4: Team 1 hosts the third seeded winner (highest number)
t4_away = winners[2]
w4 = 1 if r[3] == "H" else t4_away
# Week 2 game 5: highest seeded winner hosts the second seeded winner
host5, away5 = sorted((winners[0], winners[1])) # smallest hosts
w5 = host5 if r[4] == "H" else away5
# Week 3: highest seeded winner hosts the other winner
host6, away6 = sorted((w4, w5))
w6 = host6 if r[5] == "H" else away6
return host6, away6, w6