From 531211e09daea3ccd5e88c91bbbbbed93920219a Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 6 Dec 2023 07:27:25 +0200 Subject: [PATCH] AOC2023: implement day6 --- userspace/aoc2023/CMakeLists.txt | 1 + userspace/aoc2023/day6/CMakeLists.txt | 22 +++++ userspace/aoc2023/day6/main.cpp | 122 ++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 userspace/aoc2023/day6/CMakeLists.txt create mode 100644 userspace/aoc2023/day6/main.cpp diff --git a/userspace/aoc2023/CMakeLists.txt b/userspace/aoc2023/CMakeLists.txt index 81b74da6c1..3a44bc67fc 100644 --- a/userspace/aoc2023/CMakeLists.txt +++ b/userspace/aoc2023/CMakeLists.txt @@ -8,6 +8,7 @@ set(AOC2023_PROJECTS day3 day4 day5 + day6 ) set(BANAN_AOC2023_BIN ${BANAN_BIN}/aoc2023) diff --git a/userspace/aoc2023/day6/CMakeLists.txt b/userspace/aoc2023/day6/CMakeLists.txt new file mode 100644 index 0000000000..c1e5eb5e4c --- /dev/null +++ b/userspace/aoc2023/day6/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.26) + +project(aoc2023_day6 CXX) + +set(SOURCES + main.cpp +) + +add_executable(aoc2023_day6 ${SOURCES}) +target_compile_options(aoc2023_day6 PUBLIC -O2 -g) +target_link_libraries(aoc2023_day6 PUBLIC libc ban) + +add_dependencies(aoc2023_day6 libc-install ban-install) + +add_custom_target(aoc2023_day6-install + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/aoc2023_day6 ${BANAN_AOC2023_BIN}/day6 + DEPENDS aoc2023_day6 + DEPENDS aoc2023_always +) + +add_dependencies(aoc2023 aoc2023_day6) +add_dependencies(aoc2023-install aoc2023_day6-install) diff --git a/userspace/aoc2023/day6/main.cpp b/userspace/aoc2023/day6/main.cpp new file mode 100644 index 0000000000..b857fc51af --- /dev/null +++ b/userspace/aoc2023/day6/main.cpp @@ -0,0 +1,122 @@ +#include + +#include +#include +#include + +using i64 = int64_t; + +i64 parse_i64_all_digits(BAN::StringView part) +{ + i64 result = 0; + for (char c : part) + if (isdigit(c)) + result = (result * 10) + (c - '0'); + return result; +} + +i64 puzzle1(FILE* fp) +{ + BAN::Vector times, distances; + + { + char buffer[128]; + fgets(buffer, sizeof(buffer), fp); + BAN::StringView line(buffer); + line = line.substring(10); + line = line.substring(0, line.size() - 1); + + auto split = MUST(line.split(' ')); + for (auto part : split) + MUST(times.push_back(parse_i64_all_digits(part))); + } + + { + char buffer[128]; + fgets(buffer, sizeof(buffer), fp); + BAN::StringView line(buffer); + line = line.substring(10); + line = line.substring(0, line.size() - 1); + + auto split = MUST(line.split(' ')); + for (auto part : split) + MUST(distances.push_back(parse_i64_all_digits(part))); + } + + ASSERT(times.size() == distances.size()); + + i64 result = 1; + + for (size_t i = 0; i < times.size(); i++) + { + i64 time = times[i]; + i64 distance = distances[i]; + + i64 min = distance / time - 1; + while ((time - min) * min <= distance) + min++; + + i64 max = min; + while ((time - max) * max > distance) + max++; + + result *= max - min; + } + + return result; +} + +i64 puzzle2(FILE* fp) +{ + i64 time = 0; + i64 distance = 0; + + { + char buffer[128]; + fgets(buffer, sizeof(buffer), fp); + BAN::StringView line(buffer); + line = line.substring(10); + time = parse_i64_all_digits(line); + } + + { + char buffer[128]; + fgets(buffer, sizeof(buffer), fp); + BAN::StringView line(buffer); + line = line.substring(10); + distance = parse_i64_all_digits(line); + } + + i64 min = distance / time - 1; + while ((time - min) * min <= distance) + min++; + + i64 max = min; + while ((time - max) * max > distance) + max++; + + return max - min; +} + +int main(int argc, char** argv) +{ + const char* file_path = "/usr/share/aoc2023/day6_input.txt"; + + if (argc >= 2) + file_path = argv[1]; + + FILE* fp = fopen(file_path, "r"); + if (fp == nullptr) + { + perror("fopen"); + return 1; + } + + printf("puzzle1: %lld\n", puzzle1(fp)); + + fseek(fp, 0, SEEK_SET); + + printf("puzzle2: %lld\n", puzzle2(fp)); + + fclose(fp); +}