Skip to content

Commit 78003a4

Browse files
committed
Solve day 23 and presentation
1 parent c8606fc commit 78003a4

26 files changed

+284
-4
lines changed

1.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-1
2+
13
// Christmas Cookies
24
// It's December 1st! That means it's almost time for the big day! Santa has a preparation regimen that involves, of course, eating lots of delicious cookies.
35
// Santa's elves have provided Santa an API whereby Santa can submit his favorite cookie flavors. This year his favorites are:

10.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-10
2+
13
// Christmas Street Suffix Tester
24
// It's a little known fact that Santa's reindeer are orienteering experts. They're very particular, actually.
35
// To do this work well, they need to do some basic validation on the addresses. There were hopes among some reindeer to introduce a validation library this year, but there was simply too much infighting. It's kindof a mess. You see..

11.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-11
2+
13
// Protect the List
24
// [a conversation overhead two elves, Wunorse and Alabaster, at Santa's workshop on a monday morning after all the elves were forced to work all weekend]
35
// [Wurnose] The world will not know peace until every consultant involved in pushing SOC2 is dead and in the ground.

12.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-12
2+
13
// Find Santa
24
// Strange as it may sound.. Santa went to college with someone that works at a big silicon valley networking company. They've been buddies for years. So much so that in 2023 Santa pushed the workshop's board until they approved budget to get WiFi on the entire campus. That way Santa can browse TikTok as he walks from building to building across the campus.
35
// But after all that doomscrolling, Santa realized he has lost himself in a Christmas tree forest! A search team of elves has been deployed to find him, but he needs to give them more information about where he is among the trees.

13.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-13
2+
13
// Count the Days
24
// The elves are SPENT. They need some motivation. They are (literally) counting down the days until Christmas.
35
// side note on performance bonuses.. Santa promised that this year they'd get a bonus on the 26th (as well as an extra 2 PAID days off over the course of the next year!). Santa actually promised this last year (and the year before) but no one got a bonus because (according to Santa) "global warming has caused rising sea levels which in turn has eaten coastline, causing a need for many repairs at some of the high-density apartment complexes Santa owns in Florida, resulting in lower cashflow for the parent organization". That's what he said, anyway.

14.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-14
2+
13
// Naughty List Decipher
24
// [early on the morning of Thursday December 14th, Santa stumbles into office greeted by Bernard, the head elf..]
35
// [Bernard] YOU'RE A MESS. Were you out partying.. on a WEDNESDAY?? AGAIN??!!!

15.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-15
2+
13
// Box The Toys!
24
// [Santa walks by as Bernard, the head elf, is yelling at the other elves..]
35
// [Bernard (to his staff)] LET'S GO ELVES! LET'S GO! KEEP BOXING TOYS!

16.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-16
2+
13
// Find Santa (part 2)
24
// Since the episode with him getting lost on Tuesday (Day 12), the elves have started to get concerned about Santa getting lost again, but deeper in the forest. Since Santa's college buddy got WiFi installed in the whole property, Santa just wanders around scrolling TikTok without looking where he's going. Santa claimed that the reason the whole campus needed WiFi (even the forest) was to "future-proof the business" and "attract top talent" but it's beginning to seem like it was so he could personally get better phone service (cell reception in the north pole isn't great and without 116th H.R.7302, neither is the rural internet speed).
35
// Sure enough. It happened again. Santa got lost, again, but this time much deeper in the forest.

17.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-17
2+
13
// Rock, Paper, Scissors
24
// It's Sunday and there's one week to go before the big day (Christmas Eve) when the elfs' work for the year will finally be complete. For the last 20 years the only game the elves have had to play together is StarCraft. They're looking for a fresh game to play.
35
// So, they get the idea to try a Rock, Paper, Scissors tournament.

18.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-18
2+
13
// Santa's Remaining Deliveries
24
// Santa needs your help to count the number of presents he has to deliver! He's got all kinds of presents, from video game consoles (🎮), stuffed animals (🧸), toy cars (🏎️), books (📚), and more!
35
// We need a general purpose type that can take a tuple of items as its first arguemnt and an item to search for as the second argument. It should return a count of the item specified.

19.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-19
2+
13
// Help Santa Embezzle Funds
24
// The shady WiFi installment by Santa's college buddy in Days 12 and 16 aren't the only questionable business dealing Santa is involved in. Another of Santa's friends from college, Tod, is a partial owner of the X Games (an "extreme sports" version of the Olympics). In recent years, Santa realized that he can use his position of power at the toy factory to embezzle funds through a shell corporation that he started with Tod. The shell corporation, Icecap Assets Management, Inc., recently acquired a skateboard and scooter manufacturer, SkateScoot Syndicate. It's perfect timing because in 2022 Icecap had acquired another company that makes surfboards and bmx bikes, RideWave Dynamics.
35
// Now, all that's left to do is make sure that every child gets a skateboard or a scooter! Then the funds will be laundered to Icecap via SkateScoot and RideWave, after which Santa and Tod can then take total control of the funds.

2.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-2
2+
13
// Christmas Cookie Inventory
24
// Phew! Yesterday's tactic worked. Santa got down-and-dirty with the elves on the factory floor and they seem to have stopped planning their strike.
35
// With one small exception.. Unfortunately, two pesky elves (Jingle and Jangle) have realized that the 300 year stock options vesting cliff that Santa put into the elves' contract isn't quite typical. Jingle and Jangle already joined forces with Hermey (who has nothing to lose because he'd rather be a dentist than make toys) and they're beginning to cause a fuss.

20.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-20
2+
13
// TypeScript ASCII Art!
24
// Your goal for this challenge is to take an input like Hi and turn it into ASCII art!
35
// So for example Hi would turn into:

21.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-21
2+
13
// What is Tic Tac Toe?
24
// Tic-Tac-Toe is a two-player game where players alternate marking ❌s and ⭕s in a 3x3 grid, aiming to get three in a row.
35
// fun fact: Did you know that tic tac toe is widely considered to be the first computer video game ever created?! That's right! A S Douglas implemented it all the way back in 1952, the same year as the coronation of Queen Elizabeth II.

22.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-22
2+
13
// Reindeer Sudoku
24
// Santa's reindeer sure do like to cause trouble! This time they've decided to make a game out of arranging themselves into a Sudoku board.
35
// Before arranging themselves in this configuration, the reindeer left Santa a foreboding message:

