mirror of
https://github.com/seigler/aoc2024
synced 2025-07-25 08:16:09 +00:00
day 2
This commit is contained in:
parent
d54c4c67a2
commit
9cb3d6b5a4
4 changed files with 79 additions and 28 deletions
|
@ -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
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
18
README.md
18
README.md
|
@ -12,7 +12,7 @@
|
|||
<!--SOLUTIONS-->
|
||||
|
||||
[](src/day01)
|
||||

|
||||
[](src/day02)
|
||||

|
||||

|
||||

|
||||
|
@ -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-->
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2020",
|
||||
"target": "esnext",
|
||||
"module": "es2020",
|
||||
"removeComments": true,
|
||||
"declaration": true,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue