|
| 1 | +import fs from 'fs'; |
| 2 | + |
| 3 | +// https://adventofcode.com/2022/day/9 |
| 4 | + |
| 5 | +let testInput = fs.readFileSync('./testData.txt').toString(); |
| 6 | +let inputData = fs.readFileSync('./input.txt').toString(); |
| 7 | + |
| 8 | +console.log('test_actual OK: ', part1_correct(testInput, 1) === 13); |
| 9 | +console.log('answer_actual: ', part1_correct(inputData, 1), [6018]); |
| 10 | + |
| 11 | +let test2Input = `R 5 |
| 12 | +U 8 |
| 13 | +L 8 |
| 14 | +D 3 |
| 15 | +R 17 |
| 16 | +D 10 |
| 17 | +L 25 |
| 18 | +U 20`; |
| 19 | +console.log('test2 OK: ', part2(test2Input) === 36); |
| 20 | +console.log('answer2: ', part2(inputData), [2619]); // 491 is wrong???? correct 2619 O_o |
| 21 | + |
| 22 | +function part1_correct(inp) { |
| 23 | + return correct_approach_actually(inp, 1); |
| 24 | +} |
| 25 | + |
| 26 | +function part2(inp) { |
| 27 | + return correct_approach_actually(inp, 9); |
| 28 | +} |
| 29 | + |
| 30 | +function correct_approach_actually(inp, ropeLen) { |
| 31 | + let history = new Set(); |
| 32 | + let rope = Array.from({ length: ropeLen + 1 }, () => { |
| 33 | + return [0, 0]; |
| 34 | + }); |
| 35 | + let delta = { 'U': [1, 0], 'D': [-1, 0], 'R': [0, 1], 'L': [0, -1], }; |
| 36 | + |
| 37 | + // fs.writeFileSync('./out.txt', ''); |
| 38 | + |
| 39 | + inp.split('\n').map((line) => { |
| 40 | + let [direction, steps] = line.split(' '); |
| 41 | + steps = +steps; |
| 42 | + // fs.appendFileSync('./out.txt', `${line}\n`); |
| 43 | + |
| 44 | + while (steps--) { |
| 45 | + rope[0][0] += delta[direction][0]; |
| 46 | + rope[0][1] += delta[direction][1]; |
| 47 | + |
| 48 | + for (let i = 1; i < rope.length; i++) { |
| 49 | + rope[i] = follow(rope[i], rope[i - 1]); |
| 50 | + // renderHistory(50, 50, [15, 21], history, rope); |
| 51 | + } |
| 52 | + history.add(rope[ropeLen].join(',')); |
| 53 | + } |
| 54 | + // renderHistory(50, 50, [15, 21], history, rope); |
| 55 | + }); |
| 56 | + // renderHistory(13, 30, [5, 11], history, rope); |
| 57 | + |
| 58 | + return history.size; |
| 59 | +} |
| 60 | + |
| 61 | +function lengthBetween(a, b) { |
| 62 | + return Math.max(Math.abs(a[0] - b[0]), Math.abs(a[1] - b[1])); |
| 63 | +} |
| 64 | + |
| 65 | +function follow(tail, head) { |
| 66 | + let res = [...tail]; |
| 67 | + if (lengthBetween(tail, head) < 2) return res; |
| 68 | + |
| 69 | + if (tail[0] === head[0]) { |
| 70 | + if (tail[1] < head[1]) res[1]++; |
| 71 | + if (tail[1] > head[1]) res[1]--; |
| 72 | + return res; |
| 73 | + } |
| 74 | + |
| 75 | + if (tail[1] === head[1]) { |
| 76 | + if (tail[0] < head[0]) res[0]++; |
| 77 | + if (tail[0] > head[0]) res[0]--; |
| 78 | + return res; |
| 79 | + } |
| 80 | + |
| 81 | + if (tail[0] < head[0]) { |
| 82 | + res[0]++; |
| 83 | + } else { |
| 84 | + res[0]--; |
| 85 | + } |
| 86 | + |
| 87 | + if (tail[1] < head[1]) { |
| 88 | + res[1]++; |
| 89 | + } else { |
| 90 | + res[1]--; |
| 91 | + } |
| 92 | + return res; |
| 93 | +} |
| 94 | + |
| 95 | +function renderHistory(height, width, focus, history, rope) { |
| 96 | + let field = Array.from({ length: height }, () => { |
| 97 | + return Array.from({ length: width }, () => { |
| 98 | + return '.'; |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + history.forEach((cell) => { |
| 103 | + const [x, y] = cell.split(',').map(it => +it); |
| 104 | + |
| 105 | + if (field[x + focus[0]] && field[x + focus[0]][y + focus[1]]) { |
| 106 | + field[x + focus[0]][y + focus[1]] = '#'; |
| 107 | + } |
| 108 | + }); |
| 109 | + |
| 110 | + rope.forEach(([x, y], i) => { |
| 111 | + // const [x, y] = cell.split(',').map(it => +it); |
| 112 | + |
| 113 | + if (field[x + focus[0]] && field[x + focus[0]][y + focus[1]]) { |
| 114 | + field[x + focus[0]][y + focus[1]] = i; |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + field.reverse(); |
| 119 | + |
| 120 | + let res = field.map((row) => { |
| 121 | + return row.join(''); |
| 122 | + }).join('\n'); |
| 123 | + // console.log(res); |
| 124 | + |
| 125 | + fs.appendFileSync('./out.txt', res); |
| 126 | + fs.appendFileSync('./out.txt', '\n'); |
| 127 | +} |
0 commit comments