-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
website/blog/2024-12-08-advent-of-pbt-day-8/AdventOfTheDay.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import adventBuggy from './buggy.mjs'; | ||
import { buildAdventOfTheDay } from '../2024-12-01-advent-of-pbt-day-1/AdventOfTheDayBuilder'; | ||
|
||
const { AdventPlaygroundOfTheDay, FormOfTheDay } = buildAdventOfTheDay({ | ||
day: 8, | ||
buildBuggyAdvent: adventBuggy, | ||
referenceAdvent: respace, | ||
parser, | ||
placeholderForm: 'messagewithoutanyspace\nmessage\nspace\nnothing\nempty\nwithout\nany', | ||
functionName: 'respace', | ||
signature: 'respace(spacelessMessage: string, words: string[]): string;', | ||
signatureExtras: [], | ||
}); | ||
|
||
export { AdventPlaygroundOfTheDay, FormOfTheDay }; | ||
|
||
// Reference implementation | ||
|
||
function respace(spacelessMessage: string, words: string[]): string { | ||
const match = respaceInternal(spacelessMessage, words, 0); | ||
if (match === undefined) { | ||
return spacelessMessage; | ||
} | ||
return match.join(' '); | ||
} | ||
|
||
function respaceInternal(spacelessMessage: string, words: string[], startIndex: number): string[] | undefined { | ||
if (startIndex === spacelessMessage.length) { | ||
return []; | ||
} | ||
for (const word of words) { | ||
if (spacelessMessage.startsWith(word, startIndex)) { | ||
const subMatch = respaceInternal(spacelessMessage, words, startIndex + word.length); | ||
if (subMatch !== undefined) { | ||
return [word, ...subMatch]; | ||
} | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
// Inputs parser | ||
|
||
const validMessageOrWord = /^[a-z]+$/; | ||
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 at least one line`); | ||
} | ||
if (lines.some((line) => !validMessageOrWord.test(line))) { | ||
throw new Error(`The message and the words possibly making it should only be made of characters in a-z`); | ||
} | ||
return [lines[0], lines.slice(1)]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// @ts-check | ||
|
||
export default function advent() { | ||
/** | ||
* @param {string} spacelessMessage | ||
* @param {string[]} words | ||
* @returns {string} | ||
*/ | ||
return function respace(spacelessMessage, words) { | ||
const match = respaceInternal(spacelessMessage, words, 0); | ||
if (match === undefined) { | ||
return spacelessMessage; | ||
} | ||
return match.join(' '); | ||
}; | ||
|
||
function respaceInternal(spacelessMessage, words, startIndex) { | ||
if (startIndex === spacelessMessage.length) { | ||
return []; | ||
} | ||
for (const word of words) { | ||
if (spacelessMessage.startsWith(word, startIndex)) { | ||
const subMatch = respaceInternal(spacelessMessage, words, startIndex + word.length); | ||
if (subMatch !== undefined) { | ||
return [word, ...subMatch]; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
} | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
--- | ||
title: Advent of PBT 2024 · Day 8 | ||
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 restore spaces in compressed messages might be flawed. Can you spot problematic inputs? 🎄🔧 | ||
|
||
<!--truncate--> | ||
|
||
## Uncompress old text messages | ||
|
||
Do you remember the days when SMS messages were limited in characters? Grammar and spelling had to be creatively adjusted just to make everything fit! Santa had the same problem. To overcome these constraints, he and his elves agreed to exchange messages without using spaces, saving valuable storage and transport capacity. | ||
|
||
Now, with improved storage and communication technologies, Santa wants to restore these messages to their original, readable form by reintroducing the spaces. Recognizing the sheer volume of work this would require, he tasked his elves with developing an algorithm to automate the process. | ||
|
||
The algorithm takes two inputs: | ||
|
||
- A compressed message, which is a single string of concatenated words without spaces. | ||
- An array of valid words (all lowercase letters from `a` to `z`, and each word has at least one letter). | ||
|
||
If there’s a valid way to reconstruct the original message using the dictionary, the algorithm should return the decoded message with spaces. However, because the dictionary might be incomplete, if no valid reconstruction exists, the algorithm should simply return the original compressed message. | ||
|
||
## Hands on | ||
|
||
The elves are confident they’ve built a solid solution. But Santa, as always, has doubts. He wants you to put their implementation to the test. | ||
|
||
Your mission? Identify an input combination — a word list and a compressed message — that breaks the algorithm. Prove there’s a bug hiding in their logic. | ||
|
||
Christmas depends on restoring these messages correctly — don’t let Santa down! 🎄🔧 | ||
|
||
<AdventPlaygroundOfTheDay /> | ||
|
||
## Your answer | ||
|
||
<FormOfTheDay /> |