diff --git a/src/page-20/2103. Rings and Rods/countPoints.test.ts b/src/page-20/2103. Rings and Rods/countPoints.test.ts new file mode 100644 index 0000000..feeda5c --- /dev/null +++ b/src/page-20/2103. Rings and Rods/countPoints.test.ts @@ -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); + }); +}); diff --git a/src/page-20/2103. Rings and Rods/countPoints.ts b/src/page-20/2103. Rings and Rods/countPoints.ts new file mode 100644 index 0000000..ee4bcf7 --- /dev/null +++ b/src/page-20/2103. Rings and Rods/countPoints.ts @@ -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>(); + + // 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; +};