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);