Advent of Code 2022 - Day 4: Camp Cleanup Solution
Dealing with start, endPart 1: Wholly Include
Transform the data into the format of
1[2 [rangeA, rangeB],3 ...4]
with each range of format
1[start, end]
Now, let the inclusion logic speak for itself
Implementation
1const fs = require("fs")23const data = fs4 .readFileSync("./input", "utf-8")5 .split(/\r?\n/)6 .filter(Boolean)7 .map(s => s.split(",").map(s => s.split("-").map(Number)))89const isInclude = (rangeA, rangeB) =>10 (rangeA[0] <= rangeB[0] && rangeA[1] >= rangeB[1]) ||11 (rangeB[0] <= rangeA[0] && rangeB[1] >= rangeA[1])1213const res = data.filter(([rangeA, rangeB]) => isInclude(rangeA, rangeB)).length1415console.log(res)
Part 2: Partially Include
Same as Part 1, but with different inclusion checking logic
Implementation
1const fs = require("fs")23const data = fs4 .readFileSync("./input", "utf-8")5 .split(/\r?\n/)6 .filter(Boolean)7 .map(s => s.split(",").map(s => s.split("-").map(Number)))89const isInclude = (rangeA, rangeB) =>10 (rangeA[0] <= rangeB[0] && rangeA[1] >= rangeB[0]) ||11 (rangeB[0] <= rangeA[0] && rangeB[1] >= rangeA[0])1213const res = data.filter(([rangeA, rangeB]) => isInclude(rangeA, rangeB)).length1415console.log(res)
References
Comments
Loading comments...
Tags
adventofcode
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.
Next Post
Advent of Code 2022 - Day 5: Supply Stacks
Transform (the hard part) and mutate
Previous Post
Advent of Code 2022 - Day 2: Rock Paper Scissors
Key-pair to the help