-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Pavol Dobrocka
committed
Dec 8, 2023
1 parent
f8eaec8
commit f19fc43
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module.exports = (input) => { | ||
let rows = input.split(/\r?\n/); | ||
let instructions = [...rows[0]]; | ||
|
||
let tree = rows | ||
.slice(2) | ||
.map((r) => /([\w]+) = \(([\w]+), ([\w]+)\)/.exec(r)) | ||
.map(([, a, l, r]) => [a, l, r]) | ||
.reduce((map, [a, l, r]) => map.set(a, [l, r]), new Map()); | ||
|
||
let position = "AAA"; | ||
let ip = 0; | ||
let length = 0; | ||
while (position != "ZZZ") { | ||
position = | ||
tree.get(position)[ | ||
instructions[ | ||
ip % instructions.length | ||
] == "L" | ||
? 0 | ||
: 1 | ||
]; | ||
ip++; | ||
length++; | ||
} | ||
return length; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
function steps(tree, position) {} | ||
module.exports = (input) => { | ||
let rows = input.split(/\r?\n/); | ||
let instructions = [...rows[0]]; | ||
|
||
let tree = rows | ||
.slice(2) | ||
.map((r) => /([\w]+) = \(([\w]+), ([\w]+)\)/.exec(r)) | ||
.map(([, a, l, r]) => [a, l, r]) | ||
.reduce((map, [a, l, r]) => map.set(a, [l, r]), new Map()); | ||
|
||
let positions = [...tree.keys()].filter((a) => a.endsWith("A")); | ||
let ip = 0; | ||
let length = 0; | ||
while (positions.some((e) => !e.endsWith("Z"))) { | ||
positions = positions.map( | ||
(position) => | ||
tree.get(position)[ | ||
instructions[ | ||
ip % | ||
instructions.length | ||
] == "L" | ||
? 0 | ||
: 1 | ||
] | ||
); | ||
ip++; | ||
length++; | ||
} | ||
return length; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const expect = require("expect.js"); | ||
const part1 = require("./part1"); | ||
const part2 = require("./part2"); | ||
|
||
describe("Day 08: Part 1", () => { | ||
it("Should calculate number of steps required to finish", () => { | ||
expect( | ||
part1(`RL | ||
AAA = (BBB, CCC) | ||
BBB = (DDD, EEE) | ||
CCC = (ZZZ, GGG) | ||
DDD = (DDD, DDD) | ||
EEE = (EEE, EEE) | ||
GGG = (GGG, GGG) | ||
ZZZ = (ZZZ, ZZZ)`) | ||
).to.equal(2); | ||
|
||
expect( | ||
part1(`LLR | ||
AAA = (BBB, BBB) | ||
BBB = (AAA, ZZZ) | ||
ZZZ = (ZZZ, ZZZ)`) | ||
).to.equal(6); | ||
}); | ||
}); | ||
describe("Day 08: Part 2", () => { | ||
it("Should calculate number of steps required to finish all paths starting with A simultaniously", () => { | ||
expect( | ||
part2(`LR | ||
11A = (11B, XXX) | ||
11B = (XXX, 11Z) | ||
11Z = (11B, XXX) | ||
22A = (22B, XXX) | ||
22B = (22C, 22C) | ||
22C = (22Z, 22Z) | ||
22Z = (22B, 22B) | ||
XXX = (XXX, XXX)`) | ||
).to.equal(6); | ||
}); | ||
}); |