This commit is contained in:
Joshua Seigler 2024-12-02 01:10:16 -05:00
parent d54c4c67a2
commit 9cb3d6b5a4
4 changed files with 79 additions and 28 deletions

View file

@ -19,16 +19,16 @@
},
{
"part1": {
"solved": false,
"result": null,
"solved": true,
"result": "680",
"attempts": [],
"time": null
"time": 1.424035
},
"part2": {
"solved": false,
"result": null,
"solved": true,
"result": "710",
"attempts": [],
"time": null
"time": 2.115084
}
},
{

View file

@ -12,7 +12,7 @@
<!--SOLUTIONS-->
[![Day](https://badgen.net/badge/01/%E2%98%85%E2%98%85/green)](src/day01)
![Day](https://badgen.net/badge/02/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/02/%E2%98%85%E2%98%85/green)](src/day02)
![Day](https://badgen.net/badge/03/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/04/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/05/%E2%98%86%E2%98%86/gray)
@ -69,16 +69,16 @@ npm start 1
```
Day 01
Time part 1: 1.264ms
Time part 2: 1.025ms
Both parts: 2.289ms
Time part 1: 1.293ms
Time part 2: 1.051ms
Both parts: 2.344ms
```
```
Day 02
Time part 1: -
Time part 2: -
Both parts: -
Time part 1: 1.424ms
Time part 2: 2.115ms
Both parts: 3.539ms
```
```
@ -243,8 +243,8 @@ Both parts: -
```
```
Total stars: 2/50
Total time: 2.289ms
Total stars: 4/50
Total time: 5.883ms
```
<!--/RESULTS-->

View file

@ -1,35 +1,86 @@
import run from "aocrunner"
const parseInput = (rawInput: string) => rawInput
const parseInput = (rawInput: string) => { return rawInput.split('\n').map(line => line.split(' ').map(Number)) }
const acceptableIncreases = [1, 2, 3]
const part1 = (rawInput: string) => {
const input = parseInput(rawInput)
const differences = input.map(report => {
const d = []
report.forEach((level, i) => {
if (i === 0) return
d.push(level - report[i - 1])
})
return d
})
let safeCount = 0
differences.forEach(report => {
const sign = Math.sign(report[0])
for (const difference of report) {
if (!acceptableIncreases.includes(sign * difference)) {
return // report fails
}
}
safeCount++ // made it
})
return safeCount
}
return
const isSafe = (report: number[], errorsAllowed: 1 | 0): boolean => {
const d = [] // differences
report.forEach((level, i) => {
if (i === 0) { return }
d.push(level - report[i - 1])
})
const sign = Math.sign(d[0])
for (const difference of d) {
if (acceptableIncreases.includes(sign * difference)) continue
if (errorsAllowed === 0) {
return false
}
for (let removalIndex = 0; removalIndex < report.length; removalIndex++) {
const fixed = report.toSpliced(removalIndex, 1)
if (isSafe(fixed, 0)) { return true }
}
return false
}
return true
}
const part2 = (rawInput: string) => {
const input = parseInput(rawInput)
return
return input.reduce((acc, report) => {
const safe = isSafe(report, 1)
return acc + (safe ? 1 : 0)
}, 0)
}
run({
part1: {
tests: [
// {
// input: ``,
// expected: "",
// },
{
input: `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`,
expected: 2,
},
],
solution: part1,
},
part2: {
tests: [
// {
// input: ``,
// expected: "",
// },
{
input: `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`,
expected: 4,
}
],
solution: part2,
},

View file

@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2020",
"target": "esnext",
"module": "es2020",
"removeComments": true,
"declaration": true,