From ba3a7d27a07103b5979836c307ceb64ccb8acdf6 Mon Sep 17 00:00:00 2001 From: Jan <26145882+imverum@users.noreply.github.com> Date: Thu, 3 Dec 2020 23:09:19 +0100 Subject: [PATCH] day 3 --- day_3/input.txt | 323 ++++++++++++++++++++++++++++++++++++++++++++++++ day_3/task_1.js | 17 +++ day_3/task_2.js | 33 +++++ 3 files changed, 373 insertions(+) create mode 100644 day_3/input.txt create mode 100644 day_3/task_1.js create mode 100644 day_3/task_2.js diff --git a/day_3/input.txt b/day_3/input.txt new file mode 100644 index 0000000..eda00f8 --- /dev/null +++ b/day_3/input.txt @@ -0,0 +1,323 @@ +........#..#.##.#.............. +...#...............#.#......... +...#..#...#..##....#........... +...#.............#....#.....#.. +..#......#..#...#.......#...... +..............##............... +#.......#.........#......#....# +.#.....###.....#...#.#.#...#... +#.....................#....#.#. +.......#...................#... +...#.#...................#....# +....#....#.......#...#......... +..##.#............#..#......... +.....##.#..............##..###. +...........#....#....#......... +#.....#...#...#.#.#.#.##.#...#. +.#...............#....##....... +.....#..#......#....#.......##. +.....#........#.......#........ +...#...##...#..##...#.....##... +.....#.........#.###...##...#.. +.#.##...#........#.#.#.#....#.. +....#......##.#...#.....#....#. +.......###..........#..#..#.... +......#...#.##................. +....#...#...#.........#......#. +.....#...........#...###....#.. +.....#...#.#.#....##.#......#.# +......#...#.....#..#..#........ +#......#..#...##........###.... +##.....#....##..#.#.###.#...#.. +........#....#.......#.....#..# +#.#.#.##.#.#................... +..#...##....#......#.....##.... +.......#.##..#........##..#.... +.#.#....##......#.#..........#. +#..............#............#.. +.#.#.#.#.#.####.#.#...##....... +.#..#.....##.#.......#.##...#.. +..#.#........#.............#.#. +..#.#..........#..#........#... +..#..#...#.......##...#.#....## +...#.....#.#.#.....#....#....#. +.#...#......#.....#..##........ +...#.......##.#.#.....#......#. +...........#.....#.#.......#... +#...........#...#..#.#........# +....#......#..##........#..###. +.#..#........................#. +#.......#......#...#...#..#.... +....#.#...#..#.#....#....##.#.. +.....#......#..#..........##.#. +.#.....#...........#.........#. +...###.#...#.......#.#......... +.......#....#..........#..#...# +......##..#.......#...##....... +..#..........#.......#......... +..........#..#..#..#..#........ +.#.................####...#.#.# +..##.....#............#........ +....#.....###...#......#....#.# +...##.#...........#.##......#.. +#..##..#..#....#...#..#........ +......#....#........#.......#.. +......#.....#......###......... +.#.....#.#......#.......#...... +..#.........#..#..#........##.# +..#.#....#.....#....##....#.#.. +...#.............##............ +........#..#..#......#...#..... +.....#.#...#...##.....#.....#.. +.#..#.#..........##...##.....#. +......##.#..........#...#.....# +#.#.##......#....#..........#.. +................#.......#.##... +#.......#.....#.......#....#... +#..#.....#.##..##...........#.. +.....#....#.#.##..........#..## +#.......#.....#.##...........#. +........#.##........###..#.#... +........#..................#... +#.........................#...# +....#.........#...#.#..#.....#. +.#............#....#........... +..#.#...#..##...#.#.......#.... +.#.#....#...........#.........# +...#.#..........#.....#...#.... +......#....#.#...............## +....##......###...##.##.....##. +............#.#....#.#.....#..# +.....#..#.....#.#...###....#... +.......##....##..#...##..#...## +.....#.......##..#...#...#....# +#.........##....#........###.#. +...#..##...#...#.........#.#.#. +....#.#.....#.....#............ +#........#....#..#........#.... +.......#....#...#.............. +#...#.........##.....###.#..... +.#....##..#...#..##.........#.. +....#.....#......##..#..#....#. +#.#..#.........#........#...... +..#.......#.........#.....###.. +..#..........#...........#....# +..#...............#......#..#.. +....#..#...#....###.....#..#..# +#...#...#..#...........#....#.. +.#....#.#..#....#.#...........# +.....#.....#..#....#..#....#... +#.#..#...........#.#........... +..................#.#.......#.. +...#.........#.....#..##....#.. +.........#.#...#.........##.... +...#..#....#.....#...#..#...... +.#.##.....#....#....#......##.. +##..#.........#.#....#...#..... +#......#.#...#....#.#..#....... +.......#.....#.....###....#.#.. +.#....##.#.....#...#.......#... +.#.......#..#...#......#..#..## +...............#...#........... +#..............#....#.#.#....#. +...........#..#.......#.##..#.. +..#......#.#....#...#.#.....#.. +#.............................. +#..#....#..........#...#....... +......#.............#####...... +.#...###......#.#.#.##..#...... +............#.##.....#......... +.........#....##....#.......... +###....#......#.......#........ +.#.......##..........#..#....#. +#..#.....................#....# +........#...........#.......... +..#..........#...#..#.........# +..#..#......##................# +.....##..#...#..#.............. +.......#...##..#............... +.......##..#.####....#....#.#.. +#.#..#..........#........##.... +....##....#.#..#....#.#...#.... +......#.......#...#.....#...#.. +..#..#...#.....#.......###..... +...#.......#.#.#.......#.##.... +...............#..#.#........#. +.#....###.#......#............. +.#..#...#....#.#..#.....#...... +.......#.##....#.#.##.##...#.#. +..#...#....#.#..##.#.....#...## +..#...#......#...#......#...#.. +....#..#...#.#..#......#....... +#..#...............#......#.##. +.#....#...#..........#.#.....#. +.#..#.#.#................#..#.. +.#....#.#...#..##.###..#...###. +#.............#.....#.........# +...#.........#...#.......#..#.. +......#..#.........#..........# +........##................#..#. +......#...#.#.....#......##.... +...............#...#....#...... +...#.#..#..#.....##.###..##..#. +.#....##......#...#..##..#..... +.....#.........##.##....#...#.. +.....#.#..................####. +#.....#...#.............##....# +#.#..........#...#..#..#....... +#..#.#.........#............... +....#...#.........#...##....... +...........#.....#..##..#...... +#.....#.......#.#........#..... +..##..#.....#...##......#...... +....#....#..................... +............#......#.........## +.....##.............#.....##..# +.......#.............#..#.#.##. +.###...#......#..#........##.#. +..#.#...#.#....#.....#..#...... +..#.#..#.##........#...#....... +........#.#...............#..#. +........##.......#...#.......#. +...#........##.#..........#.#.# +..#..###.#.#.......#.#......#.. +....#..........#...#..#........ +...#..#...#...#.#....#...#..#.. +...#...#........#......##...#.# +#...........#..........#..#.##. +...#..##..................#.#.. +...##.#...#....#.#...#.####.... +.....#...#.#.#..#.............. +.....#..#.#.#..#............... +..#..#..##...#.#..#.....##....# +.......#.#..#.....#....#....... +...#..#....#.........#...#..... +..............#.#...#...##..... +...................#........... +.#......#.#...................# +.##.....#........#.........#..# +.##..##...#...................# +...#....#.#..#.#.#..#.....##... +.......#..#....#......####.#... +.##..#..##....#.......#........ +.#...#...........##............ +.....#.....#........#.......... +....##..#....#.....#........... +.#...#....................#.... +....#.........#.......##.....#. +.#....#..#.....#.##....#....... +....#..#.........#.#....#.#.... +.......#.........##....#....... +..#......#....#....#...#....... +........#..#.......#.##......#. +..#.....#......#...#..#.......# +#..#.....##...#...#............ +.......##.......#........#...#. +..#......................#...#. +....##.#.............#......#.. +#.#............................ +...##.#.....#.#............#.## +......#...#..#.........##...... +.#.......#.....##.......#.#.... +...........#.#.........#..##... +...#..........#.##....#........ +........#..#..#...#....#....#.. +........##....#.#....#........# +..#........##....###....#...... +#................###...#...#... +................#.#..###......# +..#.....##.#................#.. +.....#...............#..#...... +..#.......####.....#..#.#....## +..#.....#..#....#.............. +#.#...........#.#.....#..##.... +#.#..........#.......#...#.###. +........#....#...#..#.#........ +.#.....#......#..#..#..###..#.. +.#.........#.##.#.#......##.... +..#.........#...##..#........#. +.#...................#......... +...#.#........#................ +............#.....#..##........ +..#.....#.#......#.......#...#. +........#....##..##...#.....##. +.#........#.#....#.#....#.#..#. +#.#.......#.................... +.#..#...##.........#..#........ +.........#...............#..... +...#...#.....#......#.......#.. +###......................#.#..# +...#.....####........#..#.....# +#.#...#.#...................##. +.........#..................... +#..........##..#.....#....#.... +.......#...#.#.##.#..##........ +..........#..#.#..#.#.......#.# +.....................#.#...#... +...........#.#........#.#.#.... +.......#......#........#...#.#. +.........#....................# +.##.##....#...#.#.#.#.......... +#....##..#.##....#....#.......# +.##.#...#...............#....#. +.......#...#.###....#.......... +.....#....#...#..#............. +#.........#.##....#.#.#........ +..#...#.............##..#..#... +#..##.......#..........#...#.#. +.#..#.....#...........#......#. +......#......#..............##. +.#...#..#...#..####.....#.....# +....##.......#..........##..... +.#.....#.......#.....#.#...#... +..#..#..#.#...#......#......... +......#.#....#........#.......# +........#.......#.............. +..#...#.#....#........#.......# +............#....#...##.#...... +.........#.............#..#.... +#.............#.#..##.......#.. +#....#...........###....#...... +...#.....................#..... +....#.#..........#...#.......#. +......#..#.......#...#...#....# +.#.#..#.....##.#........#...... +...........#...#.#............. +...###............#...#..#..... +..#.#.......#...#.#..#......... +.#......##...........#.....#.## +.....##.....#....##...##.#.#... +..........#.#.#......#........# +..#.#........#....##....#.#.... +.#....#...##...........#....#.. +##......#...#.......#.......... +.##...###..#...#......#..##.#.# +...........##.#..##...#.......# +..#..............##............ +........#..#........#...#..#.#. +..#.............#......#...##.. +#...##....#...#....#....#.#.... +.#.#......#..##............#.#. +.....###.#....##....#....#..... +#.#.#..........#...#...#.#.#... +.....#.#...........####........ +.....#....##...#.##..#......#.. +#....#.......#.##.......#..#... +.....#.....#........#.......... +.......#.......#...#.##......#. +...#.........##...#.#.#......## +#........#........#...#..#..... +.#......#.#......#.#...#....#.. +#..#....##.....##.............. +...#.##............#..........# +.....#.#....#..#.#............# +..#......#...###.##.......###.. +........#....#.#.#.#........... +............#..#........#.....# +....#...............#.......... +......#....#....###..#.......## +#...#...##....#.........#...#.. +...........#.#.............#... +...#..#.....#..##.#....#......# +..#...#..#...#......#.......... +....#..#....#.......#........#. \ No newline at end of file diff --git a/day_3/task_1.js b/day_3/task_1.js new file mode 100644 index 0000000..227c40d --- /dev/null +++ b/day_3/task_1.js @@ -0,0 +1,17 @@ +const fs = require('fs'); +const file = fs.readFileSync('./input.txt').toString('utf-8'); +const arr = file.split('\n'); + +let trees = 0; +let row = 0; +let pos = 1; + +while (row < arr.length) { + const currentRow = arr[row]; + // console.log(`${currentRow.slice(0, pos - 1)}${currentRow.charAt(pos - 1) == '#' ? 'X' : 'O'}${currentRow.slice(pos - 1)}` + ' ' + pos + ' => ' + currentRow.charAt(pos - 1)); + if (currentRow.charAt(pos - 1) == '#') trees++; + row += 1; + pos += 3; + if (pos > currentRow.length) pos -= currentRow.length; +} +console.log(`Encountered ${trees} trees.`); \ No newline at end of file diff --git a/day_3/task_2.js b/day_3/task_2.js new file mode 100644 index 0000000..1885395 --- /dev/null +++ b/day_3/task_2.js @@ -0,0 +1,33 @@ +const fs = require('fs'); +const file = fs.readFileSync('./input.txt').toString('utf-8'); +const arr = file.split('\n'); + +const speeds = [ + [1, 1], + [3, 1], + [5, 1], + [7, 1], + [1, 2] +] + +let results = []; + +for (const [ rightSpeed, downSpeed ] of speeds) { + let trees = 0; + let row = 0; + let pos = 1; + + console.log(`Down ${downSpeed}, right ${rightSpeed}:`); + while (row < arr.length) { + const currentRow = arr[row]; + // console.log(`${currentRow.slice(0, pos - 1)}${currentRow.charAt(pos - 1) == '#' ? 'X' : 'O'}${currentRow.slice(pos - 1)}` + ' ' + pos + ' => ' + currentRow.charAt(pos - 1)); + if (currentRow.charAt(pos - 1) == '#') trees++; + row += downSpeed; + pos += rightSpeed; + if (pos > currentRow.length) pos -= currentRow.length; + } + console.log(`Encountered ${trees} trees.\n`); + results.push(trees); +} + +console.log(`Result: ${results.join(' * ')} = ${results.reduce((acc, cur) => acc * cur)}`); \ No newline at end of file