Advent of Code 2022 - Day 13: Distress Signal Solution
Handle conditional statements carefullyPart 1
Three cases:
- all is number
- all is array
- one is array
For each case, there are 3 possibilities of
true, false, undetermined
. But in the end there is only true, false
. So keep in mind about thatImplementation
1const fs = require("fs")23const readData = () => {4 const data = fs5 .readFileSync("./input", "utf-8")6 .split(/\r?\n\r?\n/)7 .map(line => line.split(/\r?\n/).map(part => JSON.parse(part)))89 return data10}1112const main = () => {13 const pairs = readData()1415 const compare = ([left, right]) => {16 if ([left, right].every(Number.isInteger)) {17 if (left < right) return true18 if (left > right) return false19 return20 }2122 if ([left, right].every(Array.isArray)) {23 for (let i = 0; i < Math.min(left.length, right.length); i++) {24 const res = compare([left[i], right[i]])25 if (res != null) return res26 }2728 return compare([left.length, right.length])29 }3031 return compare([[left].flat(), [right].flat()])32 }3334 const res = pairs.reduce(35 (acc, el, index) => acc + (compare(el) ? index + 1 : 0),36 037 )3839 console.log(res)40}4142main()
Part 2
Flatten the
pairs
, add 2 dividers and sort the list of packets ascendingImplementation
1const fs = require("fs")23const readData = () => {4 const data = fs5 .readFileSync("./input", "utf-8")6 .split(/\r?\n\r?\n/)7 .map(line => line.split(/\r?\n/).map(part => JSON.parse(part)))89 return data10}1112const main = () => {13 const pairs = readData()1415 const compare = ([left, right]) => {16 if ([left, right].every(Number.isInteger)) {17 if (left < right) return true18 if (left > right) return false19 return20 }2122 if ([left, right].every(Array.isArray)) {23 for (let i = 0; i < Math.min(left.length, right.length); i++) {24 const res = compare([left[i], right[i]])25 if (res != null) return res26 }2728 return compare([left.length, right.length])29 }3031 return compare([[left].flat(), [right].flat()])32 }3334 const dividers = [[[2]], [[6]]]3536 const res = [...pairs.flat(), ...dividers]37 .sort((left, right) => compare([right, left]) - compare([left, right]))38 .reduce(39 (acc, el, index) => (dividers.includes(el) ? acc * (index + 1) : acc),40 141 )4243 console.log(res)44}4546main()
Trick
For the case that one of left or right is an array, instead of using ternary operator like
1Number.isInteger(left) ? compare([[left], right]) : compare([left, [right]])
we could make use of
.flat
1compare([[left].flat(), [right].flat()])
References
Comments
Loading comments...
Tags
adventofcode
recursion
sorting
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.