Skip to content

Commit

Permalink
165th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jul 14, 2024
1 parent ef40d69 commit 9864c17
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/page-20/2103. Rings and Rods/countPoints.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { countPoints } from './countPoints';

describe('2103. Rings and Rods', () => {
test('countPoints', () => {
expect(countPoints('B0B6G0R6R0R6G9')).toBe(1);
expect(countPoints('B0R0G0R9R0B0G0')).toBe(1);
expect(countPoints('G4')).toBe(0);
});
});
27 changes: 27 additions & 0 deletions src/page-20/2103. Rings and Rods/countPoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type CountPoints = (rings: string) => number;

/**
* Accepted
*/
export const countPoints: CountPoints = (rings) => {
// Initialize a Map to store rod colors
const rodColors = new Map<string, Set<string>>();

// Iterate through the rings string
for (let i = 0; i < rings.length; i += 2) {
const color = rings[i];
const rod = rings[i + 1];

// Add colors to the corresponding rod's set
rodColors.set(rod, (rodColors.get(rod) || new Set()).add(color));
}

// Count the rods with all three colors
let count = 0;

for (const [_, colors] of rodColors) {
if (colors.size === 3) count += 1;
}

return count;
};

0 comments on commit 9864c17

Please sign in to comment.