Skip to content

Commit cd3a74f

Browse files
committed
Solution 39 - TypeScript
1 parent 69c6126 commit cd3a74f

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/* -------------------------------------------------------------------------- */
2+
/* FIRST CHALLENGE */
3+
/* -------------------------------------------------------------------------- */
4+
5+
function getBatmanDayAnniversary(anniversary: number): Date {
6+
const anniversaryDate: Date = new Date(1939, 8, 16)
7+
8+
for (let i = 0; i < anniversary; i++) {
9+
const currentYear: number = anniversaryDate.getFullYear()
10+
11+
anniversaryDate.setFullYear(currentYear + 1)
12+
anniversaryDate.setMonth(8, 1)
13+
14+
let saturdayCounter: number = anniversaryDate.getDay() === 6 ? 1 : 0
15+
16+
while (saturdayCounter < 3) {
17+
const currentDate: number = anniversaryDate.getDate()
18+
anniversaryDate.setDate(currentDate + 1)
19+
20+
if (anniversaryDate.getDay() === 6) saturdayCounter++
21+
}
22+
}
23+
24+
return anniversaryDate
25+
}
26+
27+
const batmanDay85thAnniversary: Date = getBatmanDayAnniversary(85)
28+
const batmanDay100thAnniversary: Date = getBatmanDayAnniversary(100)
29+
30+
console.log('> First challenge...')
31+
32+
console.log(
33+
`\n> The 85th anniversary of Batman day is on ${batmanDay85thAnniversary.toLocaleDateString()}.`
34+
)
35+
36+
console.log(
37+
`\n> The 100th anniversary of Batman day is on ${batmanDay100thAnniversary.toLocaleDateString()}.`
38+
)
39+
40+
/* -------------------------------------------------------------------------- */
41+
/* SECOND CHALLENGE */
42+
/* -------------------------------------------------------------------------- */
43+
44+
/* ---------------------------------- Types --------------------------------- */
45+
46+
type ArrayOfN<
47+
T extends any,
48+
N extends number,
49+
Acc extends T[] = []
50+
> = Acc['length'] extends N ? Acc : ArrayOfN<T, N, [T, ...Acc]>
51+
52+
type ToRange<
53+
Stop extends number,
54+
Counter extends 0[] = [],
55+
Acc extends number[] = []
56+
> = Counter['length'] extends Stop
57+
? Acc[number]
58+
: ToRange<Stop, [0, ...Counter], [Counter['length'], ...Acc]>
59+
60+
type ThreatLevel = ToRange<21>
61+
62+
type Sensors = ArrayOfN<ArrayOfN<ThreatLevel, 20>, 20>
63+
64+
type SensorPosX = ToRange<20>
65+
type SensorPosY = ToRange<20>
66+
67+
/* ---------------------------------- Main ---------------------------------- */
68+
69+
function getThreatLevels(
70+
sensors: Sensors
71+
): [SensorPosX, SensorPosY, ThreatLevel][] {
72+
const threatLevels: [SensorPosX, SensorPosY, ThreatLevel][] = []
73+
74+
for (let i = 1; i < sensors.length - 1; i++) {
75+
for (let j = 1; j < sensors[i].length - 1; j++) {
76+
let threatLevel: ThreatLevel = 0
77+
78+
threatLevel += sensors[i - 1][j - 1]
79+
threatLevel += sensors[i - 1][j]
80+
threatLevel += sensors[i - 1][j + 1]
81+
82+
threatLevel += sensors[i][j - 1]
83+
threatLevel += sensors[i][j]
84+
threatLevel += sensors[i][j + 1]
85+
86+
threatLevel += sensors[i + 1][j - 1]
87+
threatLevel += sensors[i + 1][j]
88+
threatLevel += sensors[i + 1][j + 1]
89+
90+
threatLevels.push([
91+
j as SensorPosX,
92+
i as SensorPosY,
93+
threatLevel as ThreatLevel,
94+
])
95+
}
96+
}
97+
98+
return threatLevels
99+
}
100+
101+
const batmanCavePos = [0, 0]
102+
103+
const sensors: Sensors = [
104+
[1, 0, 1, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1],
105+
[2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0],
106+
[1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1],
107+
[0, 9, 8, 0, 2, 0, 9, 2, 8, 1, 0, 1, 3, 7, 8, 1, 0, 2, 7, 6],
108+
[1, 0, 1, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1],
109+
[3, 1, 8, 7, 9, 6, 1, 7, 9, 0, 1, 0, 1, 9, 2, 1, 3, 0, 8, 0],
110+
[1, 6, 9, 1, 2, 8, 0, 2, 1, 3, 2, 8, 7, 2, 8, 0, 1, 7, 9, 0],
111+
[1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 2, 0, 1, 0, 2, 0, 1, 0, 1, 0],
112+
[0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0],
113+
[2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2],
114+
[7, 8, 6, 1, 0, 9, 2, 8, 1, 3, 7, 1, 0, 2, 0, 9, 1, 6, 7, 8],
115+
[0, 1, 7, 9, 2, 8, 1, 6, 7, 8, 1, 2, 0, 3, 1, 8, 2, 7, 0, 1],
116+
[3, 0, 9, 1, 8, 6, 1, 0, 7, 9, 2, 0, 1, 2, 8, 0, 1, 9, 6, 1],
117+
[1, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0],
118+
[0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0],
119+
[2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2],
120+
[9, 6, 1, 0, 9, 8, 7, 2, 8, 0, 1, 0, 3, 6, 2, 7, 9, 2, 8, 1],
121+
[0, 7, 8, 9, 6, 2, 1, 9, 6, 7, 8, 0, 9, 7, 6, 8, 1, 0, 2, 8],
122+
[2, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 1, 2],
123+
[3, 2, 1, 9, 6, 7, 0, 3, 1, 8, 2, 7, 9, 2, 0, 9, 7, 6, 8, 0],
124+
]
125+
126+
const threatLevels: [SensorPosX, SensorPosY, ThreatLevel][] =
127+
getThreatLevels(sensors)
128+
129+
const threatLevelsGreaterThanTwenty: [SensorPosX, SensorPosY, ThreatLevel][] =
130+
threatLevels.filter((sensor) => sensor[2] >= 20)
131+
132+
console.log('\n> Second challenge...')
133+
134+
console.log(
135+
'\n> Threat levels greater than 20 (security protocol activated)...'
136+
)
137+
138+
for (let i = 0; i < threatLevelsGreaterThanTwenty.length; i++) {
139+
const [x, y, threatLevel] = threatLevelsGreaterThanTwenty[i]
140+
console.log(`\n> Coordinates (x, y): (${x}, ${y}).`)
141+
console.log(`> Threat level: ${threatLevel}.`)
142+
}
143+
144+
console.log('\n> Position with the maximum threat level...')
145+
146+
let maxThreatLevel: [SensorPosX, SensorPosY, ThreatLevel] =
147+
threatLevelsGreaterThanTwenty[0]
148+
149+
for (const threatLevel of threatLevelsGreaterThanTwenty) {
150+
if (threatLevel[2] > maxThreatLevel[2]) maxThreatLevel = threatLevel
151+
}
152+
153+
const distanceToBatmanCave: number =
154+
Math.abs(maxThreatLevel[0] - batmanCavePos[0]) +
155+
Math.abs(maxThreatLevel[1] - batmanCavePos[1])
156+
157+
console.log(
158+
`\n> Coordinates (x, y): (${maxThreatLevel[0]}, ${maxThreatLevel[1]}).`
159+
)
160+
console.log(`> Threat level: ${maxThreatLevel[2]}.`)
161+
console.log(`> Distance to batman cave: ${distanceToBatmanCave} cells.`)

0 commit comments

Comments
 (0)