From 583176447335fee8156b44e096cbb787c6f50fa6 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 8 Dec 2024 16:32:57 -0500 Subject: [PATCH] day8 --- CMakeLists.txt | 2 +- include/day9.h | 24 ++++++++++ problems/day8/input | 50 +++++++++++++++++++ src/day8.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++ src/day9.cpp | 33 +++++++++++++ src/main.cpp | 4 ++ 6 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 include/day9.h create mode 100644 problems/day8/input create mode 100644 src/day9.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f1d528..1ddf124 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(Advent-Of-Code-2024 VERSION 0.0.6) +project(Advent-Of-Code-2024 VERSION 0.0.7) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/include/day9.h b/include/day9.h new file mode 100644 index 0000000..c04e0bf --- /dev/null +++ b/include/day9.h @@ -0,0 +1,24 @@ +#pragma once +/* + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef DAY9_H +#define DAY9_H + +void run_day9(); + +#endif //DAY9_H diff --git a/problems/day8/input b/problems/day8/input new file mode 100644 index 0000000..11ca9dc --- /dev/null +++ b/problems/day8/input @@ -0,0 +1,50 @@ +.................................................. +.r................................................ +..........................I....................... +........................I......................... +................................................M. +............h......................A.............. +..7....................I.........h................ +......7..................................M....9... +.o.....U.......................................... +......................................O........... +....c.................J................O...M...A.. +.................................................. +...R...7.......................................... +..............r................................... +...................J..................9........... +...7..K......UJ................................... +......0...U.........................x............. +.......R.......0..B......................x........ +.......................k.....Z.......9............ +.......L.........I.....J............m............. +.....K.BR........r.0.C............................ +.......K.BR......c................................ +..................h....m....Al...........H........ +..............L..k.......h...m..........x..9...... +........................Z.....m........xO......... +..........0................l...................... +.6..................b............................. +............k...o..............Z.................. +........4.....o...........L....................... +....................Xo............................ +...........8..B..L.........i...................... +..z...............bX..........A................... +j........z...X......C.......i........5............ +.b...H6.......................U.......l........... +..................X............................... +...6......................Z..........a............ +....6........c............5.........1............. +.4.......................5........................ +..........k.......H1l............................. +2.................C.......i...................u... +.............a....2............................... +.....z....H.......1..8.....................u...... +...........j...b.................................. +3.........j.........................a............. +...4................a............................. +..M................j.....1..........5............. +............................................u..... +..4..3...........i................................ +z3.................2.............................. +..........8..................2.C.................. diff --git a/src/day8.cpp b/src/day8.cpp index c7c3232..68e3187 100644 --- a/src/day8.cpp +++ b/src/day8.cpp @@ -28,6 +28,120 @@ #include #include +namespace day8 +{ + std::vector map; + std::vector antinodes; + std::vector antinodes2; + + int width, height; + + inline char& get(const int i, const int j) + { + return map[j * width + i]; + } + + inline void anti_set(const char c, const int i, const int j) + { + if (j >= height || i >= width || i < 0 || j < 0) + return; + antinodes[j * width + i] = c; + } + + inline bool anti_set2(const char c, const int i, const int j) + { + if (j >= height || i >= width || i < 0 || j < 0) + return false; + antinodes2[j * width + i] = c; + return true; + } + + void run(const char compare, const int x, const int y) + { + for (int j = 0; j < height; j++) + { + for (int i = 0; i < width; i++) + { + if (i == x && y == j) + continue; + const auto current = get(i, j); + + if (compare == current) + { + const int dist_x = i - x; + const int dist_y = j - y; + + anti_set('#', i + dist_x, j + dist_y); + anti_set('#', x - dist_x, y - dist_y); + } + } + } + } + + void run2(const char compare, const int x, const int y) + { + for (int j = 0; j < height; j++) + { + for (int i = 0; i < width; i++) + { + if (i == x && y == j) + continue; + const auto current = get(i, j); + + if (compare == current) + { + const int dist_x = i - x; + const int dist_y = j - y; + + for (int mul = 0; mul < width * height; mul++) + { + if (!anti_set2('#', i + (dist_x * mul), j + (dist_y * mul)) && !anti_set2('#', x - (dist_x * mul), y - (dist_y * mul))) + break; + } + } + } + } + } +} + void run_day8() { + using namespace day8; + + auto lines = blt::fs::getLinesFromFile("../problems/day8/input"); + + height = static_cast(lines.size()); + width = static_cast(lines.begin()->size()); + for (const auto& line : lines) + { + map.insert(map.begin(), line.begin(), line.end()); + antinodes.insert(antinodes.begin(), line.begin(), line.end()); + antinodes2.insert(antinodes2.begin(), line.begin(), line.end()); + } + + for (int j = 0; j < height; j++) + { + for (int i = 0; i < width; i++) + { + const auto compare = get(i, j); + if (compare != '.') + { + run(compare, i, j); + run2(compare, i, j); + } + } + } + + int unique = 0; + int unique2 = 0; + for (const auto [c1, c2] : blt::in_pairs(antinodes, antinodes2)) + { + if (c1 == '#') + unique++; + if (c2 == '#') + unique2++; + } + + BLT_TRACE("Total Unique %d", unique); + BLT_TRACE("Total Unique2 %d", unique2); } diff --git a/src/day9.cpp b/src/day9.cpp new file mode 100644 index 0000000..497b8c3 --- /dev/null +++ b/src/day9.cpp @@ -0,0 +1,33 @@ +/* + * + * Copyright (C) 2024 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +void run_day9(){ + + } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c347ead..c92a08d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,7 @@ #include #include #include +#include int main(const int argc, const char** argv) { @@ -49,6 +50,9 @@ int main(const int argc, const char** argv) case 8: run_day8(); break; + case 9: + run_day9(); + break; default: BLT_INFO("%d is not a day", day); break;