Skip to content

Commit

Permalink
refactor: merge origin challenges
Browse files Browse the repository at this point in the history
  • Loading branch information
BoonHianLim committed Jun 16, 2024
2 parents 62ba350 + c0cb953 commit db54636
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 110 deletions.
1 change: 0 additions & 1 deletion apps/challenges/src/config/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const connectDB = async () => {
useNewUrlParser: true,
dbName: process.env.CHALLENGES_MONGO_DATABSE_NAME || "challenges",
} as ConnectionOptions);

Logger.info(`[server]: MongoDB Connected: ${mongoURL}`);
} catch (error) {
let errorReason = "unknown error";
Expand Down
2 changes: 1 addition & 1 deletion apps/challenges/src/middleware/errorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ export const ErrorHandling = (err: unknown, res: Response) => {
default:
res.status(500).json({ message: "Internal Server Error" });
}
};
};
97 changes: 41 additions & 56 deletions apps/web/pages/challenges/problems/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,21 @@ import Link from "next/link";
import { useState, useEffect } from "react";
import { useRouter } from "next/router";

interface Season {
_id: string,
title: string,
start_date: Date,
end_date: Date
}

type ProblemListData = {
uuid: number;
uuid: string;
problem: string;
title: string;
point: number;
solved: number;
};

// dummy data for multiple season
const seasonsData: Record<string, ProblemListData[]> = {
season1: [
{
uuid: 1001,
problem: "A",
title: "longest substring",
point: 500,
solved: 10,
},
{
uuid: 1002,
problem: "B",
title: "3 sum",
point: 300,
solved: 15,
},
{
uuid: 1003,
problem: "C",
title: "minimum spanning tree",
point: 400,
solved: 30,
},
],

season2: [
{
uuid: 1004,
problem: "A",
title: "binary tree",
point: 200,
solved: 100,
},
{
uuid: 1005,
problem: "B",
title: "panlindrome",
point: 100,
solved: 1500,
},
],
};

const columnHelper = createColumnHelper<ProblemListData>();

