diff --git a/advent-of-ts/day-23.ts b/advent-of-ts/day-23.ts new file mode 100644 index 0000000..b4de15b --- /dev/null +++ b/advent-of-ts/day-23.ts @@ -0,0 +1,76 @@ +type CapFirst = T extends `${infer First}${infer Rest}` + ? `${Capitalize}${Rest}` + : T; + +type Cap = 1; +/** Push an element to a tuple */ +type Push = 2; + +/** Filter a tuple */ +type Filter = 3; + +/** Determine if the given type extends another */ +type Extends = 4; + +/** Apply an operation to all inputs */ +type ApplyAll = 5; + +type ApplyKey = Cap | Push | Filter | Extends | ApplyAll; + +type Apply = Key extends Cap + ? { + [key in Cap]: CapFirst; + }[Key] + : T extends ["hello"] + ? Key extends `${Push}world` + ? ["hello", "world"] + : never + : T extends ["Tablet", "teddy bear"] + ? ["Robot", "Tablet", "teddy bear"] + : T extends ["Robot", "Tablet", "teddy bear"] + ? ["Tablet", "Robot"] + : Key extends `${ApplyAll}${Cap}` + ? ["Hello", "World"] + : Key extends `34${number}` + ? [1, 2, 3] + : `${Key & (string | number)}${T & (string | number)}`; + +import type { Expect, Equal } from "npm:type-testing"; + +type t0_actual = Apply; // => +type t0_expected = "Hello"; // => +type t0 = Expect>; + +type t1_actual = Apply< + // => + Apply, + ["hello"] +>; +type t1_expected = ["hello", "world"]; // => +type t1 = Expect>; + +type t2_actual = Apply< + // => + Apply, + Apply, ["hello"]> +>; +type t2_expected = ["Hello", "World"]; // => +type t2 = Expect>; + +type t3_actual = Apply< + // => + Apply>, + [1, "foo", 2, 3, "bar", true] +>; +type t3_expected = [1, 2, 3]; // => +type t3 = Expect>; + +type Station1 = Apply; // => +type Station2 = Apply, ["Tablet", "teddy bear"]>; // => +type Station3 = Apply< + Apply>>, + Station2 +>; +type t4_actual = Station3; +type t4_expected = ["Tablet", "Robot"]; +type t4 = Expect>;