Skip to content

Commit

Permalink
Day 08 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavol Dobrocka committed Dec 8, 2023
1 parent f8eaec8 commit f19fc43
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
27 changes: 27 additions & 0 deletions day08/part1.js
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;
};
31 changes: 31 additions & 0 deletions day08/part2.js
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;
};
43 changes: 43 additions & 0 deletions day08/test.js
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);
});
});

0 comments on commit f19fc43

Please sign in to comment.