Skip to content

Commit

Permalink
feat: solved day 18 type challenge
Browse files Browse the repository at this point in the history
Signed-off-by: rajput-hemant <rajput.hemant2001@gmail.com>
  • Loading branch information
rajput-hemant committed Dec 18, 2023
1 parent 47c26ca commit c3ac00e
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions advent-of-typescript/2023/day_18.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { Equal, Expect } from "type-testing";

import { Increment } from "@/utils/type-helpers";

/* -----------------------------------------------------------------------------------------------
* Code here
* -----------------------------------------------------------------------------------------------*/

type Count<T, E, C extends any[] = []> = T extends [infer S, ...infer R]
? S extends E // if the current element is the element we're counting
? Count<R, E, Increment<C>> // increment the count and recurse
: Count<R, E, C> // otherwise, recurse for the rest of the array without incrementing
: C["length"]; // if we've reached the end of the array, return the count

/* -----------------------------------------------------------------------------------------------
* Do not edit below this line
* -----------------------------------------------------------------------------------------------*/

type ToySack = [
"🎸",
"🎧",
"👟",
"👟",
"💻",
"🪀",
"🧩",
"🎮",
"🎨",
"🕹️",
"📱",
"🧩",
"🧸",
"🎧",
"👟",
"🚲",
"📚",
"⌚",
"🎨",
"👟",
"🎸",
"🧸",
"👟",
"🎸",
"📱",
"🎧",
"🎮",
"🎒",
"📱",
"🧩",
"🧩",
"🚲",
"🕹️",
"🧵",
"📱",
"🕹️",
"🕰️",
"🧢",
"🕹️",
"👟",
"🧸",
"📚",
"🧁",
"🧩",
"🎸",
"🎮",
"🧁",
"📚",
"💻",
"⌚",
"🛹",
"🧁",
"🧣",
"🪁",
"🎸",
"🧸",
"🧸",
"🧸",
"🧩",
"🪁",
"🏎️",
"🏎️",
"🧁",
"📚",
"🧸",
"🕶️",
"💻",
"⌚",
"⌚",
"🕶️",
"🎧",
"🎧",
"🎧",
"💻",
"👟",
"🎸",
"💻",
"🪐",
"📚",
"🎨",
"📱",
"🎧",
"📱",
"🎸",
"🏎️",
"👟",
"🚲",
"📱",
"🚲",
"🎸",
];

type test_0_actual = Count<ToySack, "👟">;
// ^?
type test_0_expected = 8;
type test_0 = Expect<Equal<test_0_expected, test_0_actual>>;

type test_1_actual = Count<ToySack, "🧦">;
// ^?
type test_1_expected = 0;
type test_1 = Expect<Equal<test_1_expected, test_1_actual>>;

type test_2_actual = Count<ToySack, "🧩">;
// ^?
type test_2_expected = 6;
type test_2 = Expect<Equal<test_2_expected, test_2_actual>>;

type test_3_actual = Count<ToySack, "🛹">;
// ^?
type test_3_expected = 1;
type test_3 = Expect<Equal<test_3_expected, test_3_actual>>;

type test_4_actual = Count<ToySack, "🏎️">;
// ^?
type test_4_expected = 3;
type test_4 = Expect<Equal<test_4_expected, test_4_actual>>;

type test_5_actual = Count<ToySack, "📚">;
// ^?
type test_5_expected = 5;
type test_5 = Expect<Equal<test_5_expected, test_5_actual>>;

0 comments on commit c3ac00e

Please sign in to comment.