Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 Advent of PBT Day 9 #5500

Merged
merged 3 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions website/blog/2024-12-09-advent-of-pbt-day-9/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: 9,
buildBuggyAdvent: adventBuggy,
referenceAdvent: isProbablyEnchantedWord,
parser,
placeholderForm: 'any set of characters as long as it fits on one line',
functionName: 'isProbablyEnchantedWord',
signature: 'isProbablyEnchantedWord(word: string): string;',
dubzzz marked this conversation as resolved.
Show resolved Hide resolved
signatureExtras: [],
});

export { AdventPlaygroundOfTheDay, FormOfTheDay };

// Reference implementation

function isProbablyEnchantedWord(word: string): string {
dubzzz marked this conversation as resolved.
Show resolved Hide resolved
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]];
}
18 changes: 18 additions & 0 deletions website/blog/2024-12-09-advent-of-pbt-day-9/buggy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @ts-check

export default function advent() {
/**
* @param {string} word
* @returns {boolean}
*/
return function isProbablyEnchantedWord(word) {
const lastIndex = word.length - 1;
const lastScannedIndex = Math.floor(lastIndex / 2);
for (let i = 0; i < lastScannedIndex; ++i) {
if (word[i] !== word[lastIndex - i]) {
return false;
}
}
return true;
};
}
39 changes: 39 additions & 0 deletions website/blog/2024-12-09-advent-of-pbt-day-9/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: Advent of PBT 2024 · Day 9
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: the elves’ algorithm to verify enchanted words might not handle all cases correctly. Can you find inputs that break it? 🎄🔧

<!--truncate-->

## Santa’s enchanted words

In Santa’s magical realm, certain enchanted words — known as Magic Palindromes — are used to unlock secret functions, such as accessing special gift archives, enabling the sleigh’s high-speed mode, or even activating Christmas spells. These palindromes are unique because their symmetrical structure makes them immune to tampering or corruption during transmission.

However, with the growing number of such magical commands and increasing complexities, Santa realized that manually checking whether a word is a palindrome has become error-prone. To avoid mistakes that could disrupt Christmas preparations, he tasked the elves with creating an algorithm to automatically verify whether a given word is a palindrome.

Here’s how [Wikipedia defines a palindrome](https://en.wikipedia.org/wiki/Palindrome):

> A palindrome is a word [...] that reads the same backwards as forwards

For example, “noon” or “racecar” are palindromes, but “santa” or “christmas” are not.

## Hands on

The stakes are high: if the algorithm fails, Santa might use a corrupted or invalid magic word, causing sleigh malfunctions, misrouted presents, or even delayed deliveries. And here’s a quick note Santa shared before leaving you in front of the playground: words can contain any printable characters, as long as they fit on a single line.

Your mission? Find any edge cases where it might fail and report them to Santa before it’s too late. 🎄🔧

<AdventPlaygroundOfTheDay />

## Your answer

<FormOfTheDay />
Loading