From b2027635c2344d25a1a93b1eba98a4d9008610da Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Wed, 2 Dec 2020 22:03:52 -0800 Subject: [PATCH] Completed day 3 --- 2020/Makefile | 3 + 2020/day3.cpp | 68 ++++++++++ 2020/day3.input | 323 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 394 insertions(+) create mode 100644 2020/day3.cpp create mode 100644 2020/day3.input diff --git a/2020/Makefile b/2020/Makefile index 89345cb..4ee74ce 100644 --- a/2020/Makefile +++ b/2020/Makefile @@ -4,6 +4,9 @@ day1.out: out/types.o day1.cpp day1.input day2.out: out/types.o day2.cpp day2.input g++ $(flags) day2.cpp out/types.o -o $@ +day3.out: out/types.o day3.cpp day3.input + g++ $(flags) day3.cpp out/types.o -o $@ + out/types.o: types.hpp types.cpp mkdir -p out/ g++ $(flags) -c types.cpp -o $@ diff --git a/2020/day3.cpp b/2020/day3.cpp new file mode 100644 index 0000000..4c6268b --- /dev/null +++ b/2020/day3.cpp @@ -0,0 +1,68 @@ +/******************************************************************************* + * + * Copyright (c) 2020 Gnarwhal + * + * ----------------------------------------------------------------------------- + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files(the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + *******************************************************************************/ + +#include +#include +#include +#include + +#include "types.hpp" + +auto read_field_and_count_trees(std::vector & trees, usize horizontal_increment, usize vertical_increment) -> u64 { + auto tree_count = u64(0); + + auto x_index = usize(-horizontal_increment); + auto y_index = usize(-1); + auto line = std::string(); + auto file = std::ifstream("day3.input"); + + while (getline(file, line)) { + auto vertical_check = (++y_index % vertical_increment == 0); + tree_count += vertical_check * (line[(x_index += (vertical_check * horizontal_increment)) % line.size()] == '#'); + } + + return tree_count; +} + +auto main(i32 argc, char ** argv) -> i32 { + auto trees = std::vector(); + auto tree_counts = std::vector(); + + tree_counts.push_back(read_field_and_count_trees(trees, 3, 1)); + std::cout << tree_counts[0] << std::endl; + + tree_counts.push_back(read_field_and_count_trees(trees, 1, 1)); + tree_counts.push_back(read_field_and_count_trees(trees, 5, 1)); + tree_counts.push_back(read_field_and_count_trees(trees, 7, 1)); + tree_counts.push_back(read_field_and_count_trees(trees, 1, 2)); + auto tree_count_product = u64(1); + for (auto tree_count : tree_counts) { + tree_count_product *= tree_count; + } + std::cout << tree_count_product << std::endl; + + return 0; +} diff --git a/2020/day3.input b/2020/day3.input new file mode 100644 index 0000000..6727b2a --- /dev/null +++ b/2020/day3.input @@ -0,0 +1,323 @@ +.....#............#....#####.## +.#.#....#......#....##......... +......#.#.#.....###.#.#........ +......#...#.....#####....#..##. +...#............##...###.##.... +#.....#...#....#......##....##. +#...#.#....#..#..##.##...#..... +.......#..........#..#..#.#.... +.#.....#.#.......#..#...#....#. +#..#.##.#..................###. +...#.#.##...##.###.....#..#...# +..#.#...#............#.......#. +#..#.#..#.#....#...#.#.....#..# +#......##....#..#.#.#........#. +....#..#.#.#.##............#..# +....#..#..#...#.#.##......#...# +##...#...........#.....###.#... +..#...#.#...#.#.....#....##.##. +....##...##.#....#.....#.##.... +#........##......#......#.#.#.# +....#.#.#.........##......#.... +.#......#...#.....##..#....#..# +....#..#.#.....#..........#..#. +..##...#..##................#.# +.....#....#.#..#......#........ +........#..#.#......#.#........ +.....#.#....##.###....#...#.... +...##.#.......#....###..#...... +............##.#..#...#........ +#..###..#.....#.####........... +.......##.....#......#......#.. +#........##..#.....##.......#.# +#.##...#...#...#......##..#.#.# +......#....##.#.#...#...##....# +#..#....##.#......#.......##... +.#..........#..........#....#.# +#.....##......##....#.......... +..#.#.....#.#...#........#..... +...#........#..#..#.##..##..... +......###.....#..#...#.###...## +.##.##.......#.......###...#... +#.#..#.#.#....#.....###..#...## +......#.##..........#.......##. +#..#.#.........#.....##...##... +..#...#....#....###.#......#... +.....#..#.######.....#..#.#.... +..#.#.....#.....##.#....##.#.## +...#.#.#....#....##..#..#.#.##. +...........#.#...#..#..####.... +.........#####.#.#.#...#.##.#.. +.......#...#......#.##.#.##.... +....#.....#.....###..........#. +.#.###....##.#..#..........#... +#...#.........##.....####....#. +##....##...#..........#........ +...#.#.#.#....#..........#..... +.......#....#......##.......#.. +.#.#..#.........#.#.##....#.... +..#.............#..##.#.##..### +.#.##..............#..#..##..#. +..##.#..#......#...##..#.##...# +......#..#....#....#....##..#.. +...#...##.............#..###... +...##....#.#.##........#.....## +....#.#.......#..###..#....#### +#...#...##..#.####..#...##....# +.......#..#.##..#...#.#........ +###.#......#..##..#......#.##.. +#....#............#.....#...... +..##...#..##......#..#....#.... +.#..##...#....#.#...#...#..#..# +........#....###...#..##..###.# +.........#....#....#..#.#.#...# +.#....###.##...#.#...........## +..#..#.#..#.#.##..#...##....... +##..#.#.#....#...#..#.......... +#..#.......#....#..##...####... +............#.#..........##.##. +#...#..#.#....#..#.#....##..... +......#...#...#.##............# +#.....##..###..#.#..#.#.##..#.# +#..#.#..#......#.......##.#.... +##..#.#..#...#......#.##...###. +.#....#..............#....#.#.. +..#.#..##....#....#..##........ +.#.#...#..#.....#.#..##........ +.....#..#.#......#....#.#..#.#. +....#.###...###.#.#.....#...... +...........#.#....##....##..... +..#..#.##..........#...#...#..# +.....#.###.#..........#........ +....#....##........###...#..... +.#.....##.......#....#..##..### +#.....#...............##......# +#..#.#..#.#.#.....#.#...#...... +.##.###...#....#..........##... +.#.......#..................... +.#.#....#...##..#...#...##..... +.#.#...#.......#.......#...#... +....#.#..#.#..#...#....##...... +....##.....#.##....#.##..##..## +..#............#...###.##..#... +.#..#.........#.##....#....#..# +.#..##..#..#........#.#.##.#.## +.###.#...#...............#...#. +...#.##.##.#......#...#....##.# +#......##.......##...###....#.# +#..##.....##......#.#.##....#.# +...#.#....#.#.#...........##..# +#.....##......##.#..........##. +###....#.#...#.#..####......... +.##.#.#...##..#.....#..#...#... +#.....#.#......#..........#.#.. +..###.##.#...................#. +#.............#..#........#.##. +#.#.#.#..#.....##..##.#....#... +...#...#..#...#..##..##........ +...##...##..#...##...........#. +.####..#.#.#.##.#.......#...... +...#....#.......#......#....... +.....#.#...#...#..##..#..#..... +......#.....###.#..#..#.#..###. +.#....#....#..#..##.....##...#. +.#.............##.###.#...#.#.. +#..#..#......#.###............# +##.#..##....#..........#.#.#... +......#........#...#.......##.. +....#.#..#..........#.....#.#.. +...#..#...#.#...#........#..... +.....##...#....#.........##.##. +....#...#...#.##.##...#....#... +.#..#.....##......#..#.#..#.... +........##...##.##......#.#.#.# +.................#..#.....##.#. +...#.....#...#.........#..#.#.# +....##.#.....#........#...#..#. +#...............#..#.....#...#. +.....#..#....#...#.####.#.#.... +####.#..#.##...#....#...##..... +#...##..#...####..#....#.#...#. +..#.......#.##..##...#.#....... +...........##.......#....#..#.. +#.##....#...#.....#....##...... +....##.#.......#..#...##....... +...#.........##.#..#......#.### +.#..#..#....#.#.##....###..###. +....#.#........##........##.... +....########....#.#.#.###.#...# +...#.###.###.##......##.......# +.#...#.###.......#..........#.. +..#..##.........#............#. +.......##.#...#...##...#...#..# +#.##....#.#...#.....#..#.#..... +..#........#..#.#.#.#....#.##.. +...#...#.#.........#...#.#..##. +#....#......#.#...........#..## +...#.#.#..#...##...#...#...#... +###..........#.#..........#.... +..#....#.#.#.#............#.#.. +....#...#..###...#.#....#...... +#...........####......##.#..... +..#..##.#...#.....#..#.......## +#.....#..###.....#...##..##.... +##..###..##...........#.#...#.. +.....#......#..............#... +#..#.##.###.......#.......#...# +#........#....##......#.#...... +.#.#.#...#.......#........#.##. +#..#..##.....#...#.#.#.#..###.. +.#.#....#..#..#.#....##.#.#.... +..#.#.........####.#...#.#.###. +....##........##....#........#. +................#..........#... +..#...................###.##..# +.........#..#..#.#...#....#.#.# +......#.....###.....#.#..#...#. +.#.#.....#..##............##... +...##......##.#....#........... +...##..##..###.#...##.......... +....###...#..#.#......#......#. +....##..............#..#..#.#.. +####.......#...##.##..#.#...... +.#......#.....#....###..#....#. +.#.......#...##...#..##.#...... +#.......#.......#.#....#.#.#..# +........#..#..#............##.# +#.#...#.#..##..#.......##..#... +...#....#...#..........##..#... +#.#...#.##....###......##....#. +#..#...###........#..#....#..#. +#....#....###....#..#.......#.. +....#.#........#.............#. +.#.##........##...#...#...#...# +#.....##.....#.......#.#.#..... +.#.##..........##..#....#...... +.#..##.##.#...##....#.#....##.. +........#.#.##.#....#.#..#....# +..#...........................# +.#...........#....#....#.#..#.. +........##...........#...#...#. +..#.....#..#......#..##.......# +..#....###..###...#.#.#..#....# +#..#.#...#......##......#...... +...........#...##..##....##.... +#.#......###..#.....#.......#.# +#.....#....#....#.#...#...#.... +....#...#.......#....##.#..#... +.####..##......##.#........#..# +..###..#.#.....#...........##.. +..##.#.#..#....#..#..#......... +..........#.#.#####...#........ +.###......##.#....#........#... +.....#..#..#.#..#.........#.... +..#....#...#...#...##.......... +....#..##.#.........##.#..##... +##.####..#...#.#...#.....#..### +..#..#...#...#.....##....#..#.# +#..##..#.....#....#.#.....##..# +...#...........##.....#......#. +......#...#.....#.#..###....... +.........#.....###.##..#...#... +.#...#.##...#..........#.#..##. +......#.......##.....#.....##.. +........###..........#...#..... +##.......###..###...##...#..... +#.#.............#..#..#.#...... +..##........#.###.....#....##.. +......#...#......#....##......# +..#.....#...##...#.......#..#.. +..#.###..##.##...#....#...##.#. +........##...#..#.#..##.....#.# +.......................#......# +..##.###......#.#.............# +....#...........###............ +##...##.....#.......##.......#. +...#..##..##..#.#.###..#......# +........#........#.#..#..#..... +.#......#....##..........#...#. +.##...........##....#.......... +.#..#....###.......#....#..##.. +.....###..........#....#.#.#... +...#....###.#.#......#......#.. +#.#.##.#.....#..#........#...#. +...#.##.........#..#.....#..... +.##...##......##...###...#..... +...#.....#.##..#...#..#........ +........#............#.#.#..##. +###...#.....#...#..#........##. +##...#..#.....#.#....#.#.#..... +#..##.......#...#.#...##..#.... +#...#.##.....#.#..#.##......#.# +..#......#.#.#.##.##..........# +..#.##......#.#.#..##.......... +....#..#....#..#..............# +..........###.....##..#........ +...#.....##.....#..#.#..#...##. +.#..##.#..#....#.#......#.##... +...#.....#..#.#...#..#.....#.#. +#...#.#......##...#..#...#....# +..#.......##...#..#.......#...# +#.....#...........##.#......... +.#......##.....####...#.......# +........#..#.....#.......#..#.. +....#.#...##..##...#..#....#... +#.#......#...#.#.###.....#..... +..##...#.#........#.##....#.#.# +.#....#......#.#...###.#....... +.......#.#...##....#.#....#.... +.....##..##...#..#.#.....##..#. +.##..#.#.#....##.#...#.....#... +.#..#..##....#.##.......#...#.. +....#.##...#..##......#.....#.. +.#..#....##....#............... +..##...#.....###............... +..............#.#.##........#.# +.#.#....#....#...#.#........#.. +.##...#...#.#....#....#.#.....# +#..............#......#.####.#. +......#...........#..#.....##.. +#.#..##.##.....#......#..#.#..# +##.##..#.##.#.............#...# +...#..#......#....#............ +........###.#.#..#...#.....#.## +..#.......#.##.........#..#.... +...##.#........##...#.#.##..#.. +...#..#......#...#....#........ +...........#..#..#...##...#.... +...#.....#....#..####..##.....# +.......#..#..#......#.........# +#......#........###.....##....# +..#..#..#.#.#....##...##......# +#.#..#..###.#..#.....####...... +.#................#####....#... +.#.........#...#.......#......# +..#.......#######........#..... +..#........#.....#..#...#..#..# +.#..#.#..#....#.#..##...#..#.#. +..#...........#.#...#.#.##..... +...#.#.#....##.###....#...####. +.....#..#.....#..#.#.........#. +......##...#...###............# +..#.#......###..####..#......#. +###.##.#..#......##.#..##.....# +....###...##............#.#.... +..#.....##...#...##....#...#... +#.....#.....#...#...#.#..#..... +####..........##.#.#..#.....##. +...#..........#...#...##..##.#. +..........#.........#.#..#..#.. +#....###.....#.#...#.......##.# +#..#.##.....#..........#...#... +...#.#.###.......##..#.....#... +#...#.#..#.............#..#.#.. +#........#.................#..# +..#.#....#.#..##.#...#..#....#. +#...#..........#...###....#...# +......#.............#....#....# +#.#.......##.......#.#....##..# +##...#....#.............#..#... +........#...###.##.#..###.#...# +...##...#..#..#...##..##......# +..#.......##....#.#.##....#.... +.....#....#..#.#...##.#.#.....#