-
-
Notifications
You must be signed in to change notification settings - Fork 184
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
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
website/blog/2024-12-20-advent-of-pbt-day-20/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,70 @@ | ||
import adventBuggy from './buggy.mjs'; | ||
import { buildAdventOfTheDay } from '../2024-12-01-advent-of-pbt-day-1/AdventOfTheDayBuilder'; | ||
|
||
const { AdventPlaygroundOfTheDay, FormOfTheDay } = buildAdventOfTheDay({ | ||
day: 20, | ||
buildBuggyAdvent: adventBuggy, | ||
referenceAdvent: findStartIndex, | ||
parser, | ||
placeholderForm: '1\n2\n3\n3\n9', | ||
functionName: 'findStartIndex', | ||
signature: 'findStartIndex(partlyShuffled: number[]): number;', | ||
signatureExtras: [], | ||
}); | ||
|
||
export { AdventPlaygroundOfTheDay, FormOfTheDay }; | ||
|
||
// Reference implementation | ||
|
||
function findStartIndex(partlyShuffled) { | ||
if (partlyShuffled.length === 0) { | ||
return -1; | ||
} | ||
for (let i = 0; i < partlyShuffled.length; ++i) { | ||
if (partlyShuffled[i - 1] > partlyShuffled[i]) { | ||
return i; | ||
} | ||
} | ||
if (partlyShuffled[partlyShuffled.length - 1] > partlyShuffled[0]) { | ||
return 0; | ||
} | ||
} | ||
|
||
// Inputs parser | ||
|
||
const presentRegex = /^(\d+)$/; | ||
|
||
function parser(answer: string): unknown[] | undefined { | ||
const lines = answer.split('\n'); | ||
const items: number[] = []; | ||
for (let i = 0; i < lines.length - 1; ++i) { | ||
const m = presentRegex.exec(lines[i]); | ||
if (m === null) { | ||
throw new Error(`All lines except must be of the form <item>. Received: ${lines[i]}.`); | ||
} | ||
const weight = Number(m[1]); | ||
if (!Number.isInteger(weight)) { | ||
throw new Error(`The value of items must be integer. Received: ${m[1]}.`); | ||
} | ||
items.push(weight); | ||
} | ||
if (items.length === 0) { | ||
throw new Error(`Must provide at least one item.`); | ||
} | ||
if (items.every((i) => i === items[0])) { | ||
throw new Error(`Must provide at least two distinct items.`); | ||
} | ||
let numJumps = 0; | ||
for (let i = 0; i < items.length; ++i) { | ||
if (items[i - 1] > items[i]) { | ||
numJumps += 1; | ||
} | ||
} | ||
if (items[items.length - 1] > items[0]) { | ||
numJumps += 1; | ||
} | ||
if (numJumps !== 1) { | ||
throw new Error(`Must be partially sorted.`); | ||
} | ||
return [items]; | ||
} |
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,39 @@ | ||
// @ts-check | ||
|
||
export default function advent() { | ||
/** | ||
* This solution has been provided to you by GPT-4o | ||
* @param {number[]} partlyShuffled | ||
* @returns {number} | ||
*/ | ||
return function findStartIndex(partlyShuffled) { | ||
let left = 0; | ||
let right = partlyShuffled.length - 1; | ||
|
||
// Handle the case where the array is not rotated | ||
if (partlyShuffled[left] <= partlyShuffled[right]) return 0; | ||
|
||
while (left <= right) { | ||
let mid = Math.floor((left + right) / 2); | ||
|
||
// Check if mid is the rotation point | ||
if (partlyShuffled[mid] > partlyShuffled[mid + 1]) { | ||
return mid + 1; | ||
} | ||
if (partlyShuffled[mid] < partlyShuffled[mid - 1]) { | ||
return mid; | ||
} | ||
|
||
// Decide which half to search next | ||
if (partlyShuffled[mid] >= partlyShuffled[left]) { | ||
// Rotation point is in the right half | ||
left = mid + 1; | ||
} else { | ||
// Rotation point is in the left half | ||
right = mid - 1; | ||
} | ||
} | ||
|
||
return -1; // This should never happen in a valid rotated array | ||
}; | ||
} |
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,49 @@ | ||
--- | ||
title: Advent of PBT 2024 · Day 20 | ||
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 pinged you with your next challenge: he tackled a tricky coding problem to boost his morale but suspects his solution might have flaws. Can you find a bug in his implementation and restore his confidence? 🎄✨ | ||
|
||
<!--truncate--> | ||
|
||
## Coding day | ||
|
||
After a hectic Christmas season, Santa decided to take a short break to unwind. This morning, he stumbled upon a coding challenge online and couldn’t resist giving it a try. | ||
|
||
The challenge goes like this: | ||
|
||
> Imagine a sorted list of integers, but someone has rotated it by taking a portion from the beginning and moving it to the end. Your task is to find the index of the original first item in the list. | ||
In other words: | ||
|
||
> You start with a list: `[i0, i1, ..., in]` where all items are sorted in ascending order (`i{index} <= i{index+1}`). | ||
> Then someone rearranges the list to look like this: `[im+1, im+2, ..., in, i0, i1, ..., im]`. | ||
> Your goal is to determine the index of `i0` in the modified list. | ||
## Hands on | ||
|
||
This time, Santa isn’t asking you to save Christmas — he just needs your help to boost his morale. He’s fairly confident about his solution to the coding challenge but has a sneaking suspicion there might be a bug. | ||
|
||
To impress himself further, Santa attempted to solve the problem with an optimized approach that avoids scanning through all the items in the list. However, he sheepishly admits that the solution wasn’t entirely his own — it’s based on suggestions from GPT-4o. While he trusts the AI’s results, his inexperience with such tools makes him cautious. | ||
|
||
Santa has already tested the solution thoroughly and hasn’t found any issues, but if a bug exists, it’s likely deeply hidden. You’ll need to let fast-check run for more than its default 100 runs to uncover it. Can you identify an input that breaks Santa’s implementation? 🎄✨ | ||
|
||
<AdventPlaygroundOfTheDay /> | ||
|
||
## Your answer | ||
|
||
<FormOfTheDay /> | ||
|
||
## Comments | ||
|
||
<BlueskyComments url="" /> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.