Table of contents
Open Table of contents
Running maelstrom test
To run maelstrom
test we can use the command
./maelstrom test -w echo --bin ~/echo-challenge/index.ts --node-count 1 --time-limit 10
But running this command right now will throw an error. This is because we have not set bang
for index.ts
to specify that this file has to run using Deno
and we have to set correct execution permission for ~/echo-challenge/index.
file too.
So first let’s set bang
for index.ts
file
#!/usr/bin/env -S deno run
import { readline } from "https://deno.land/x/readline@v1.1.0/mod.ts";
import z from "https://deno.land/x/zod@v3.21.4/index.ts";
Here is the whole code for ~/echo-challenge/index.ts
file.
#!/usr/bin/env -S deno run
import { readline } from "https://deno.land/x/readline@v1.1.0/mod.ts";
import z from "https://deno.land/x/zod@v3.21.4/index.ts";
const InitInputMessageSchema = z.object({
src: z.string(),
dest: z.string(),
body: z.object({
type: z.literal("init"),
msg_id: z.number(),
node_id: z.string(),
node_ids: z.array(z.string()),
}),
});
const EchoInputMessageSchema = z.object({
src: z.string(),
dest: z.string(),
body: z.object({
type: z.literal("echo"),
msg_id: z.number(),
echo: z.string(),
}),
});
const InputMessagesSchema = z.union([
InitInputMessageSchema,
EchoInputMessageSchema,
]);
const stdin = Deno.stdin;
for await (const encodedLine of readline(stdin)) {
const line = new TextDecoder().decode(encodedLine);
const inputMessage = InputMessagesSchema.parse(JSON.parse(line));
if (inputMessage.body.type === "init") {
console.log(
JSON.stringify({
src: inputMessage.dest,
dest: inputMessage.src,
body: { type: "init_ok", in_reply_to: inputMessage.body.msg_id },
})
);
} else if (inputMessage.body.type === "echo") {
console.log(
JSON.stringify({
src: inputMessage.dest,
dest: inputMessage.src,
body: {
type: "echo_ok",
echo: inputMessage.body.echo,
in_reply_to: inputMessage.body.msg_id,
},
})
);
}
}
Then to set execution permission for ~/echo-challenge/index.ts
. If you are using linux/mac-os
use this command. If you are using Windows
please refer internet to figure out how to do that because I have no idea how to do this in Windows
(I don’t even know whether it is needed to do this in Windows
or not)
chmod +x ~/echo-challenge/index.ts
Now if you execute the command
./maelstrom test -w echo --bin ~/echo-challenge/index.ts --node-count 1 --time-limit 10
You should see tons of logs and a last message which says
Everything looks good! ヽ(‘ー`)ノ
If you got this congrats you have solved echo challenge
successfully. If you are getting errors make sure you have followed all the steps properly and still it is not resolved write a comment or send me a message. I will help to the best of my abilities.