mirror of
https://github.com/seigler/aoc2024
synced 2025-07-26 00:36:10 +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": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "680",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 1.424035
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "710",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 2.115084
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
18
README.md
18
README.md
|
@ -12,7 +12,7 @@
|
||||||
<!--SOLUTIONS-->
|
<!--SOLUTIONS-->
|
||||||
|
|
||||||
[](src/day01)
|
[](src/day01)
|
||||||

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

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -69,16 +69,16 @@ npm start 1
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 01
|
Day 01
|
||||||
Time part 1: 1.264ms
|
Time part 1: 1.293ms
|
||||||
Time part 2: 1.025ms
|
Time part 2: 1.051ms
|
||||||
Both parts: 2.289ms
|
Both parts: 2.344ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 02
|
Day 02
|
||||||
Time part 1: -
|
Time part 1: 1.424ms
|
||||||
Time part 2: -
|
Time part 2: 2.115ms
|
||||||
Both parts: -
|
Both parts: 3.539ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 2/50
|
Total stars: 4/50
|
||||||
Total time: 2.289ms
|
Total time: 5.883ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
|
@ -1,35 +1,86 @@
|
||||||
import run from "aocrunner"
|
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 part1 = (rawInput: string) => {
|
||||||
const input = parseInput(rawInput)
|
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 part2 = (rawInput: string) => {
|
||||||
const input = parseInput(rawInput)
|
const input = parseInput(rawInput)
|
||||||
|
return input.reduce((acc, report) => {
|
||||||
return
|
const safe = isSafe(report, 1)
|
||||||
|
return acc + (safe ? 1 : 0)
|
||||||
|
}, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
run({
|
run({
|
||||||
part1: {
|
part1: {
|
||||||
tests: [
|
tests: [
|
||||||
// {
|
{
|
||||||
// input: ``,
|
input: `7 6 4 2 1
|
||||||
// expected: "",
|
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,
|
solution: part1,
|
||||||
},
|
},
|
||||||
part2: {
|
part2: {
|
||||||
tests: [
|
tests: [
|
||||||
// {
|
{
|
||||||
// input: ``,
|
input: `7 6 4 2 1
|
||||||
// expected: "",
|
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,
|
solution: part2,
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es2020",
|
"target": "esnext",
|
||||||
"module": "es2020",
|
"module": "es2020",
|
||||||
"removeComments": true,
|
"removeComments": true,
|
||||||
"declaration": true,
|
"declaration": true,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue