Skip to content

Commit

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

const { AdventPlaygroundOfTheDay, FormOfTheDay } = buildAdventOfTheDay({
day: 10,
buildBuggyAdvent: adventBuggy,
referenceAdvent: isProbablyEnchantedWordV2,
parser,
placeholderForm: 'any set of characters as long as it fits on one line',
functionName: 'isProbablyEnchantedWordV2',
signature: 'isProbablyEnchantedWordV2(word: string): string;',
signatureExtras: [],
});

export { AdventPlaygroundOfTheDay, FormOfTheDay };

// Reference implementation

function isProbablyEnchantedWordV2(word: string): boolean {
const segmenter = new Intl.Segmenter();
return (
[...segmenter.segment(word)]
.map((chunk) => chunk.segment)
.reverse()
.join('') === word
);
}

// Inputs parser

function parser(answer: string): unknown[] | undefined {
const lines = answer.trim().split('\n');
if (lines.length < 1) {
throw new Error(`Your answer should be made of one line`);
}
return [lines[0]];
}
11 changes: 11 additions & 0 deletions website/blog/2024-12-10-advent-of-pbt-day-10/buggy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-check

export default function advent() {
/**
* @param {string} word
* @returns {boolean}
*/
return function isProbablyEnchantedWordV2(word) {
return word.split('').reverse().join('') === word;
};
}
41 changes: 41 additions & 0 deletions website/blog/2024-12-10-advent-of-pbt-day-10/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: Advent of PBT 2024 · Day 10
authors: [dubzzz]
tags: [advent-of-pbt, advent-of-pbt-2024]
---

import {AdventPlaygroundOfTheDay,FormOfTheDay} from './AdventOfTheDay';

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 pinged you with your next challenge: after the issue you uncovered yesterday, the elves rewrote their algorithm to verify enchanted words. But Santa isn’t fully convinced the new version is flawless. Can you find inputs that expose any remaining flaws? 🎄🔧

<!--truncate-->

## Santa’s enchanted words

In Santa’s magical realm, enchanted words unlock special functionalities, like accessing secret archives or enabling high-speed sleigh mode. Thanks to your efforts yesterday, Santa discovered a critical bug in the elves’ first implementation. Alarmed, he immediately demanded a fix.

The elves have since rewritten their algorithm to verify whether a given word is an enchanted word. They admitted that the previous issue stemmed from an overly aggressive optimization aimed at speeding up the algorithm. Learning from their mistake, they’ve reverted to a simpler, more reliable implementation.

## Hands on

The elves are confident their updated solution is foolproof. But Santa isn’t so sure. He needs you to thoroughly test their work to ensure it holds up under scrutiny.

To assist you, Santa shared a few examples of valid enchanted words he use on regular basis:

- “⛄⭐⛄“
- “noon“
- “☀️🌙⭐🌙☀️🌙⭐🌙☀️“

Santa also reminded you that enchanted words can include any printable character as long as they fit on a single line.

Your mission? Identify a word that breaks the algorithm. Prove there’s a bug hiding in their logic. 🎄🔧

<AdventPlaygroundOfTheDay />

## Your answer

<FormOfTheDay />

0 comments on commit 76c1527

Please sign in to comment.