Skip to content

Commit

Permalink
add day 16 and 17
Browse files Browse the repository at this point in the history
  • Loading branch information
zyhou committed Dec 18, 2023
1 parent c8d42a0 commit 57a305f
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
68 changes: 68 additions & 0 deletions 16/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Expect, Equal } from "type-testing";

type StringToNumber<T> = T extends `${infer N extends number}` ? N : never;

type FindSanta<Forest extends unknown[][]> = {
[Row in keyof Forest]: {
[Column in keyof Forest[Row]]: Forest[Row][Column] extends "πŸŽ…πŸΌ"
? [StringToNumber<Row>, StringToNumber<Column>]
: never;
}[keyof Forest[Row]];
}[number];

// ------------------- Test section ---------------------

type Forest0 = [
["πŸŽ…πŸΌ", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
];
type test_0_actual = FindSanta<Forest0>;
// ^?
type test_0_expected = [0, 0];
type test_0 = Expect<Equal<test_0_expected, test_0_actual>>;

type Forest1 = [
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ…πŸΌ", "πŸŽ„", "πŸŽ„"],
];
type test_1_actual = FindSanta<Forest1>;
// ^?
type test_1_expected = [3, 1];
type test_1 = Expect<Equal<test_1_expected, test_1_actual>>;

type Forest2 = [
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ…πŸΌ", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
];
type test_2_actual = FindSanta<Forest2>;
// ^?
type test_2_expected = [2, 2];
type test_2 = Expect<Equal<test_2_expected, test_2_actual>>;

type Forest3 = [
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ…πŸΌ", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
];
type test_3_actual = FindSanta<Forest3>;
// ^?
type test_3_expected = [2, 1];
type test_3 = Expect<Equal<test_3_expected, test_3_actual>>;

type Forest4 = [
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ…πŸΌ", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
["πŸŽ„", "πŸŽ„", "πŸŽ„", "πŸŽ„"],
];
type test_4_actual = FindSanta<Forest4>;
// ^?
type test_4_expected = [1, 2];
type test_4 = Expect<Equal<test_4_expected, test_4_actual>>;
60 changes: 60 additions & 0 deletions 17/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Expect, Equal } from "type-testing";

type RockPaperScissors = "πŸ‘ŠπŸ»" | "πŸ–πŸΎ" | "✌🏽";
type RockPaperScissorsWin = ["πŸ‘ŠπŸ»", "πŸ–πŸΎ"] | ["πŸ–πŸΎ", "✌🏽"] | ["✌🏽", "πŸ‘ŠπŸ»"];

type WhoWins<
Player1 extends RockPaperScissors,
Player2 extends RockPaperScissors,
> = Player1 extends Player2
? "draw"
: [Player1, Player2] extends RockPaperScissorsWin
? "win"
: "lose";

// ------------------- Test section ---------------------

type test_0_actual = WhoWins<"πŸ‘ŠπŸ»", "πŸ–πŸΎ">;
// ^?
type test_0_expected = "win";
type test_0 = Expect<Equal<test_0_expected, test_0_actual>>;

type test_1_actual = WhoWins<"πŸ‘ŠπŸ»", "✌🏽">;
// ^?
type test_1_expected = "lose";
type test_1 = Expect<Equal<test_1_expected, test_1_actual>>;

type test_2_actual = WhoWins<"πŸ‘ŠπŸ»", "πŸ‘ŠπŸ»">;
// ^?
type test_2_expected = "draw";
type test_2 = Expect<Equal<test_2_expected, test_2_actual>>;

type test_3_actual = WhoWins<"πŸ–πŸΎ", "πŸ‘ŠπŸ»">;
// ^?
type test_3_expected = "lose";
type test_3 = Expect<Equal<test_3_expected, test_3_actual>>;

type test_4_actual = WhoWins<"πŸ–πŸΎ", "✌🏽">;
// ^?
type test_4_expected = "win";
type test_4 = Expect<Equal<test_4_expected, test_4_actual>>;

type test_5_actual = WhoWins<"πŸ–πŸΎ", "πŸ–πŸΎ">;
// ^?
type test_5_expected = "draw";
type test_5 = Expect<Equal<test_5_expected, test_5_actual>>;

type test_6_actual = WhoWins<"✌🏽", "πŸ‘ŠπŸ»">;
// ^?
type test_6_expected = "win";
type test_6 = Expect<Equal<test_6_expected, test_6_actual>>;

type test_7_actual = WhoWins<"✌🏽", "✌🏽">;
// ^?
type test_7_expected = "draw";
type test_7 = Expect<Equal<test_7_expected, test_7_actual>>;

type test_8_actual = WhoWins<"✌🏽", "πŸ–πŸΎ">;
// ^?
type test_8_expected = "lose";
type test_8 = Expect<Equal<test_8_expected, test_8_actual>>;

0 comments on commit 57a305f

Please sign in to comment.