const columns = [
Expand All @@ -68,7 +31,7 @@ const columns = [
columnHelper.accessor("title", {
cell: (prop) => (
<span>
<Link href={"/challenges/problems/submission"}>{prop.getValue()}</Link>
<Link href={`/challenges/problems/submission?id=${prop.row.original.uuid}`}>{prop.getValue()}</Link>
</span>
),
header: "Title",
Expand All @@ -86,19 +49,42 @@ const columns = [
const Problems = () => {
const [option, setOption] = useState("");
const router = useRouter();
const [data, setData] = useState<ProblemListData[]>([])
const [seasons, setSeasons] = useState<Season[]>([])

function updateSeasonProblems(seasonID: string) {
const url = `http://localhost:3000/api/seasons/${seasonID}/questions`
fetch(url)
.then((res: Response) => {
return res.json()})
.then((res: any) => {
let probs: ProblemListData[] = res.map((ele: any) => {
return {uuid: ele._id, problem: ele.question_no, title: ele.question_title, point: ele.points, solved: ele.correct_submissions_count } as ProblemListData}
)
setData(probs)
})

}

useEffect(() => {
const { season } = router.query;
if (season) setOption(season as string);
}, [router.query]);
fetch("http://localhost:3000/api/seasons/")
.then((res: Response) => {
return res.json()
})
.then((res: any) => {
let seasons: Season[] = res.seasons
setSeasons(seasons)
updateSeasonProblems(seasons[0]._id)
})

}, []);

const handleOptionChange = (event: { target: { value: string } }) => {
const selectedOption = event.target.value;
setOption(selectedOption);
router.push(`/challenges/problems?season=${String(selectedOption)}`);
updateSeasonProblems(selectedOption)
};

const selectedSeasonData = seasonsData[option] || [];

return (
<Flex
Expand All @@ -109,16 +95,15 @@ const Problems = () => {
alignItems="center"
>
<Select
placeholder="Select Season"
w="80vw"
my={4}
bg="#CBD5F0"
onChange={handleOptionChange}
value={option}
>
{Object.keys(seasonsData).map((season) => (
<option key={season} value={season}>
{season}
{seasons.map((season: Season) => (
<option key={season._id} value={season._id}>
{season.title}
</option>
))}
</Select>
Expand All @@ -131,7 +116,7 @@ const Problems = () => {
minHeight="70vh"
borderRadius="8"
>
<ProblemListTable columns={columns} data={selectedSeasonData} />
<ProblemListTable columns={columns} data={data} />
</Box>
</Flex>
);
Expand Down
84 changes: 32 additions & 52 deletions apps/web/pages/challenges/problems/submission/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ import {
ModalFooter,
useToast,
} from "@chakra-ui/react";
import { useState } from "react";
import { useEffect, useState } from "react";
import { useRouter } from "next/router";


type InputData = {
input: string;
};

interface Problem {
uuid: string;
title: string;
desc: string;
answer: string
}

const inputData: InputData = {
input: "2\n5\n30 40 20 20 100\n6\n1 2 3 4 5 6",
};
Expand All @@ -33,9 +42,10 @@ const Profile = () => {
const [errorMessage, setErrorMessage] = useState("");
const [isCorrect, setIsCorrect] = useState(false);
const [isInvalid, setIsInvalid] = useState(false);

const { isOpen, onOpen, onClose } = useDisclosure();

const [problem, setProblem] = useState<Problem>();

const router = useRouter()
const toast = useToast();

const handleUserInputChange = (event: { target: { value: string } }) => {
Expand All @@ -50,7 +60,7 @@ const Profile = () => {
} else if (isNaN(Number(userInput))) {
setErrorMessage("numbers only");
setIsInvalid(true);
} else if (userInput != 5) {
} else if (userInput != problem?.answer) {
// just a dummy hardcoded answer here
setErrorMessage("wrong answer, please try again");
setIsInvalid(true);
Expand Down Expand Up @@ -79,6 +89,22 @@ const Profile = () => {
});
};

useEffect(() => {
fetch(`http://localhost:3000/api/question/${router.query.id}`)
.then((res: Response) => {
return res.json()
})
.then((res: any) => {
const resProbem: Problem = {
uuid: res._uid,
title: res.question_title,
desc: res.question_desc,
answer: res.answer
}
setProblem(resProbem)
})
}, [])

return (
<Flex minH="100vh" flexDirection="row" justifyContent="space-around">
<Box
Expand All @@ -89,7 +115,7 @@ const Profile = () => {
mx={20}
flexDirection="column"
>
<Text>-- Cube Conundrum --</Text>
<Text>-- {problem?.title} --</Text>
<Button variant="link" color="#6AA8FA" onClick={onOpen}>
[ Puzzle Input ]
</Button>
Expand Down Expand Up @@ -117,53 +143,7 @@ const Profile = () => {
</ModalContent>
</Modal>
<Text>
You&apos;re launched high into the atmosphere! The apex of your
trajectory just barely reaches the surface of a large island floating
in the sky. You gently land in a fluffy pile of leaves. It&apos;s
quite cold, but you don&apos;t see much snow. An Elf runs over to
greet you. <br /> <br />
The Elf explains that you&apos;ve arrived at Snow Island and
apologizes for the lack of snow. He&apos;ll be happy to explain the
situation, but it&apos;s a bit of a walk, so you have some time. They
don&apos;t get many visitors up here; would you like to play a game in
the meantime?
<br /> <br />
As you walk, the Elf shows you a small bag and some cubes which are
either red, green, or blue. Each time you play this game, he will hide
a secret number of cubes of each color in the bag, and your goal is to
figure out information about the number of cubes. <br /> <br />
To get information, once a bag has been loaded with cubes, the Elf
will reach into the bag, grab a handful of random cubes, show them to
you, and then put them back in the bag. He&apos;ll do this a few times
per game. <br /> <br />
You play several games and record the information from each game (your
puzzle input). Each game is listed with its ID number (like the 11 in
Game 11: ...) followed by a semicolon-separated list of subsets of
cubes that were revealed from the bag (like 3 red, 5 green, 4 blue).
<br /> <br />
For example, the record of a few games might look like this: <br />
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green Game 2: 1 blue,
2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue Game 3: 8 green, 6
blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red Game 4: 1 green,
3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red Game 5: 6 red,
1 blue, 3 green; 2 blue, 1 red, 2 green <br /> <br />
In game 1, three sets of cubes are revealed from the bag (and then put
back again). The first set is 3 blue cubes and 4 red cubes; the second
set is 1 red cube, 2 green cubes, and 6 blue cubes; the third set is
only 2 green cubes. <br /> <br />
The Elf would first like to know which games would have been possible
if the bag contained only 12 red cubes, 13 green cubes, and 14 blue
cubes? <br /> <br />
In the example above, games 1, 2, and 5 would have been possible if
the bag had been loaded with that configuration. However, game 3 would
have been impossible because at one point the Elf showed you 20 red
cubes at once; similarly, game 4 would also have been impossible
because the Elf showed you 15 blue cubes at once. If you add up the
IDs of the games that would have been possible, you get 8. <br />
<br />
Determine which games would have been possible if the bag had been
loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. What
is the sum of the IDs of those games? <br /> <br />
{problem?.desc}
</Text>
</Box>
<Box w="40vw" px={4} my={40} mx={10} flexDirection="column">
Expand Down

0 comments on commit db54636

Please sign in to comment.