Skip to content

Commit

Permalink
📝 Advent of PBT, Day 22 (#5552)
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz authored Dec 22, 2024
1 parent 75a539a commit b394202
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
62 changes: 62 additions & 0 deletions website/blog/2024-12-22-advent-of-pbt-day-22/AdventOfTheDay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import adventBuggy from './buggy.mjs';
import { buildAdventOfTheDay } from '../2024-12-01-advent-of-pbt-day-1/AdventOfTheDayBuilder';

const { AdventPlaygroundOfTheDay, FormOfTheDay } = buildAdventOfTheDay({
day: 22,
buildBuggyAdvent: adventBuggy,
referenceAdvent: computeSantaMindScore,
parser,
placeholderForm: '🎄🎁⛄🎈🎅\n🎁🎄⛄🎄🦌',
functionName: 'computeSantaMindScore',
signature:
'findOptimalPacking(secretSequence: Sequence, guessedSequence: Sequence): { goodPlacement: number; misplaced: number };',
signatureExtras: [
"type Icon = '🎄' | '🦌' | '⛄' | '🛷' | '🎈' | '🎀' | '🎅' | '🎁';",
'type Sequence = [Icon, Icon, Icon, Icon, Icon];',
],
});

export { AdventPlaygroundOfTheDay, FormOfTheDay };

// Reference implementation

type Icon = '🎄' | '🦌' | '⛄' | '🛷' | '🎈' | '🎀' | '🎅' | '🎁';
type Sequence = [Icon, Icon, Icon, Icon, Icon];

function computeSantaMindScore(
secretSequence: Sequence,
guessedSequence: Sequence,
): { goodPlacement: number; misplaced: number } {
const badlyPlacedInSecret = secretSequence.filter((item, index) => item !== guessedSequence[index]);
const badlyPlacedInGuessed = guessedSequence.filter((item, index) => item !== secretSequence[index]);
const goodPlacement = 5 - badlyPlacedInSecret.length;
let misplaced = 0;
for (const item of badlyPlacedInGuessed) {
const indexInSecret = badlyPlacedInSecret.indexOf(item);
if (indexInSecret !== -1) {
++misplaced;
badlyPlacedInSecret.splice(indexInSecret, 1);
}
}
return { goodPlacement, misplaced };
}

// Inputs parser

const sequenceRegex = /^[\u{1f384}\u{1f98c}\u{26c4}\u{1f6f7}\u{1f388}\u{1f380}\u{1f385}\u{1f381}]{5}$/u;

function parser(answer: string): unknown[] | undefined {
const lines = answer.split('\n');
if (lines.length !== 2) {
throw new Error('Expected two lines, each of them made of one sequence of 5 icons');
}
const secret = sequenceRegex.exec(lines[0]);
if (secret === null) {
throw new Error(`Expected one sequence of 5 icons for the secret. Received: ${lines[0]}.`);
}
const guessed = sequenceRegex.exec(lines[1]);
if (guessed === null) {
throw new Error(`Expected one sequence of 5 icons for the guess. Received: ${lines[1]}.`);
}
return [[...secret[0]], [...guessed[0]]];
}
30 changes: 30 additions & 0 deletions website/blog/2024-12-22-advent-of-pbt-day-22/buggy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// @ts-check

export default function advent() {
/** @typedef {"\u{1f384}"|"\u{1f98c}"|"\u{26c4}"|"\u{1f6f7}"|"\u{1f388}"|"\u{1f380}"|"\u{1f385}"|"\u{1f381}"} Icon */
/** @typedef {[Icon, Icon, Icon, Icon, Icon]} Sequence */

/**
* @param {Sequence} secretSequence
* @param {Sequence} guessedSequence
* @returns {{goodPlacement:number; misplaced: number}}
*/
return function computeSantaMindScore(secretSequence, guessedSequence) {
let goodPlacement = 0;
let misplaced = 0;
const copiedSecretSequence = [...secretSequence];
for (let index = 0; index !== guessedSequence.length; ++index) {
const item = guessedSequence[index];
const indexInSecret = secretSequence.indexOf(item);
const indexInCopiedSecret = copiedSecretSequence.indexOf(item);
if (index === indexInSecret) {
++goodPlacement;
copiedSecretSequence.splice(indexInCopiedSecret, 1);
} else if (indexInCopiedSecret !== -1) {
++misplaced;
copiedSecretSequence.splice(indexInCopiedSecret, 1);
}
}
return { goodPlacement, misplaced };
};
}
60 changes: 60 additions & 0 deletions website/blog/2024-12-22-advent-of-pbt-day-22/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: Advent of PBT 2024 · Day 22
authors: [dubzzz]
tags: [advent-of-pbt, advent-of-pbt-2024]
image: ./social.png
---

import {AdventPlaygroundOfTheDay,FormOfTheDay} from './AdventOfTheDay';
import BlueskyComments from '../2024-12-01-advent-of-pbt-day-1/BlueskyComments';

Christmas is at risk! In their rush to meet tight deadlines, Santa’s elves accidentally introduced bugs into critical algorithms. If these issues aren’t discovered in time, Christmas could be delayed for everyone worldwide!

Your mission is to troubleshoot these black-box algorithms using the power of fast-check.

The clock is ticking! Santa just sent you a new challenge: his elves’ algorithm for SantaMind might have bugs. Can you uncover any issues and ensure every guess gets the perfect feedback? 🎄✨

<!--truncate-->

## SantaMind

Santa has reinvented a classic game, filling it with the joy of Christmas! Instead of the usual colors, the game now uses festive icons, bringing holiday cheer to every round. Here's how it works:

> **The Goal**
>
> Guess the secret sequence of icons chosen by your opponent (or the game). With icons from: 🎄, 🦌, ⛄, 🛷, 🎈, 🎀, 🎅, 🎁.
>
> **Gameplay**
>
> Players submit their guesses, attempting to match the secret sequence.
>
> After each guess, the game provides feedback:
>
> - Good Placements: Icons that are in the correct position in the sequence.
> - Misplaced Icons: Icons that are in the secret sequence but not in the correct position.
> Victory: You win by guessing the exact sequence within the allowed number of attempts.
Santa’s version introduces automated feedback to make the game smoother. Santa instructed his elves to create an algorithm that calculates the number of Good Placements and Misplaced Icons for any guess compared to the secret sequence.

For example:

If the secret sequence is [🎄, 🎁, ⛄, 🎈, 🎅] and the guess is [🎁, 🎄, ⛄, 🎄, 🦌], the feedback would be:

- 1 Good placement (⛄ is in the correct position).
- 2 Misplaced icons (🎄 and 🎁 are correct icons but in the wrong positions).

## Hands on

Santa believes the elves’ algorithm might contain a bug, and he’s counting on you to find it before it’s too late. Using property-based testing, can you identify an input that exposes a flaw in the implementation?

Christmas is at stake—debug fast and save the day! 🎅✨

<AdventPlaygroundOfTheDay />

## Your answer

<FormOfTheDay />

## Comments

<BlueskyComments url="" />
3 changes: 3 additions & 0 deletions website/blog/2024-12-22-advent-of-pbt-day-22/social.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b394202

Please sign in to comment.