23.ts

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-23
2+
3+
// Connect 4, but in TypeScript types
4+
// Your goal for this challenge is to implement Connect 4 in TypeScript types.
5+
// Each cell in the game can contain 🔴 or 🟡 or be empty ( ). You're provided with a rough layout of how to organize the board in the EmptyBoard type. The game state is represented by an object with a board property and a state property (which keeps track of which player is next up to play).
6+
// What is Connect 4
7+
// In case you haven't played it before: Connect 4 is a game in which the players choose a color and then take turns dropping colored tokens into a six-row, seven-column vertically suspended grid. The pieces fall straight down, occupying the lowest available space within the column. The objective of the game is to be the first to form a horizontal, vertical, or diagonal line of four of one's own tokens.
8+
// fun fact: Connect 4 is also known as Connect Four, Four Up, Plot Four, Find Four, Captain's Mistress, Four in a Row, Drop Four, and Gravitrips in the Soviet Union
9+
// another fun fact: Connect 4 was "solved" by James Allen and Victor Allis (independently from one another.. like two weeks apart!) in 1988. They couldn't do a full brute-force proof at the time, but 7 years later John Tromp in the Netherlands did it with a database on a Sun Microsystems and Silicon Graphics International worksations (for a combined total of 40,000 computation hours!).
10+
11+
type Connect4EmptyCell = " ";
12+
type Connect4Chips = "🔴" | "🟡";
13+
type Connect4Cell = Connect4Chips | Connect4EmptyCell;
14+
type Connect4State = "🔴" | "🟡" | "🔴 Won" | "🟡 Won" | "Draw";
15+
type Fill<
16+
Content extends unknown,
17+
Size extends number,
18+
Container extends Array<Content> = [],
19+
> = Container["length"] extends Size ? Container : Fill<Content, Size, [...Container, Content]>;
20+
// https://stackoverflow.com/a/62976061
21+
type LEQ = {
22+
0: 0;
23+
1: 0 | 1;
24+
2: 0 | 1 | 2;
25+
3: 0 | 1 | 2 | 3;
26+
4: 0 | 1 | 2 | 3 | 4;
27+
5: 0 | 1 | 2 | 3 | 4 | 5;
28+
6: 0 | 1 | 2 | 3 | 4 | 5 | 6;
29+
7: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7;
30+
};
31+
type Same<T, U, Y = T, N = never> = [T] extends [U] ? ([U] extends [T] ? Y : N) : N;
32+
type ToLEQ<N extends number> = {
33+
[K in keyof LEQ]: [N] extends [Exclude<LEQ[K], K>] ? never : K;
34+
}[keyof LEQ];
35+
type Max<N extends number, U extends number = ToLEQ<Extract<N, keyof LEQ>>> = {
36+
[K in keyof LEQ]: Same<U, LEQ[K], K>;
37+
}[keyof LEQ];
38+
// https://stackoverflow.com/a/62976061
39+
type Connect4RowIndex = 0 | 1 | 2 | 3 | 4 | 5;
40+
type Connect4ColumnIndex = 0 | 1 | 2 | 3 | 4 | 5 | 6;
41+
type Increment<N extends number> = [
42+
...Fill<"", N, []>,
43+
"",
44+
]["length"] extends infer IncrementedNumber extends number
45+
? IncrementedNumber
46+
: never;
47+
type Decrement<N extends number> = Fill<"", N, []> extends [infer First, ...infer Rest]
48+
? Rest["length"]
49+
: never;
50+
51+
type Connect4Board = Fill<
52+
Fill<Connect4Cell, Increment<Max<Connect4ColumnIndex>>>,
53+
Increment<Max<Connect4RowIndex>>
54+
>;
55+
type Connect4Game = {
56+
board: Connect4Board;
57+
state: Connect4State;
58+
};
59+
60+
type EmptyBoard = [
61+
[" ", " ", " ", " ", " ", " ", " "],
62+
[" ", " ", " ", " ", " ", " ", " "],
63+
[" ", " ", " ", " ", " ", " ", " "],
64+
[" ", " ", " ", " ", " ", " ", " "],
65+
[" ", " ", " ", " ", " ", " ", " "],
66+
[" ", " ", " ", " ", " ", " ", " "],
67+
];
68+
69+
type NewGame = {
70+
board: EmptyBoard;
71+
state: "🟡";
72+
};
73+
74+
type ChangeArrayElement<A extends Array<unknown>, I extends keyof A, E extends unknown> = {
75+
[Key in keyof A]: Key extends `${I extends number ? I : never}` ? E : A[Key];
76+
};
77+
78+
type FindRowIndex<Board extends Connect4Board, ColumnIndex extends Connect4ColumnIndex> = Max<
79+
{
80+
[RowIndex in keyof Board]: Board[RowIndex][ColumnIndex] extends Connect4EmptyCell
81+
? StringToNumber<RowIndex>
82+
: never;
83+
}[number]
84+
>;
85+
86+
type StringToNumber<S> = S extends `${infer FirstDigit}${infer RestDigits}`
87+
? S extends `${infer OnlyDigit}`
88+
? OnlyDigit extends infer N
89+
? N extends "0"
90+
? 0
91+
: N extends "1"
92+
? 1
93+
: N extends "2"
94+
? 2
95+
: N extends "3"
96+
? 3
97+
: N extends "4"
98+
? 4
99+
: N extends "5"
100+
? 5
101+
: N extends "6"
102+
? 6
103+
: N extends "7"
104+
? 7
105+
: N extends "8"
106+
? 8
107+
: N extends "9"
108+
? 9
109+
: never
110+
: never
111+
: // TODO: doesn't support two digit numbers
112+
never
113+
: never;
114+
115+
type GenerateNextBoard<
116+
Game extends Connect4Game,
117+
ColumnIndex extends Connect4ColumnIndex,
118+
> = FindRowIndex<Game["board"], ColumnIndex> extends infer RowIndex extends Connect4RowIndex
119+
? ChangeArrayElement<
120+
Game["board"],
121+
RowIndex,
122+
ChangeArrayElement<Game["board"][RowIndex], ColumnIndex, Game["state"]>
123+
>
124+
: never;
125+
126+
type AreThreeSameHorizontally<Row extends Array<Connect4Cell>> = Row[0] extends infer FirstCell
127+
? Row[1] extends FirstCell
128+
? Row[2] extends FirstCell
129+
? Row[3] extends FirstCell
130+
? FirstCell
131+
: never
132+
: never
133+
: never
134+
: never;
135+
136+
type FindHorizontalWinner<Board extends Connect4Board> = {
137+
[RowIndex in keyof Board]: AreThreeSameHorizontally<Board[RowIndex]> extends infer Winner extends
138+
Connect4Chips
139+
? Winner
140+
: never;
141+
}[number];
142+
143+
type SouthWestOriginatingRows = 0 | 1 | 2;
144+
145+
type AreNextThreeSameSouthWest<
146+
RowIndex extends SouthWestOriginatingRows,
147+
ColumnIndex extends Connect4ColumnIndex,
148+
Board extends Connect4Board,
149+
> = Board[RowIndex][ColumnIndex] extends infer FirstCell extends Connect4Chips
150+
? Board[Increment<RowIndex>][Decrement<ColumnIndex>] extends FirstCell
151+
? Board[Increment<Increment<RowIndex>>][Decrement<Decrement<ColumnIndex>>] extends FirstCell
152+
? Board[Increment<Increment<Increment<RowIndex>>>][Decrement<
153+
Decrement<Decrement<ColumnIndex>>
154+
>] extends FirstCell
155+
? FirstCell
156+
: never
157+
: never
158+
: never
159+
: never;
160+
161+
type FindSouthWestWinner<Board extends Connect4Board> = {
162+
[RowIndex in keyof Board]: StringToNumber<RowIndex> extends infer RowIndexAsNumber extends
163+
SouthWestOriginatingRows
164+
? AreNextThreeSameSouthWest<RowIndexAsNumber, 3, Board>
165+
: never;
166+
}[number];
167+
168+
type Test = FindSouthWestWinner<
169+
[
170+
[" ", " ", " ", " ", " ", " ", " "],
171+
[" ", " ", " ", " ", " ", " ", " "],
172+
[" ", " ", " ", "🟡", " ", " ", " "],
173+
[" ", " ", "🟡", "🔴", " ", " ", " "],
174+
["🔴", "🟡", "🔴", "🔴", " ", " ", " "],
175+
["🟡", "🔴", "🟡", "🟡", " ", " ", " "],
176+
]
177+
>;
178+
179+
type FindWinner<Board extends Connect4Board> = FindHorizontalWinner<Board> | FindSouthWestWinner<Board>;
180+
181+
type IsFilled<Board extends Connect4Board> = Connect4EmptyCell extends Board[number][number] ? false : true
182+
183+
type Test2 = IsFilled<[
184+
[" ", " ", " ", " ", " ", " ", " "],
185+
[" ", " ", " ", " ", " ", " ", " "],
186+
[" ", " ", " ", " ", " ", " ", " "],
187+
[" ", " ", " ", " ", " ", " ", " "],
188+
[" ", " ", " ", " ", " ", " ", " "],
189+
["🟡", " ", " ", " ", " ", " ", " "],
190+
]>
191+
192+
type Connect4<
193+
Game extends Connect4Game,
194+
ColumnIndex extends Connect4ColumnIndex,
195+
> = GenerateNextBoard<Game, ColumnIndex> extends infer NextBoard extends Connect4Board
196+
? {
197+
board: NextBoard;
198+
state: IsFilled<NextBoard> extends true ? 'Draw' : FindWinner<NextBoard> extends never
199+
? Exclude<Connect4Chips, Game["state"]>
200+
: FindWinner<NextBoard> extends infer Winner extends Connect4Chips ? `${Winner} Won` : never;
201+
}
202+
: never;

24.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-24
2+
13
// Santa is stuck!
24
// Santa is craving cookies! But Alas, he's stuck in a dense North Polar forest.
35
// Implement Move so Santa ('🎅') can find his way to the end of the maze.

3.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-3
2+
13
// The Gift Wrapper
24
// Did you know that there's also monetary inflation at the North Pole? You betcha, there is. And after 200+ years without a pay raise, Santa's elves are beginning to discuss a general strike.
35
// December 3rd is just about the worst time imaginable for such a strike, and Santa's desperate to calm the elves down. If he can just wrap a few presents, maybe the elves will forget that they're being paid well below market rate (don't worry: the North Pole's actually still a Deleware-based startup so therefore it's ok).

4.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-4
2+
13
// Christmas Present Delivery Addresses
24
// [a conversation in the North Pole kitchenette on the morning of December 4th between Santa and the head elf, Bernard]
35
// [Bernard] This is bullshit, Kris. I've been leading the Elves for 200+ years. Don't you trust that I know what I'm talking about?? WE NEED TO GROW THE TEAM. We're running a skeleton-crew here.

5.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-5
2+
13
// Organize Santa's List
24
// [The elves walk into work early on the morning of December 5th. A sign that reads "we're all about passion, not just paychecks" hangs above the entrance to the factory floor.]
35
// It's been a tough year for Santa's workshop. The elves are a little behind schedule on getting Santa his list. Santa reallly really likes to see the full list of names far in advance of Christmas Eve when he makes his deliveries.

6.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// Filtering The Children (part 1)
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-6
22

3+
// Filtering The Children (part 1)
34
// As you may be aware, kids that are naughty get a lump of coal and kids that are nice get a toy. Santa's sorta controlling, honestly, and he likes being able to manipulate the data by filtering out the naughty kids on some days, and filtering out the nice kids on other days.
45
// So, Santa walks over to the (open floorplan) office where the engineering team sits. Although he's just addressing the engineers, the rest of the office is distracted because they can clearly hear him since there are no walls.
56
// [Santa] You know, this job is a great opportunity for you elves, even without high pay! You're gaining valuable experience, which is more important than money!

7.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-7
2+
13
// Filtering The Children (part 2)
24
// [transcript of a slack conversation at 11:23pm between Santa and Chipper (one of the elves that worked on the filtering code from yesterday)]
35
// [Santa] We've got a big problem. That code that you gave me yesterday doesn't work!

8.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// Filtering The Children (part 3)
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-8
22

3+
// Filtering The Children (part 3)
34
// Yet again, Santa has made a request to change the children filtering code. This time he just sent an email to the entire engineering team (which is absolutely not the process, but since Santa is sometimes a bit difficult to communicate with, no one has yet had the courage to tell him). Here's the contents of the email.
4-
55
// POST /sendmail HTTP/1.1
66
// Host: mail.hohoholdings.com
77
// Content-Type: text/plain; charset=utf-8

9.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// TS playground link: https://tsplay.dev/advent-of-typescript-2023-9
2+
13
// Is Santa Dyslexic?
24
// [it's early Saturday morning and the team has been working overtime. Santa storms into the factory floor shouting..]
35
// [Santa] Don't you elves take any pride in your work?!?! Others would love to have your job for much less pay! I asked for a simple type that will reverse strings!! How hard is that?!? What do we even pay you for??

0 commit comments

Comments
 (0)