mirror of
https://github.com/seigler/aoc2021
synced 2025-07-26 17:06:09 +00:00
day 18
This commit is contained in:
parent
9f368e7d9c
commit
36173c1a15
4 changed files with 125 additions and 12 deletions
|
@ -257,16 +257,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"part1": {
|
"part1": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "4235",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 35.41
|
||||||
},
|
},
|
||||||
"part2": {
|
"part2": {
|
||||||
"solved": false,
|
"solved": true,
|
||||||
"result": null,
|
"result": "4659",
|
||||||
"attempts": [],
|
"attempts": [],
|
||||||
"time": null
|
"time": 268.36
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
12
README.md
12
README.md
|
@ -28,7 +28,7 @@
|
||||||
[](src/day15)
|
[](src/day15)
|
||||||
[](src/day16)
|
[](src/day16)
|
||||||
[](src/day17)
|
[](src/day17)
|
||||||

|
[](src/day18)
|
||||||

|

|
||||||

|

|
||||||

|

|
||||||
|
@ -188,9 +188,9 @@ Both parts: 430.38ms
|
||||||
|
|
||||||
```
|
```
|
||||||
Day 18
|
Day 18
|
||||||
Time part 1: -
|
Time part 1: 38.77ms
|
||||||
Time part 2: -
|
Time part 2: 147.3ms
|
||||||
Both parts: -
|
Both parts: 186.07000000000002ms
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
|
@ -243,8 +243,8 @@ Both parts: -
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```
|
||||||
Total stars: 34/50
|
Total stars: 36/50
|
||||||
Total time: 2333.3799999999997ms
|
Total time: 2519.45ms
|
||||||
```
|
```
|
||||||
|
|
||||||
<!--/RESULTS-->
|
<!--/RESULTS-->
|
||||||
|
|
9
src/day18/README.md
Normal file
9
src/day18/README.md
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
# 🎄 Advent of Code 2021 - day 18 🎄
|
||||||
|
|
||||||
|
## Info
|
||||||
|
|
||||||
|
Task description: [link](https://adventofcode.com/2021/day/18)
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
...
|
104
src/day18/index.js
Normal file
104
src/day18/index.js
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
import run from "aocrunner"
|
||||||
|
|
||||||
|
const parseInput = (rawInput) => rawInput.trim().split`\n`.map(JSON.parse)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @returns [exploded, toAddLeft, newValue, toAddRight]
|
||||||
|
*/
|
||||||
|
function explode(p, maxDepth = 4) {
|
||||||
|
if (typeof p === "number") return [false, undefined, p, undefined]
|
||||||
|
const [left, right] = p
|
||||||
|
if (maxDepth == 0) return [true, left, 0, right]
|
||||||
|
let [exploded, toAddLeft, newValue, toAddRight] = explode(left, maxDepth - 1)
|
||||||
|
if (exploded) {
|
||||||
|
return [true, toAddLeft, [newValue, addRight(right, toAddRight)], undefined]
|
||||||
|
}
|
||||||
|
;[exploded, toAddLeft, newValue, toAddRight] = explode(right, maxDepth - 1)
|
||||||
|
if (exploded) {
|
||||||
|
return [true, undefined, [addLeft(left, toAddLeft), newValue], toAddRight]
|
||||||
|
}
|
||||||
|
return [false, undefined, p, undefined]
|
||||||
|
}
|
||||||
|
|
||||||
|
function addRight(v, n) {
|
||||||
|
// to the right means expanding the left side
|
||||||
|
if (n === undefined) return v
|
||||||
|
if (typeof v === "number") return v + n
|
||||||
|
return [addRight(v[0], n), v[1]]
|
||||||
|
}
|
||||||
|
|
||||||
|
function addLeft(v, n) {
|
||||||
|
// to the left means expanding the right side
|
||||||
|
if (n === undefined) return v
|
||||||
|
if (typeof v === "number") return v + n
|
||||||
|
return [v[0], addLeft(v[1], n)]
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @returns [changed, newValue] */
|
||||||
|
function split(v) {
|
||||||
|
if (typeof v === "number") {
|
||||||
|
if (v > 9) return [true, [Math.floor(v / 2), Math.ceil(v / 2)]]
|
||||||
|
return [false, v]
|
||||||
|
}
|
||||||
|
let [left, right] = v
|
||||||
|
let [changed, newValue] = split(left)
|
||||||
|
if (changed) {
|
||||||
|
return [true, [newValue, right]]
|
||||||
|
}
|
||||||
|
;[changed, newValue] = split(right)
|
||||||
|
if (changed) {
|
||||||
|
return [true, [left, newValue]]
|
||||||
|
}
|
||||||
|
return [false, v]
|
||||||
|
}
|
||||||
|
|
||||||
|
function add(a, b) {
|
||||||
|
let changed,
|
||||||
|
p = [a, b]
|
||||||
|
while (true) {
|
||||||
|
;[changed, , p] = explode(p)
|
||||||
|
if (changed) continue
|
||||||
|
;[changed, p] = split(p)
|
||||||
|
if (changed) continue
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
function magnitude(v) {
|
||||||
|
return typeof v === "number" ? v : 3 * magnitude(v[0]) + 2 * magnitude(v[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
const part1 = (rawInput) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
return magnitude(input.reduce(add))
|
||||||
|
}
|
||||||
|
|
||||||
|
const part2 = (rawInput) => {
|
||||||
|
const input = parseInput(rawInput)
|
||||||
|
let best = -Infinity
|
||||||
|
for (let i = 0; i < input.length; i++)
|
||||||
|
for (let j = i + 1; j < input.length; j++)
|
||||||
|
best = Math.max(
|
||||||
|
best,
|
||||||
|
magnitude(add(input[i], input[j])),
|
||||||
|
magnitude(add(input[j], input[i])),
|
||||||
|
)
|
||||||
|
return best
|
||||||
|
}
|
||||||
|
|
||||||
|
run({
|
||||||
|
part1: {
|
||||||
|
tests: [
|
||||||
|
// { input: ``, expected: "" },
|
||||||
|
],
|
||||||
|
solution: part1,
|
||||||
|
},
|
||||||
|
part2: {
|
||||||
|
tests: [
|
||||||
|
// { input: ``, expected: "" },
|
||||||
|
],
|
||||||
|
solution: part2,
|
||||||
|
},
|
||||||
|
trimTestInputs: true,
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue