This commit is contained in:
2024-12-06 10:25:57 +01:00
parent 05f4b0b165
commit 0c0bb5e198
4 changed files with 1090 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
import { open } from 'node:fs/promises';
const file = await open('./2024/inputs/2');
const all_diffs = []
for await (const line of file.readLines()) {
const levelsMatch = line.matchAll(/\d+ ?/gm)
const levels = Array.from(levelsMatch).map(e => Number(e[0]))
let previous = levels.shift()!
all_diffs.push(levels.map((e) => {
const value = e - previous
previous = e
return value
}))
}
const result = all_diffs.reduce((prev, line, index) => {
if (line.some(e => e == 0)) {
return prev
}
if (!line.every(e => e < 0) && !line.every(e => e > 0)) {
return prev
}
if (!line.every(e => Math.abs(e) >= 1 && Math.abs(e) <= 3)) {
return prev
}
return prev + 1
}, 0)
console.log(result)
all_diffs
+54
View File
@@ -0,0 +1,54 @@
// Doesn't works
import { open } from 'node:fs/promises';
const file = await open('./2024/inputs/2');
const all_diffs = []
const orig_lines: number[][] = []
for await (const line of file.readLines()) {
const levelsMatch = line.matchAll(/\d+ ?/gm)
const levels = Array.from(levelsMatch).map(e => Number(e[0]))
all_diffs.push(compute_diff(levels))
orig_lines.push(levels)
}
function compute_diff(line: number[]) {
line = Array.from(line)
let previous = line.shift()!
return line.map((e) => {
const value = e - previous
previous = e
return value
})
}
function keep_only_errors(line: number[]) {
return line.reduce<number[]>((prev, curr, index) => {
if (curr == 0) {
prev.push(index)
} else if (index > 0 && (line[index - 1] < 0) && (curr > 0)) {
prev.push(index)
} else if (index > 0 && (line[index - 1] > 0) && (curr < 0)) {
prev.push(index)
} else if (!((Math.abs(curr) >= 1) && (Math.abs(curr) <= 3))) {
prev.push(index)
}
return prev
}, [])
}
const result = all_diffs.map((line) => {
return keep_only_errors(line)
})
const orig_lines_spliced = all_diffs.map((line, index) => {
const l = Array.from(orig_lines[index])
const errors_indexes = keep_only_errors(line)
if (errors_indexes.length > 0) {
l.splice(errors_indexes[0], 1)
}
return l
})
console.log(orig_lines_spliced.map(compute_diff).map(keep_only_errors).reduce((t, c) => c.length == 0 ? t + 1 : t, 0))
all_diffs
+1000
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -0,0 +1,6 @@
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9