Today I’m going for Javascript. Advent of Code, day 9, here I come. I’m going to run the code on the command line using node.
Part 1: Process a string containing instructions such as (AxB) where A and B are integers saying to repeat the next A characters B times. What’s the length of the expanded string? The expansions are not processed. Examples given:
ADVENT → 6 A(1x5)BC → ABBBBBC → 7 (6x1)(1x3)A → (1x3)A → 6
Part 2: Same thing, but now expansions *are* processed.
X(8x2)(3x3)ABCY → XABCABCABCABCABCABCY → 20
Part 1 was more or less simple, but as you can see I needed a lot of console output to get my off-by-one errors under control and it also took me quite some time to find the `6 + "1" → 61` error.
When I saw the input provided I decided not to worry about the “ignore whitespace” section of the puzzle.
process.stdin.setEncoding('utf8'); const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, }); const assert = require('assert'); function process_line (line) { line = line.replace(/\s+/, ""); // console.log("01234567890123456789"); var i = 0; while (i < line.length) { if (line[i] === '(') { // console.log("> i=" + i); var a = line.indexOf('x', i + 1); // console.log("> a=" + a); assert(a > i, 'did not find "x"'); var l = parseInt(line.slice(i+1, a)); // console.log("> l=" + l); var b = line.indexOf(')', a + 1); // console.log("> b=" + b); assert(b > a + 1, 'did not find ")"'); var t = parseInt(line.slice(a+1, b)); // console.log("> t=" + t); var chunk = line.substr(b + 1, l); // console.log("> chunk=" + chunk); line = line.substr(0, i) + chunk.repeat(t) + line.substr(b + 1 + l); // console.log("> line=" + line); i = i - 1 + t * l; // console.log("> i=" + i); } else { i += 1; } } console.log("> " + line + " (" + line.length + ")"); } function process_input () { rl.on('line', (input) => process_line(input)); } process_input();
Part 2 surprised me because the recursive expansions make my previous approach untenable. I waited for a minute or two and aborted it. So now I can’t just build representations of the intermediate strings.
process.stdin.setEncoding('utf8'); const readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, }); const assert = require('assert'); function process_line (line) { line = line.replace(/\s+/, ""); // console.log("01234567890123456789"); var i = 0; var c = 0; while (i < line.length) { if (line[i] === '(') { // console.log("> i=" + i); var a = line.indexOf('x', i + 1); // console.log("> a=" + a); // assert(a > i, 'did not find "x"'); var l = parseInt(line.slice(i+1, a)); // console.log("> l=" + l); var b = line.indexOf(')', a + 1); // console.log("> b=" + b); // assert(b > a + 1, 'did not find ")"'); var t = parseInt(line.slice(a+1, b)); // console.log("> t=" + t); var chunk = line.substr(b + 1, l); // console.log("> chunk=" + chunk); c += t * process_line(chunk); line = line.substr(b + 1 + l); // console.log("> line=" + line); i = 0; // console.log("> i=" + i); } else { i += 1; c += 1; } } return c; } function process_input () { rl.on('line', (input) => console.log("> " + process_line(input))); } process_input();
#Javascript #Advent of Code #Programming