Skip to content

Commit 62e7d78

Browse files
committed
feat: day 02, part 2 - not completed
1 parent 63a8fbd commit 62e7d78

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

2024/ts/src/day-2-red-nosed-reports.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { describe, it, expect } from "bun:test";
22
import {
33
day_2_part_1,
4+
day_2_part_2,
45
isReportSafe,
56
transformInputIntoArrays,
7+
removeFirstNonSequential,
68
} from "./day-2-red-nosed-reports";
79

810
const fileTest = Bun.file("./src/day-2-red-nosed-reports.input.test.txt");
@@ -64,6 +66,44 @@ describe("transformInputIntoArrays", () => {
6466
});
6567
});
6668

69+
describe("removeFirstNonSequential", () => {
70+
it("return [7, 6, 4, 2, 1] for [7, 6, 4, 2, 1]", () => {
71+
const result = removeFirstNonSequential([7, 6, 4, 2, 1]);
72+
const expected = [7, 6, 4, 2, 1];
73+
expect(result).toEqual(expected);
74+
});
75+
it("return [1, 2, 7, 8, 9] for [1, 2, 7, 8, 9]", () => {
76+
const result = removeFirstNonSequential([1, 2, 7, 8, 9]);
77+
const expected = [1, 2, 7, 8, 9];
78+
expect(result).toEqual(expected);
79+
});
80+
it("return [9, 7, 6, 2, 1] for [9, 7, 6, 2, 1]", () => {
81+
const result = removeFirstNonSequential([9, 7, 6, 2, 1]);
82+
const expected = [9, 7, 6, 2, 1];
83+
expect(result).toEqual(expected);
84+
});
85+
it("return [1, 2, 4, 5] for [1, 3, 2, 4, 5]", () => {
86+
const result = removeFirstNonSequential([1, 3, 2, 4, 5]);
87+
const expected = [1, 2, 4, 5];
88+
expect(result).toEqual(expected);
89+
});
90+
it("return [8, 6, 4, 1] for [8, 6, 4, 4, 1]", () => {
91+
const result = removeFirstNonSequential([8, 6, 4, 4, 1]);
92+
const expected = [8, 6, 4, 1];
93+
expect(result).toEqual(expected);
94+
});
95+
it("return [1, 3, 6, 7, 9] for [1, 3, 6, 7, 9]", () => {
96+
const result = removeFirstNonSequential([1, 3, 6, 7, 9]);
97+
const expected = [1, 3, 6, 7, 9];
98+
expect(result).toEqual(expected);
99+
});
100+
it.skip("return [ 2, 4, 6, 9, 10] for [ 2, 4, 6, 9, 10, 9 ]", () => {
101+
const result = removeFirstNonSequential([2, 4, 6, 9, 10, 9]);
102+
const expected = [2, 4, 6, 9, 10];
103+
expect(result).toEqual(expected);
104+
});
105+
});
106+
67107
describe("Day 2: Red-Nosed Reports", () => {
68108
describe("part 1", () => {
69109
it("should return the correct amount of report from the test file", () => {
@@ -79,4 +119,18 @@ describe("Day 2: Red-Nosed Reports", () => {
79119
expect(typeof contentTest).toBe("string");
80120
});
81121
});
122+
describe("part 2", () => {
123+
it("should return the correct amount of report from the test file", () => {
124+
const result = day_2_part_2(contentTest);
125+
const expected = 4;
126+
expect(result).toBe(expected);
127+
expect(typeof contentTest).toBe("string");
128+
});
129+
it.skip("should return the correct amount of report from the file", () => {
130+
const result = day_2_part_2(content);
131+
const expected = 307;
132+
expect(result).toBe(expected);
133+
expect(typeof contentTest).toBe("string");
134+
});
135+
});
82136
});

2024/ts/src/day-2-red-nosed-reports.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,66 @@ export const day_2_part_1 = (input: string): number => {
4646

4747
return safeReports;
4848
};
49+
50+
// ------ PART 2
51+
52+
export function removeFirstNonSequential(arr) {
53+
if (arr.length <= 2) return arr;
54+
55+
// Determine if sequence is ascending or descending
56+
const isAscending = arr[1] > arr[0];
57+
const result = [...arr];
58+
59+
// Find first number that breaks the sequence
60+
for (let i = 1; i < arr.length - 1; i++) {
61+
if (isAscending) {
62+
// For ascending sequence, check current and next number
63+
if (result[i] > result[i + 1]) {
64+
result.splice(i, 1);
65+
break;
66+
}
67+
} else {
68+
// For descending sequence, check current and next number
69+
// Also remove if numbers are equal (not strictly descending)
70+
if (result[i] <= result[i + 1]) {
71+
result.splice(i, 1);
72+
break;
73+
}
74+
}
75+
}
76+
77+
return result;
78+
}
79+
80+
const removeConsecutiveDuplicates = (arr: number[]) =>
81+
arr.filter((num, index) => num !== arr[index - 1]);
82+
83+
// Check for consecutive duplicates
84+
const hasConsecutiveDuplicates = (arr: number[]) => {
85+
return arr.some((num, index) => num === arr[index + 1]);
86+
};
87+
88+
export const removingSingleBadLevel = (report: number[]): number[] => {
89+
// find first duplicate, remove one of and return the new array
90+
91+
if (hasConsecutiveDuplicates(report))
92+
return removeConsecutiveDuplicates(report);
93+
94+
return removeFirstNonSequential(report);
95+
};
96+
97+
export const day_2_part_2 = (input: string): number => {
98+
let safeReports = 0;
99+
100+
const reports = transformInputIntoArrays(input);
101+
102+
reports.map((report) => {
103+
const changed_report = removingSingleBadLevel(report);
104+
105+
if (isReportSafe(changed_report)) {
106+
safeReports += 1;
107+
}
108+
});
109+
110+
return safeReports;
111+
};

0 commit comments

Comments
 (0)