Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit 60a8c69

Browse files
authored
Merge pull request #16 from ProgSoc/02-fuzzbuzz
FuzzBuzz
2 parents b2e5dba + 0278895 commit 60a8c69

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+243
-0
lines changed

competition/02-fuzzbuzz/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# dependencies (bun install)
2+
node_modules
3+
4+
# output
5+
out
6+
dist
7+
*.tgz
8+
9+
# code coverage
10+
coverage
11+
*.lcov
12+
13+
# logs
14+
logs
15+
_.log
16+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
17+
18+
# dotenv environment variable files
19+
.env
20+
.env.development.local
21+
.env.test.local
22+
.env.production.local
23+
.env.local
24+
25+
# caches
26+
.eslintcache
27+
.cache
28+
*.tsbuildinfo
29+
30+
# IntelliJ based IDEs
31+
.idea
32+
33+
# Finder (MacOS) folder config
34+
.DS_Store

competition/02-fuzzbuzz/bun.lock

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"lockfileVersion": 1,
3+
"workspaces": {
4+
"": {
5+
"name": "02-fizzbuzz",
6+
"dependencies": {
7+
"rand-seed": "^2.1.7",
8+
},
9+
"devDependencies": {
10+
"@types/bun": "latest",
11+
},
12+
"peerDependencies": {
13+
"typescript": "^5",
14+
},
15+
},
16+
},
17+
"packages": {
18+
"@types/bun": ["@types/[email protected]", "", { "dependencies": { "bun-types": "1.2.17" } }, "sha512-l/BYs/JYt+cXA/0+wUhulYJB6a6p//GTPiJ7nV+QHa8iiId4HZmnu/3J/SowP5g0rTiERY2kfGKXEK5Ehltx4Q=="],
19+
20+
"@types/node": ["@types/[email protected]", "", { "dependencies": { "undici-types": "~7.8.0" } }, "sha512-R4I/kzCYAdRLzfiCabn9hxWfbuHS573x+r0dJMkkzThEa7pbrcDWK+9zu3e7aBOouf+rQAciqPFMnxwr0aWgKg=="],
21+
22+
"bun-types": ["[email protected]", "", { "dependencies": { "@types/node": "*" } }, "sha512-ElC7ItwT3SCQwYZDYoAH+q6KT4Fxjl8DtZ6qDulUFBmXA8YB4xo+l54J9ZJN+k2pphfn9vk7kfubeSd5QfTVJQ=="],
23+
24+
"rand-seed": ["[email protected]", "", {}, "sha512-Yaz75D2fTWtIr69iDd+PGwtQkFkqOIMQZl+W8U3NMR6F2a1UEk2FmnQkNd6Z1eNtL96Z5nznw5PKYk1Z9m6lxw=="],
25+
26+
"typescript": ["[email protected]", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ=="],
27+
28+
"undici-types": ["[email protected]", "", {}, "sha512-9UJ2xGDvQ43tYyVMpuHlsgApydB8ZKfVYTsLDhXkFL/6gfkp+U8xTGdh8pMJv1SpZna0zxG1DwsKZsreLbXBxw=="],
29+
}
30+
}

