From d87fa1a7ead11e0cdabbafa3ed89292f390d0019 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 26 Dec 2023 14:10:49 +0200 Subject: [PATCH] AOC2023: Implement day24 part1 --- userspace/aoc2023/CMakeLists.txt | 1 + userspace/aoc2023/day24/CMakeLists.txt | 22 ++++ userspace/aoc2023/day24/main.cpp | 138 +++++++++++++++++++++++++ userspace/aoc2023/full/main.cpp | 2 +- 4 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 userspace/aoc2023/day24/CMakeLists.txt create mode 100644 userspace/aoc2023/day24/main.cpp diff --git a/userspace/aoc2023/CMakeLists.txt b/userspace/aoc2023/CMakeLists.txt index 7e8f9b5c53..c32a1907a1 100644 --- a/userspace/aoc2023/CMakeLists.txt +++ b/userspace/aoc2023/CMakeLists.txt @@ -25,6 +25,7 @@ set(AOC2023_PROJECTS day20 day21 day23 + day24 full ) diff --git a/userspace/aoc2023/day24/CMakeLists.txt b/userspace/aoc2023/day24/CMakeLists.txt new file mode 100644 index 0000000000..90e2b855c3 --- /dev/null +++ b/userspace/aoc2023/day24/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.26) + +project(aoc2023_day24 CXX) + +set(SOURCES + main.cpp +) + +add_executable(aoc2023_day24 ${SOURCES}) +target_compile_options(aoc2023_day24 PUBLIC -O2 -g) +target_link_libraries(aoc2023_day24 PUBLIC libc ban) + +add_dependencies(aoc2023_day24 libc-install ban-install) + +add_custom_target(aoc2023_day24-install + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/aoc2023_day24 ${BANAN_AOC2023_BIN}/day24 + DEPENDS aoc2023_day24 + DEPENDS aoc2023_always +) + +add_dependencies(aoc2023 aoc2023_day24) +add_dependencies(aoc2023-install aoc2023_day24-install) diff --git a/userspace/aoc2023/day24/main.cpp b/userspace/aoc2023/day24/main.cpp new file mode 100644 index 0000000000..d8db910b92 --- /dev/null +++ b/userspace/aoc2023/day24/main.cpp @@ -0,0 +1,138 @@ +#include + +#include +#include +#include + +using i8 = int8_t; +using i16 = int16_t; +using i32 = int32_t; +using i64 = int64_t; + +using u8 = uint8_t; +using u16 = uint16_t; +using u32 = uint32_t; +using u64 = uint64_t; + +struct Hailstone +{ + double px, py, pz; + double vx, vy, vz; +}; + +i64 parse_i64(BAN::StringView str) +{ + while (isspace(str.front())) + str = str.substring(1); + bool negative = str.front() == '-'; + if (negative) + str = str.substring(1); + i64 result = 0; + for (char c : str) + if (isdigit(c)) + result = (result * 10) + (c - '0'); + return negative ? -result : result; +} + +BAN::Vector parse_hailstones(FILE* fp) +{ + BAN::Vector hailstones; + + char buffer[128]; + while (fgets(buffer, sizeof(buffer), fp)) + { + BAN::StringView line(buffer); + ASSERT(line.back() == '\n'); + line = line.substring(0, line.size() - 1); + if (line.empty()) + break; + + auto position_velocity_strs = MUST(line.split('@')); + ASSERT(position_velocity_strs.size() == 2); + + Hailstone hailstone; + + auto position_strs = MUST(position_velocity_strs[0].split(',')); + ASSERT(position_strs.size() == 3); + hailstone.px = parse_i64(position_strs[0]); + hailstone.py = parse_i64(position_strs[1]); + hailstone.pz = parse_i64(position_strs[2]); + + auto velocity_strs = MUST(position_velocity_strs[1].split(',')); + ASSERT(velocity_strs.size() == 3); + hailstone.vx = parse_i64(velocity_strs[0]); + hailstone.vy = parse_i64(velocity_strs[1]); + hailstone.vz = parse_i64(velocity_strs[2]); + + MUST(hailstones.push_back(hailstone)); + } + + return hailstones; +} + +i64 puzzle1(FILE* fp) +{ + auto hailstones = parse_hailstones(fp); + + i64 result = 0; + + for (size_t i = 0; i < hailstones.size(); i++) + { + for (size_t j = i + 1; j < hailstones.size(); j++) + { + const auto& h1 = hailstones[i]; + const auto& h2 = hailstones[j]; + + // skip parallel lines + if (h1.vy * h2.vx == h2.vy * h1.vx) + continue; + + const double t2 = ((h2.py - h1.py) * h1.vx - (h2.px - h1.px) * h1.vy) / (h2.vx * h1.vy - h1.vx * h2.vy); + if (t2 < 0.0) + continue; + + const double t1 = ((h2.py - h1.py) + t2 * h2.vy) / h1.vy; + if (t1 < 0.0) + continue; + + const double x = h2.px + t2 * h2.vx; + const double y = h2.py + t2 * h2.vy; + + constexpr double min = 200000000000000.0; + constexpr double max = 400000000000000.0; + if ((min <= x && x <= max) && (min <= y && y <= max)) + result++; + } + } + + return result; +} + +i64 puzzle2(FILE* fp) +{ + (void)fp; + return -1; +} + +int main(int argc, char** argv) +{ + const char* file_path = "/usr/share/aoc2023/day24_input.txt"; + + if (argc >= 2) + file_path = argv[1]; + + FILE* fp = fopen(file_path, "r"); + if (fp == nullptr) + { + perror("fopen"); + return 1; + } + + printf("puzzle1: %" PRId64 "\n", puzzle1(fp)); + + fseek(fp, 0, SEEK_SET); + + printf("puzzle2: %" PRId64 "\n", puzzle2(fp)); + + fclose(fp); +} diff --git a/userspace/aoc2023/full/main.cpp b/userspace/aoc2023/full/main.cpp index b98090c925..d99104f45c 100644 --- a/userspace/aoc2023/full/main.cpp +++ b/userspace/aoc2023/full/main.cpp @@ -4,7 +4,7 @@ int main() { - for (int i = 1; i <= 23; i++) + for (int i = 1; i <= 24; i++) { if (i == 22) continue;