Skip to content

Commit

Permalink
add solution location
Browse files Browse the repository at this point in the history
  • Loading branch information
SKTT1Ryze committed Oct 14, 2023
1 parent c68857e commit 563d18e
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 6 deletions.
6 changes: 6 additions & 0 deletions frontend/run_dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
cd ../runtime
cargo run
cp undeads.json ../frontend/
cd ../frontend
yarn dev
12 changes: 9 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ interface Item {
title: string;
description: string;
labels: string[];
solutions: string[];
}

// @ts-ignore
const items: Item[] = Undeads.sort((a, b) => a.id - b.id);

const SolutionUrlBase =
"https://github.com/SKTT1Ryze/Valhalla/blob/main/leetcode/src/solutions/";

function App() {
return (
<div className={styles.container}>
Expand Down Expand Up @@ -51,9 +55,11 @@ function App() {
<div className={styles.id}>{item.id}</div>
<div className={styles["problem-title"]}>{item.title}</div>
<div className={styles.solution}>
<a href="https://github.com/SKTT1Ryze/Valhalla">
<FileDoneOutlined />
</a>
{item.solutions.map((solution) => (
<a href={SolutionUrlBase + solution}>
<FileDoneOutlined />
</a>
))}
</div>
<div
className={classNames(styles["problem-difficulty"], {
Expand Down
13 changes: 13 additions & 0 deletions leetcode/src/solutions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ pub mod two_sum;
pub trait Solution: Send + Sync {
fn problem_id(&self) -> usize;
fn name(&self) -> String;
fn location(&self) -> String;
fn test(&self) -> Result<()>;
fn benchmark(&self) -> Result<usize>;
}

#[macro_export]
macro_rules! location {
() => {
return std::path::Path::new(file!())
.file_name()
.unwrap_or_default()
.to_str()
.unwrap_or_default()
.to_string()
};
}
4 changes: 3 additions & 1 deletion leetcode/src/solutions/add_two_numbers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::Solution;
use crate::list::{assert_eq_list, ListNode};

pub struct AddTwoNumbers;

impl Solution for AddTwoNumbers {
Expand All @@ -10,6 +9,9 @@ impl Solution for AddTwoNumbers {
fn problem_id(&self) -> usize {
2
}
fn location(&self) -> String {
crate::location!();
}
fn test(&self) -> anyhow::Result<()> {
let testcases = [
(
Expand Down
6 changes: 6 additions & 0 deletions leetcode/src/solutions/longest_substring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ impl Solution for LongestSubstring {
fn problem_id(&self) -> usize {
3
}
fn location(&self) -> String {
crate::location!();
}
fn test(&self) -> anyhow::Result<()> {
for (input, expect) in TESTCASES {
let output = Self::length_of_longest_substring(input.to_string());
Expand Down Expand Up @@ -66,6 +69,9 @@ impl Solution for LongestSubstringHashMap {
fn problem_id(&self) -> usize {
3
}
fn location(&self) -> String {
crate::location!();
}
fn test(&self) -> anyhow::Result<()> {
for (input, expect) in TESTCASES {
let output = Self::length_of_longest_substring(input.to_string());
Expand Down
3 changes: 3 additions & 0 deletions leetcode/src/solutions/two_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ impl Solution for TwoSum {
fn problem_id(&self) -> usize {
1
}
fn location(&self) -> String {
crate::location!();
}
fn test(&self) -> anyhow::Result<()> {
let testcases = [
(vec![2, 7, 11, 15], 9, [0, 1]),
Expand Down
10 changes: 9 additions & 1 deletion runtime/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::{
sync::{Arc, Mutex},
};

use crate::serder::Undead;

mod container;
mod registration;
mod serder;
Expand Down Expand Up @@ -43,8 +45,14 @@ fn main() -> anyhow::Result<()> {
log::info!("Solution {} test passed!", solution.name());
// TODO: bencmark
}

// serde as json
undeads.push(problem.into());
let mut undead: Undead = problem.into();
undead.solutions = solutions
.into_iter()
.map(|solution| solution.location())
.collect();
undeads.push(undead);
}
}

Expand Down
4 changes: 3 additions & 1 deletion runtime/src/serder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use std::sync::Arc;
use leetcode::problems::Problem;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Default)]
pub struct Undead {
id: usize,
difficulty: String,
topic: String,
title: String,
description: String,
labels: Vec<String>,
pub solutions: Vec<String>,
}

impl From<Arc<dyn Problem>> for Undead {
Expand All @@ -22,6 +23,7 @@ impl From<Arc<dyn Problem>> for Undead {
title: value.title(),
description: value.description(),
labels: value.labels(),
..Default::default()
}
}
}

0 comments on commit 563d18e

Please sign in to comment.