This commit is contained in:
Joshua Seigler 2021-12-18 03:54:44 -05:00
parent 9f368e7d9c
commit 36173c1a15
4 changed files with 125 additions and 12 deletions

View file

@ -257,16 +257,16 @@
},
{
"part1": {
"solved": false,
"result": null,
"solved": true,
"result": "4235",
"attempts": [],
"time": null
"time": 35.41
},
"part2": {
"solved": false,
"result": null,
"solved": true,
"result": "4659",
"attempts": [],
"time": null
"time": 268.36
}
},
{

View file

@ -28,7 +28,7 @@
[![Day](https://badgen.net/badge/15/%E2%98%85%E2%98%85/green)](src/day15)
[![Day](https://badgen.net/badge/16/%E2%98%85%E2%98%85/green)](src/day16)
[![Day](https://badgen.net/badge/17/%E2%98%85%E2%98%85/green)](src/day17)
![Day](https://badgen.net/badge/18/%E2%98%86%E2%98%86/gray)
[![Day](https://badgen.net/badge/18/%E2%98%85%E2%98%85/green)](src/day18)
![Day](https://badgen.net/badge/19/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/20/%E2%98%86%E2%98%86/gray)
![Day](https://badgen.net/badge/21/%E2%98%86%E2%98%86/gray)
@ -188,9 +188,9 @@ Both parts: 430.38ms
```
Day 18
Time part 1: -
Time part 2: -
Both parts: -
Time part 1: 38.77ms
Time part 2: 147.3ms
Both parts: 186.07000000000002ms
```
```
@ -243,8 +243,8 @@ Both parts: -
```
```
Total stars: 34/50
Total time: 2333.3799999999997ms
Total stars: 36/50
Total time: 2519.45ms
```
<!--/RESULTS-->

9
src/day18/README.md Normal file
View 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
View 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,
})