Skip to content

Commit 526fe01

Browse files
committed
🚦 added challenge 9
1 parent 49e7af3 commit 526fe01

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

β€Ž.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
node_modules/
2+
3+
tricks.md

β€Žsrc/challenges/09.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function adjustLights (lights: string[]): number {
2+
let o1 = 0
3+
let o2 = 0
4+
let index = 0
5+
for (const light of lights) {
6+
o1 += +(light !== '🟒') * +(index % 2 !== 0)
7+
o2 += +(light !== 'πŸ”΄') * +(index % 2 !== 0)
8+
o1 += +(light !== 'πŸ”΄') * +(index % 2 !== 1)
9+
o2 += +(light !== '🟒') * +(index % 2 !== 1)
10+
index++
11+
}
12+
const x = o1 - o2
13+
return (x + 2 * o2 - ((x + (x >> 31)) ^ (x >> 31))) / 2
14+
}

β€Žsrc/tests/09.spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { test, expectTypeOf, expect, describe } from 'vitest'
2+
import { adjustLights } from '../challenges/09'
3+
4+
describe('Challenge #09', () => {
5+
test('Test #01', () => {
6+
expectTypeOf(adjustLights).returns.toEqualTypeOf(1)
7+
})
8+
9+
// Test: adjustLights(["🟒", "πŸ”΄", "🟒", "🟒", "🟒"]) -> 1
10+
test('Test #02', () => {
11+
expect(adjustLights(['🟒', 'πŸ”΄', '🟒', '🟒', '🟒'])).toEqual(1)
12+
})
13+
14+
// Test: adjustLights(["πŸ”΄", "πŸ”΄", "🟒", "🟒", "πŸ”΄"]) -> 2
15+
test('Test #03', () => {
16+
expect(adjustLights(['πŸ”΄', 'πŸ”΄', '🟒', '🟒', 'πŸ”΄'])).toEqual(2)
17+
})
18+
19+
// Test: adjustLights(["🟒", "πŸ”΄", "🟒", "πŸ”΄", "🟒"]) -> 0
20+
test('Test #04', () => {
21+
expect(adjustLights(['🟒', 'πŸ”΄', '🟒', 'πŸ”΄', '🟒'])).toEqual(0)
22+
})
23+
24+
// Test: adjustLights(["πŸ”΄", "πŸ”΄", "πŸ”΄"] -> 1
25+
test('Test #05', () => {
26+
expect(adjustLights(['πŸ”΄', 'πŸ”΄', 'πŸ”΄'])).toEqual(1)
27+
})
28+
})

0 commit comments

Comments
Β (0)