Skip to content

Commit

Permalink
Day 18 part 2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
harryagstian committed Dec 18, 2021
1 parent 1ef08a9 commit 1c2995a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 40 deletions.
88 changes: 58 additions & 30 deletions js/day18.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@ const flags = require('flags')
require("util").inspect.defaultOptions.depth = null;
const _ = require('lodash')

const logWrapper = (item) => {
const debug = true

if (debug) {
console.log(item)
}
}

let globalTree = {}
let actionTaken = 0
let canSplit = true
Expand All @@ -30,7 +22,7 @@ const solve = (sample) => {

snailfishReducer(0, [], "explode")
snailfishReducer(0, [], "split")

if (t === actionTaken) {
break
} else {
Expand All @@ -40,10 +32,45 @@ const solve = (sample) => {
}
}

// console.log({ output: JSON.stringify(treeToArray(globalTree)) })
printSolution(getMagnitude(globalTree))

let maxSum = 0
for (let i = 0; i < inputs.length; i++) {
for (let j = 0; j < inputs.length; j++) {
if (i === j) {
continue
}
let line1 = inputs[i]
let line2 = inputs[j]

globalTree = {
x: arrayToTree(eval(line1)),
y: arrayToTree(eval(line2))
}

let t = actionTaken
while (true) {
canSplit = true

snailfishReducer(0, [], "explode")
snailfishReducer(0, [], "split")

if (t === actionTaken) {
break
} else {
t = actionTaken
}
}

let sum = getMagnitude(globalTree)
maxSum = Math.max(sum, maxSum)
}
}

printSolution(maxSum)
}

// convert object to array
const treeToArray = (tree) => {
let { x, y } = tree
if (typeof x === "object") {
Expand All @@ -56,6 +83,7 @@ const treeToArray = (tree) => {
return [x, y]
}

// convert array to object
const arrayToTree = (arr) => {
const tree = {}

Expand All @@ -78,71 +106,63 @@ const arrayToTree = (arr) => {

const snailfishReducer = (depth, key = [], mode) => {
let { x, y } = getPropByString(globalTree, key.join("."))

// traverse left most first
if (typeof x === "object") {
let clone = _.cloneDeep(key)
clone.push("x")
snailfishReducer(depth + 1, clone, mode)
}
// explode

// explode
if (depth === 4 && mode === "explode") {
// console.log("before", mode, globalTreeToJSON())
let tempKey = _.cloneDeep(key)
const currentSide = tempKey.pop()
const parentTree = getPropByString(globalTree, tempKey.join("."))

parentTree[currentSide] = "?"
// console.log("during", mode, globalTreeToJSON())
let jsonString = JSON.stringify(treeToArray(globalTree))
// console.log({ a: parentTree[currentSide], currentSide, jsonString })
// get first number after question mark
let rightRule = /(.*"\?".*?)(\d+)(.*)/g
// get first number before question mark
let leftRule = /(.*)(\W)(\d+)(.*"\?".*)(.*)/g

let res = leftRule.exec(jsonString)
if (res !== null) {
// addition to first number before question mark
let newStr = res[1] + res[2] + String(Number(res[3]) + x) + res[4] + res[5]
// console.log({ jsonString, newStr, res })
jsonString = newStr
}

let res2 = rightRule.exec(jsonString)

if (res2 !== null) {
// addition to first number after question mark
let newStr = res2[1] + String(Number(res2[2]) + y) + res2[3]
jsonString = newStr
}

jsonString = jsonString.replace(/"\?"/, "0")
// console.log({ jsonString, res, x, y, depth })
globalTree = arrayToTree(JSON.parse(jsonString))
// console.log({ globalTree, jsonString })
// console.log("after", mode, globalTreeToJSON())
} else if (canSplit && mode === "split") {
// console.log({ x, y, key: "asdasdasd" })
if (x >= 10) {
let base = x / 2
let tempKey = _.cloneDeep(key)
// const currentSide = tempKey.pop()
const parentTree = getPropByString(globalTree, tempKey.join("."))
// console.log("before", mode, globalTreeToJSON(), x, base, parentTree)
parentTree.x = { x: Math.floor(base), y: Math.ceil(base) }
// console.log("after", mode, globalTreeToJSON(), parentTree)
actionTaken++
canSplit = false
doneForNow = true
} else if (y >= 10) {
let base = y / 2
let tempKey = _.cloneDeep(key)
// const currentSide = tempKey.pop()
const parentTree = getPropByString(globalTree, tempKey.join("."))
parentTree.y = { x: Math.floor(base), y: Math.ceil(base) }
actionTaken++
canSplit = false
doneForNow = true
}
// console.log({ a: JSON.stringify(treeToArray(globalTree)) })
}

// traverse to y
if (typeof y === "object") {
let clone = _.cloneDeep(key)
clone.push("y")
Expand All @@ -157,7 +177,6 @@ const getPropByString = (obj, propString) => {
let prop, props = propString.split('.');
let i = 0


for (let iLen = props.length - 1; i < iLen; i++) {
prop = props[i];

Expand All @@ -171,7 +190,15 @@ const getPropByString = (obj, propString) => {
return obj[props[i]];
}

let dp = {}

const getMagnitude = (tree) => {
let key = JSON.stringify(tree)

if (dp[key]) {
return dp[key]
}

let { x, y } = tree


Expand All @@ -183,12 +210,13 @@ const getMagnitude = (tree) => {
y = getMagnitude(y)
}



return (3 * x) + (2 * y)
let ans = (3 * x) + (2 * y)
dp[key] = ans
return ans
}

const globalTreeToJSON = () => {
// for debugging
return JSON.stringify(treeToArray(globalTree))
}

Expand Down
20 changes: 10 additions & 10 deletions js/inputs/day18sample.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
[7,[5,[[3,8],[1,4]]]]
[[2,[2,2]],[8,[8,1]]]
[2,9]
[1,[[[9,3],9],[[9,0],[0,7]]]]
[[[5,[7,4]],7],1]
[[[[4,2],2],6],[8,7]]
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
[[[5,[2,8]],4],[5,[[9,9],0]]]
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
[[[[5,4],[7,7]],8],[[8,3],8]]
[[9,3],[[9,9],[6,[4,9]]]]
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]

0 comments on commit 1c2995a

Please sign in to comment.