Skip to content

Commit

Permalink
solved day 18 (forgor to commit)
Browse files Browse the repository at this point in the history
  • Loading branch information
NullDev committed Dec 18, 2023
1 parent a697f4b commit 1fa61d0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
31 changes: 31 additions & 0 deletions 2023/Day_18/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,34 @@ At this point, the trench could contain 38 cubic meters of lava. However, this i
Now, the lagoon can contain a much more respectable **`62`** cubic meters of lava. While the interior is dug out, the edges are also painted according to the color codes in the dig plan.

The Elves are concerned the lagoon won't be large enough; if they follow their dig plan, **how many cubic meters of lava could it hold?**

---

## --- Part Two ---

The Elves were right to be concerned; the planned lagoon would be **much too small**.

After a few minutes, someone realizes what happened; someone ** the color and instruction parameters** when producing the dig plan. They don't have time to fix the bug; one of them asks if you can **extract the correct instructions** from the hexadecimal codes.

Each hexadecimal code is **six hexadecimal digits** long. The first five hexadecimal digits encode the **distance in meters** as a five-digit hexadecimal number. The last hexadecimal digit encodes the **direction to dig**: `0` means `R`, `1` means `D`, `2` means `L`, and `3` means `U`.

So, in the above example, the hexadecimal codes can be converted into the true instructions:

- `#70c710` = `R 461937`
- `#0dc571` = `D 56407`
- `#5713f0` = `R 356671`
- `#d2c081` = `D 863240`
- `#59c680` = `R 367720`
- `#411b91` = `D 266681`
- `#8ceee2` = `L 577262`
- `#caa173` = `U 829975`
- `#1b58a2` = `L 112010`
- `#caa171` = `D 829975`
- `#7807d2` = `L 491645`
- `#a77fa3` = `U 686074`
- `#015232` = `L 5411`
- `#7a21e3` = `U 500254`

Digging out this loop and its interior produces a lagoon that can hold an impressive **`952408144115`** cubic meters of lava.

Convert the hexadecimal color codes into the correct instructions; if the Elves follow this new dig plan, **how many cubic meters of lava could the lagoon hold?**
15 changes: 10 additions & 5 deletions 2023/Day_18/part_1.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

/* eslint-disable one-var */

const fs = require("node:fs");
const path = require("node:path");
const { performance } = require("node:perf_hooks");
Expand All @@ -8,12 +10,15 @@ const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().

const pStart = performance.now();

//
// YOUR CODE HERE
//
const result = "...";
let r = 0, c = 0, a = 0, p = 0;
INPUT.forEach((l, _, __, dn = l.split(/[ ()]+/g), [dr, dc] = { U: [-1, 0], D: [1, 0], L: [0, -1], R: [0, 1] }[dn[0]], r0 = r, c0 = c) => (
(((r += dr * Number(dn[1])) || 1) && (c += dc * Number(dn[1])) || 1)
&& (a += (r * c0 - r0 * c) / 2) || 1) && (p += Number(dn[1])),
);

const res = a + p / 2 + 1;

const pEnd = performance.now();

console.log("<DESCRIPTION>: " + result);
console.log("CUBIC METERS OF LAVA: " + res);
console.log(pEnd - pStart);
17 changes: 12 additions & 5 deletions 2023/Day_18/part_2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

/* eslint-disable one-var */

const fs = require("node:fs");
const path = require("node:path");
const { performance } = require("node:perf_hooks");
Expand All @@ -8,12 +10,17 @@ const INPUT = String(fs.readFileSync(path.join(__dirname, "input.txt"))).trim().

const pStart = performance.now();

//
// YOUR CODE HERE
//
const result = "...";
let r = 0, c = 0, a = 0, p = 0;
INPUT.forEach((l, _, __, dnc = l.split(/[ ()]+/g)) => { // @ts-ignore
(dnc[0] = ["R", "D", "L", "U"][(dnc[2].at(-1) || 0)]) && (dnc[1] = parseInt(dnc[2].slice(1, -1), 16));
const [dr, dc] = { U: [-1, 0], D: [1, 0], L: [0, -1], R: [0, 1] }[dnc[0]], r0 = r, c0 = c;
((((r += dr * Number(dnc[1])) || 1) && (c += dc * Number(dnc[1])) || 1)
&& (a += (r * c0 - r0 * c) / 2) || 1) && (p += Number(dnc[1]));
});

const res = a + p / 2 + 1;

const pEnd = performance.now();

console.log("<DESCRIPTION>: " + result);
console.log("NEW CUBIC METERS OF LAVA: " + res);
console.log(pEnd - pStart);

0 comments on commit 1fa61d0

Please sign in to comment.