Readme

This is a great template if you want to solve the https://adventofcode.com puzzles in a TDD style:

  1. dump your puzzle input into the input function
  2. copy/paste sample input and output from the page
  3. once all assertions pass, the solution is calculated

There is one function for each star that can be achieved.

Some helpers for common tasks are defined at the end of code: debug, toInt, exctractNumbers, sum, ... ?

PS: Did you know advent of code goes as far back as 2015 and you can still solve those puzzles?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { expect } from "https://esm.town/v/karfau/chai";
expect(firstStar(`sample input`), "*1 sample 1").to.equal("expected output");
// expect(firstStar(``), "*1 sample 2").to.equal("?");
function firstStar(input: string) {
return "expected output"; // implement :)
}
// as as soon as the assertions pass, the solution is calculated
console.log("solution *1:", firstStar(input()));
expect(secondStar(``), "*2 sample 1").to.equal("?");
// expect(secondStar(``), "*2 sample 2").to.equal("?");
function secondStar(input: string) {
return "?";
}
console.log("solution *2:", secondStar(input()));
function input() {
return ``;
}
function toInt(s: string) {
return parseInt(s);
}
function extractNumbers(line: string) {
return line.match(/\d+/g).map(toInt);
}
// [0,1,2,3].reduce(sum, 0) => 6
function sum(sum: number, current: number) {
return sum + current;
}
// wrap a value to print and return it
function debug<T>(value: T, msg = "debug"): T {
console.log(msg, value);
return value;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
December 8, 2023