-
-
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
146 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
website/blog/2024-12-07-advent-of-pbt-day-7/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: 7, | ||
buildBuggyAdvent: adventBuggy, | ||
referenceAdvent: simplifyLocation, | ||
parser, | ||
placeholderForm: '/123//456/789', | ||
functionName: 'simplifyLocation', | ||
signature: 'function simplifyLocation(sourceLocation: string): string;', | ||
signatureExtras: [], | ||
}); | ||
|
||
export { AdventPlaygroundOfTheDay, FormOfTheDay }; | ||
|
||
// Reference implementation | ||
|
||
function simplifyLocation(sourceLocation: string): string { | ||
const components = sourceLocation.split('/'); | ||
const stack = []; | ||
for (const component of components) { | ||
if (component !== '.' && component !== '') { | ||
if (component === '..') { | ||
if (stack.length === 0) { | ||
return sourceLocation; | ||
} | ||
stack.pop(); | ||
} else { | ||
stack.push(component); | ||
} | ||
} | ||
} | ||
if (stack.length === 0) { | ||
return sourceLocation; | ||
} | ||
return '/' + stack.join('/'); | ||
} | ||
|
||
// Inputs parser | ||
|
||
const validLocationRegex = /^\/\d+(\/\d+|\/|\/\.|\/\.\.)*$/; | ||
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`); | ||
} | ||
if (lines[0] !== '✉️' && !validLocationRegex.test(lines[0])) { | ||
throw new Error( | ||
'Invalid location provided, we expect the beginning of the path to be /<number> then you can put /., /.. and // wherever you want', | ||
); | ||
} | ||
return [lines[0]]; | ||
} |
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 @@ | ||
// @ts-check | ||
|
||
export default function advent() { | ||
/** | ||
* @param {string} sourceLocation | ||
* @returns {string} | ||
*/ | ||
return function simplifyLocation(sourceLocation) { | ||
const components = sourceLocation.replace(/\/$/, '').split('/'); | ||
|
||
let numMovesHigher = 0; | ||
const stack = []; | ||
for (const component of components) { | ||
if (component === '.' || component === '') { | ||
if (numMovesHigher > stack.length) { | ||
return sourceLocation; | ||
} | ||
for (let i = 0; i !== numMovesHigher; ++i) { | ||
stack.pop(); | ||
} | ||
} else if (component === '..') { | ||
++numMovesHigher; | ||
} else { | ||
if (numMovesHigher > stack.length) { | ||
return sourceLocation; | ||
} | ||
for (let i = 0; i !== numMovesHigher; ++i) { | ||
stack.pop(); | ||
} | ||
numMovesHigher = 0; | ||
stack.push(component); | ||
} | ||
} | ||
if (numMovesHigher >= stack.length) { | ||
return sourceLocation; | ||
} | ||
for (let i = 0; i !== numMovesHigher; ++i) { | ||
stack.pop(); | ||
} | ||
return '/' + stack.join('/'); | ||
}; | ||
} |
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,50 @@ | ||
--- | ||
title: Advent of PBT 2024 · Day 7 | ||
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 have developed an algorithm to simplify the paths to items stored in Santa’s massive network of nested boxes. Santa fears the algorithm may have flaws, potentially making critical items impossible to find. Can you uncover any issues to ensure the system works perfectly? 🎄🔧 | ||
|
||
<!--truncate--> | ||
|
||
## Inventory locator | ||
|
||
Finding specific items in Santa’s workshop has become like searching for a needle in a haystack. To solve this problem centuries ago, Santa introduced an ingenious system for assigning every item a unique location—a concept so advanced it resembles the modern idea of file paths. Each item's location is a path through a vast network of nested boxes. | ||
|
||
For example, the “Sticker of fast-check” might be stored in box 123, which is inside box 58, which itself is inside box 159. Santa’s paths always begin with a slash `/` followed by a sequence of box names—positive integers or zero—separated by slashes. This ensures that every item can be pinpointed quickly, no matter how deeply nested it is. | ||
|
||
However, over time, moving items around in the tree of boxes has introduced clutter into these paths making some messy: | ||
|
||
- `/123/` could be simplified into `/123`. | ||
- `/123///456` could be simplified into `/123/456`. | ||
- `/123/./././456` could be simplified into `/123/456`. | ||
- `/123/456/../789` could be simplified into `/123/789`. | ||
- ... | ||
|
||
To make the system manageable again, Santa asked his elves to clean up the paths using these rules: | ||
|
||
- Paths must not end with a slash. | ||
- No two or more consecutive slashes are allowed. | ||
- `.` and `..` boxes must be resolved or removed entirely. | ||
- Corrupted paths must not be modified. | ||
|
||
## Hands on | ||
|
||
The elves, realizing how critical this task is, prioritized creating an algorithm to simplify paths. However, Santa, busy with his Christmas preparations, is concerned that the implementation might have bugs. | ||
|
||
Your mission is to test the algorithm using property based capabilities and uncover any paths that are improperly simplified. Such errors could cause chaos during Christmas, as Santa and his elves won’t be able to find key items in a timely manner. | ||
|
||
The stakes are high — can you ensure the system is foolproof? 🎄🔧 | ||
|
||
<AdventPlaygroundOfTheDay /> | ||
|
||
## Your answer | ||
|
||
<FormOfTheDay /> |