competition/02-fuzzbuzz/index.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import Rand from "rand-seed";
2+
3+
// take 2 parameters, a mode and a string seed
4+
const mode = Bun.argv[2];
5+
const seed = Bun.argv[3];
6+
7+
if (!mode || !seed) {
8+
console.error("Usage: node index.js <mode> <seed>");
9+
process.exit(1);
10+
}
11+
12+
function generateRandomNumber(seed: string, digits: number): number {
13+
const rand = new Rand(seed);
14+
return Math.floor(rand.next() * Math.pow(10, digits));
15+
}
16+
17+
/**
18+
* Generates FuzzBuzz lines for numbers from 1 to the given number.
19+
* @param number The number to generate FuzzBuzz lines for
20+
* @returns An array of strings representing the FuzzBuzz output
21+
*/
22+
function fuzzBuzz(number: number): string[] {
23+
const fuzzBuzzLines: string[] = [];
24+
for (let num = 1; num <= number; num++) {
25+
if (num % 3 === 0 && num % 5 === 0) { // If number is divisible by both 3 and 5
26+
fuzzBuzzLines.push("FuzzBuzz");
27+
} else if (num % 3 === 0) { // If number is divisible by 3
28+
fuzzBuzzLines.push("Fuzz");
29+
} else if (num % 5 === 0) { // If number is divisible by 5
30+
fuzzBuzzLines.push("Buzz");
31+
} else { // If number is not divisible by 3 or 5
32+
fuzzBuzzLines.push(num.toString());
33+
}
34+
}
35+
return fuzzBuzzLines;
36+
}
37+
38+
async function getInputLines(): Promise<string[]> {
39+
const inputLines: string[] = [];
40+
for await (const line of console) {
41+
inputLines.push(line.trim());
42+
if (line.trim() === "") {
43+
break;
44+
}
45+
}
46+
return inputLines;
47+
}
48+
49+
switch (mode) {
50+
case "fuzz": {
51+
// Fuzz mode: generate random numbers and print them
52+
53+
const number = generateRandomNumber(seed, 3);
54+
console.log(number)
55+
56+
break;
57+
}
58+
case "judge": {
59+
// Judge mode, create solution based on same seed
60+
const number = generateRandomNumber(seed, 3);
61+
62+
const fuzzBuzzLines = fuzzBuzz(number);
63+
64+
const inputLines = await getInputLines();
65+
// Compare the input lines with the fuzzBuzzLines
66+
if (inputLines.length !== fuzzBuzzLines.length) {
67+
console.error("Input length does not match expected FuzzBuzz output length.");
68+
process.exit(1);
69+
}
70+
for (let i = 0; i < inputLines.length; i++) {
71+
if (inputLines[i] !== fuzzBuzzLines[i]) {
72+
console.error(`Mismatch at line ${i + 1}: expected "${fuzzBuzzLines[i]}", got "${inputLines[i]}"`);
73+
process.exit(1);
74+
}
75+
}
76+
console.log("All lines match the expected FuzzBuzz output.");
77+
process.exit(0);
78+
}
79+
case "solution": {
80+
// Solution mode: generate a solution based on the seed
81+
const number = generateRandomNumber(seed, 3);
82+
const fuzzBuzzLines = fuzzBuzz(number);
83+
console.log(fuzzBuzzLines.join("\n"));
84+
process.exit(0);
85+
}
86+
default: {
87+
console.error("Invalid mode. Use 'fuzz' or 'judge'.");
88+
process.exit(1);
89+
}
90+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "02-fuzzbuzz",
3+
"module": "index.ts",
4+
"type": "module",
5+
"private": true,
6+
"devDependencies": {
7+
"@types/bun": "latest"
8+
},
9+
"peerDependencies": {
10+
"typescript": "^5"
11+
},
12+
"dependencies": {
13+
"rand-seed": "^2.1.7"
14+
}
15+
}

competition/02-fuzzbuzz/prob.md

Lines changed: 34 additions & 0 deletions

competition/02-fuzzbuzz/run.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
# Run bun install and pipe output to /dev/null
4+
bun install --silent > /dev/null 2>&1
5+
# Check if the bun install command was successful
6+
if [ $? -ne 0 ]; then
7+
echo "bun install failed. Please check your environment."
8+
exit 1
9+
fi
10+
# Run the main script with the provided arguments
11+
bun run index.ts $1 $2
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compilerOptions": {
3+
// Environment setup & latest features
4+
"lib": ["ESNext"],
5+
"target": "ESNext",
6+
"module": "Preserve",
7+
"moduleDetection": "force",
8+
"jsx": "react-jsx",
9+
"allowJs": true,
10+
11+
// Bundler mode
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"verbatimModuleSyntax": true,
15+
"noEmit": true,
16+
17+
// Best practices
18+
"strict": true,
19+
"skipLibCheck": true,
20+
"noFallthroughCasesInSwitch": true,
21+
"noUncheckedIndexedAccess": true,
22+
"noImplicitOverride": true,
23+
24+
// Some stricter flags (disabled by default)
25+
"noUnusedLocals": false,
26+
"noUnusedParameters": false,
27+
"noPropertyAccessFromIndexSignature": false
28+
}
29+
}

0 commit comments

Comments
 (0)