From f16dccdee6e048fb79431db9565740c552936b83 Mon Sep 17 00:00:00 2001 From: Alfred Melch Date: Thu, 3 Dec 2020 10:22:42 +0100 Subject: [PATCH] Solve 2020/03 --- 2020/03/example | 11 ++ 2020/03/input | 323 +++++++++++++++++++++++++++++++++++++++++++ 2020/03/solution1.js | 25 ++++ 2020/03/solution2.js | 35 +++++ 4 files changed, 394 insertions(+) create mode 100644 2020/03/example create mode 100644 2020/03/input create mode 100644 2020/03/solution1.js create mode 100644 2020/03/solution2.js diff --git a/2020/03/example b/2020/03/example new file mode 100644 index 0000000..7e88cdc --- /dev/null +++ b/2020/03/example @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# diff --git a/2020/03/input b/2020/03/input new file mode 100644 index 0000000..a41b21f --- /dev/null +++ b/2020/03/input @@ -0,0 +1,323 @@ +...#...###......##.#..#.....##. +..#.#.#....#.##.#......#.#....# +......#.....#......#....#...##. +...#.....##.#..#........##..... +...##...##...#...#....###....#. +...##...##.......#....#...#.#.. +..............##..#..#........# +#.#....#.........#...##.#.#.#.# +.#..##......#.#......#...#....# +#....#..#.#.....#..#...#...#... +#.#.#.....##.....#.........#... +......###..#....#..#..#.#....#. +##.####...#.............#.##..# +....#....#..#......#.......#... +...#.......#.#..#.........##.#. +......#.#.....###.###..###..#.. +##..##.......#.#.....#..#....#. +..##.#..#....#.............##.# +....#.#.#..#..#........##....#. +.....####..#..#.###..#....##..# +#.#.......#...##.##.##..#....#. +.#..#..##...####.#......#..#... +#...##.......#...####......##.. +...#.####....#.#...###.#.#...#. +....#...........#.##.##.#...... +.....##...#.######.#..#....#..# +.#....#...##....#..######....#. +...#.....#...#####.##...#..#.#. +.....#...##........##.##.##.### +#.#..#....##....#......#....#.# +......##...#.........#....#.#.. +###..#..##......##.#####.###.## +#.....#..##.##....#...........# +##..#.#..##..#.#.....#......#.. +.#.##.#..#.#....##..#..#....#.. +.#......##..##.#...#..#.......# +#....##.##..###..###......##.#. +....###..##.......#.###.#....#. +..##........#........##.....#.. +.#..#.....#...####.##...##..... +....#.#.#.....#.##..##.....#..# +..............#.....#...#.....# +.#.....#......###...........#.# +.....#.#......#.##..#.......... +.#......###............#.#.##.. +.#.#....##.#..###.....#.##..#.# +.......#.#.#..#..#..#...##..#.# +.#.###...##.#.#.####.#.#...#... +...#.#....#......##.##.#....... +#...#.....##....#........##.... +.....###...#.##.#......##.#..#. +..#...##.##.###..#..#......#### +.#.##.#..#.##..##..........##.. +..#.#.#..#.#.....#...###.....#. +..#..#.#....#.##.............## +.......#..###..#.#...#.....##.# +####.#.#......#..#.##.........# +..........#.....#..##......###. +..#..............#...#..##..... +......#.#.#..#.##.....####..##. +.##.#..#.#....#.......#..#..... +..#..#..#.##.#....###.#.#.#.#.# +.....#....#......###..#........ +#.#.#..#...###.....#......#.##. +...#.#....#.#......#........#.. +..#...###.#...#..#....##...#..# +.###.##..#..#...###.#..#.####.. +#....#..##..##..#......#...##.. +#.#..#...#..#...###..#.#.##.... +##....#....##.####...#.#.###... +##.#...#.......#.##.##....#...# +..#.#..........#..#.#.#....#..# +..#........#...#....#....#....# +..#..#....#.......#........#... +......#....#....##.#....#.#.##. +.##...###.##.##....##.#...###.. +.....##..#.#.....###..#.....### +....##.#.##...##..##........#.. +#...#..##.#.#....#......#...#.. +.###.##.#........#.####....#... +#.##.....#..#...#..##.##..#.#.. +.....#.#..#....#..#...##.##.#.. +.#......#####...##...#.#.###.#. +#......##....#.....#......##.#. +#.#.##.###.#......#####..#..... +........###.#...#..#.#........# +....#....###..#.##.#...#....#.. +..........#..#.#....#...#.#...# +#.##......###.#.#.#....####...# +...#.#....#........##.#.....##. +.....##..#.#.#..###...##...#... +#...#...#....#....##........#.. +.....#.........##.#......#..#.. +#.....##..#.###.....#....##.##. +...#..#..#.#........##...##.#.# +..#..##.###.....#.#.....###.##. +..###.........#...##.....###... +...###...##.#...##....##....... +.#.#..#...###..#.#....#....#... +##..#.......#....#.#...#..#..#. +..#......#....####..##..#.###.# +..#.......##........#.#.#..#... +.#.......#.##.#.##.#.......#..# +###...#...#...#...#..#...#...## +..#..........#..###........##.. +.##..#....##......##........#.# +......#.##......#......##...#.# +.#.#....#.#.#........#......#.. +.#.#..#....####..##...##....#.. +.#...##..#..#..#####....##.#... +.##.#.#...#...#.#...#.##.#...#. +###.#...##..#.###.#.....#.##.#. +#.....#.###.#.#...#..#....#.#.. +..##..#....#......#.........### +.#...#...##......#...#.####.... +..#.##...##..............#.#..# +..#......#..##...........#..#.# +..#####...#..#.......##...###.. +..##..#....#.#...###.#...#..... +..#....#..#.#.......#..#.#.#... +.##..#.#.....##....#.......#... +...#.#..###...##....#....##..#. +.....##..#...##.####....##...#. +.......#.........#...#.##..#### +........###..#..#.##.###..#.... +.#.#..#.##.##.........#...#.... +.###......#.....#....##....#### +.##..##...........#.....#.....# +#.#.#.#.#.#.##..#.#.#..#.##.... +.##.##...##..#....##..###..#### +#..##.#......#...###.........#. +#..#...#..##..#..##.....##...#. +#...##..#...##.#.###.#...#..... +.###.....#.......#...#.##.###.# +..........#.#......#...###...## +..##....#.#..#....#.###...#..## +#.#..#....##.##..##.........##. +#.....#.##.###.#..#....##...#.. +...#........##...#..###..###### +#..#.#.#.#...#....#....###.#..# +...##.##.##.....##..#........#. +..#.#....#..#.......#...##.##.# +###.##.......##..#.####...#.#.. +....#.#.....##.....#.#.##...#.. +.#..##..#.....#.#..#...#..#..#. +.###....##...#......#.....#.... +##.##.###......#...#...###.#... +#...#.##...#.#....##.....####.. +#.#.#.#...###...##............. +..#....#.....##.#...#......#... +....#...#......#...#..#...#.#.. +.###..#.....#..#...#....#...#.. +..#...#.#..###.......#..#.#...# +#...###.##.....#....#..#.#..##. +...#.##.#.##......#.#.#.##..... +........####...##...##..#....#. +.#.#....#....#.#...##.###...##. +#.#...###.#.#.#....#....#.#.... +.####.#..#.#....#..#.#..#..#... +#####...#...#...#....#.#.#..##. +..###.##.###...#..........#.##. +##.....#...#....###..###.#.#.#. +#..##.#..#..#..#...#.#...###..# +##....#.#...##......#.#...#...# +.##..........#.#....#...#.##..# +....#....####.#.####......#.### +..##.#.....####.#.####.......#. +#.##.##.#.....#.##......##...#. +......###..#.....#.....##...... +..#..#....#.#...#.....#......## +##..#..#..###.#.#..#..##.....#. +#....#.#.....#####...#.#....... +.....#..#....#.#.#..#...#...#.. +............#.##......##.##.#.# +....#...#.#........###....#.... +..#..#...###.#....##..#..###.## +#.##....#...#.....##..#.##.#... +...##..###...#.#.....##.#...... +.#..#.##.#####..#.......#..###. +...#.##...###.....####.#.#..... +.#......##.#.#.#.#.##.#.....#.. +..#.##.#..##.......#.#.....##.. +..................#....#...#... +.##.#..#.#.#..#.......#.#..##.# +....#........#......#..####..#. +#...#...##..##.#..#.......##... +#..#..#..#..#........####..#.#. +..#.#......#..#.##.##.#.#...#.# +...#..#......#.#.###.#.##..##.. +..#...##.....#..#...##..#.#.... +#.........#....#..#....##.##..# +..#..#.#....#..#...#.##.....#.. +...#..#...#.#.....#..##..#.#... +....#........#.#....##..##..#.. +#.....#.#..#.......#.##.....#.. +.#.###.###...##...##..###...#.. +.##.##.......#.#......#.....#.# +...#....##....#..#..........#.# +..#.##.........#.#.....#.....#. +...#.##..##.......##..##...#... +#.##......##.##..#.....##...##. +#.#.#..##...#.#............#.#. +....#.....#......##...#.#.....# +...#.#......#.#...###.......#.. +......##..###....#.#...#.#####. +..#..#.#.#...##.#...###..##..#. +##.##.#.#.##.#..#....#...#...#. +#..#....######.##.#...#...#.#.. +.#..#.##....#..#.#.......#....# +....#....#......##....##.#.#... +.###......#..#..#.......####..# +.#.#.....#..###...........##... +.##..##.##.....####..#..#..##.. +..#..##.#......#...###.##..#.#. +....##..#.....###..#.##....##.# +#..#......#....#.........#..... +.#...#.....#.#..#..##....#..... +.##..#...#..##.#..#...........# +..#..##........##.......#..#... +#.....#....#....#.#.#...##.#... +...#...#.#.#..#.##.#.#...#..... +..#..#.#....#....###....#.#.#.. +...###..#...#..#....#.....#.... +...#...#.#..#.....#...###...... +##......#..........#.#.#..#.#.# +....#.....#.....#..#..#.#.#.#.. +...####...#.##...#.#..#....#.#. +#.##........##.............#.## +#.#.#.#.#.....................# +.#.###....#..##.##.##....#..... +#.#...#.####.###...#..#..#.#... +.##...#..###.......##..#.#..... +#.#.#.#...#...#.##.....#....... +.##.#.#.#......####..#.#....... +###..........#......#...##...#. +.........##...#.##...#.#....... +...#.#.....#...#..#...#..##..#. +.#..###...#.#.........###....#. +##..#...#........#.........##.. +.....#...#.#...#.#.#........... +..#....##...#.#..#..#.##....##. +.##....#.#.....##.#..#..#...##. +..##......#.#...#.#.......##.#. +##...#..#...##.#........#.##... +#......#.##..#.#..#.###.......# +#.#...#.....#.#......#.#.#..... +#.....#..#.......#....##.#.#..# +###.#....#..##.#.##....#....#.. +#.##.##....#.#..#.#...#....#... +####...#####...#.....#....##.#. +....#.#...#.#.##.#.#.##.#.#.### +#.....##.#.....#.#......#.#..#. +.#....##.#..#........#...##.#.. +......#...#....##....##....##.. +..###.....#....##.#...#..#..... +#....##...##.##..##.#...#...#.. +#..#...#...#.#....##..#.#....#. +......................#.....#.. +.#..#...#.........#....##...### +.##.#.#...##...#...#...#..#.... +.#.###....#.#............##..#. +###..##.#.#.#.#....##...#...... +.##................####...##.## +.#.#.........##....#.#.##.##.#. +....#...#...#...##...#.##.#..#. +.#.#........#..##.....#..#....# +##.#..#.#....#.....#...#...#... +.#...##....#.....##....#..#.#.# +####.....#..#....#......###.##. +##..##.#....###.....#...#...... +.##.#...#.....#.#..#.#..#.#...# +.....#.#..#..#..###.#...###.#.. +.#.#.##.#..#.#..#...#..#....... +..#.....#....#.##.##.##.......# +.#..##....###...#.............. +#....#...#.#.......#....##.#... +....#.#..##.#....#..#.#....#.#. +#.........##...#.#............. +#.#.......##.....#...##..##.#.# +.......#....#..##...#..#######. +.#.#...##........#..#.....#.#.. +.#.......#..#......#.##.##...## +.........#............#....#..# +.#......#...##...##...#....###. +.........#...#.#.###.......#... +###.#..#.#.#.#......##...#.#... +.#.........##.#....###....#.#.. +#.#....#..#.##.#..#....##...#.. +###.#...#..#..##........#.###.. +.....#....#..#........#..#.##.# +..#.....##......#....#..#.#.#.. +.#.........#.....#.....#....... +......#....#.###..#.#....#....# +..#.#..#.#.###.........#..#..#. +..#..#.#.#.........#....##.#.#. +#.......#........##...##....#.. +##..#..#...###...#..##..#..#.#. +##..#..#....#.#..##..#..#.#..#. +..##.....##.#..#.#........###.. +..#.#..#..##........#...#....## +.##..#....##..#..#..#..#.#....# +#....#.....##........#.....#.## +......#....#.#..#......#.##.... +.......#..#..#......##......... +......#...#..##.....#......#..# +#..#..#....##....#........##..# +##....#...#.#.....#####........ +...#.#..#.#.##.#.##..##...#.... +..#..#..#..#..#....#..#..##...# +.#.....#....##.##....##.....#.. +#...#.....#.....#.#...#.#....#. +.###...#..##....#..#...#.###... +....#..##..#.......#.##.##..### +#.......##.....#.......#.#...## +#.....#.#.#....#.#......#.#.#.. +..##.....#..###......##........ +.....#...#..##....#......#..... +#..#..#....#.#...#..###.......# +.....#.....#....#..#...#.#..##. +#####........#...#..#..##..#.#. +.#..#...#.##....#.#..#......### +#.###.#..#.....##..##....#...#. +.#...#.#####....##..........##. diff --git a/2020/03/solution1.js b/2020/03/solution1.js new file mode 100644 index 0000000..1c47635 --- /dev/null +++ b/2020/03/solution1.js @@ -0,0 +1,25 @@ +const fs = require("fs"); + +function setCharAt(str, index, chr) { + if (index > str.length - 1) return str; + return str.substring(0, index) + chr + str.substring(index + 1); +} + +const input = fs.readFileSync("input", "utf-8"); +const rows = input.split("\n"); + +console.log(rows.slice(0, 10)); + +let countTrees = 0; + +let curX = 0; +for (let curY = 1; curY < rows.length; curY++) { + curX = (curX + 3) % rows[curY].length; + const row = rows[curY]; + const field = rows[curY][curX]; + rows[curY] = setCharAt(row, curX, field === "#" ? "X" : "O"); + if (field === "#") countTrees++; +} + +console.log(rows.slice(0, 10)); +console.log(countTrees); diff --git a/2020/03/solution2.js b/2020/03/solution2.js new file mode 100644 index 0000000..0122c9f --- /dev/null +++ b/2020/03/solution2.js @@ -0,0 +1,35 @@ +const fs = require("fs"); + +const input = fs.readFileSync("input", "utf-8").trim(); +const rows = input.split("\n"); + +console.log("Num of rows:", rows.length); +console.log("Column width:", rows[0].length); + +function run(dX, dY) { + let countTrees = 0; + let curX = 0; + for (let curY = dY; curY < rows.length; curY += dY) { + curX = (curX + dX) % rows[curY].length; + const field = rows[curY][curX]; + if (field === "#") countTrees++; + } + return countTrees; +} + +const runs = [ + [1, 1], + [3, 1], + [5, 1], + [7, 1], + [1, 2], +]; + +let solution = 1; +for (const args of runs) { + const countTrees = run(...args); + console.log(`Right: ${args[0]}, down: ${args[1]}: Trees: ${countTrees}`); + solution *= countTrees; +} +console.log("Solution:"); +console.log(solution);