AOC2023: Implement day9

This commit is contained in:
Bananymous 2023-12-09 16:22:43 +02:00
parent 951873098e
commit 0c3e5980d6
3 changed files with 171 additions and 0 deletions

View File

@ -11,6 +11,7 @@ set(AOC2023_PROJECTS
day6
day7
day8
day9
)
set(BANAN_AOC2023_BIN ${BANAN_BIN}/aoc2023)

View File

@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.26)
project(aoc2023_day9 CXX)
set(SOURCES
main.cpp
)
add_executable(aoc2023_day9 ${SOURCES})
target_compile_options(aoc2023_day9 PUBLIC -O2 -g)
target_link_libraries(aoc2023_day9 PUBLIC libc ban)
add_dependencies(aoc2023_day9 libc-install ban-install)
add_custom_target(aoc2023_day9-install
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/aoc2023_day9 ${BANAN_AOC2023_BIN}/day9
DEPENDS aoc2023_day9
DEPENDS aoc2023_always
)
add_dependencies(aoc2023 aoc2023_day9)
add_dependencies(aoc2023-install aoc2023_day9-install)

View File

@ -0,0 +1,148 @@
#include <BAN/String.h>
#include <BAN/Vector.h>
#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;
i64 parse_i64(BAN::StringView sv)
{
bool negative = (!sv.empty() && sv.front() == '-');
if (negative)
sv = sv.substring(1);
i64 result = 0;
for (char c : sv)
{
if (!isdigit(c))
break;
result = (result * 10) + (c - '0');
}
return negative ? -result : result;
}
i64 puzzle1(FILE* fp)
{
i64 result = 0;
char buffer[128];
while (fgets(buffer, sizeof(buffer), fp))
{
BAN::StringView line(buffer);
auto value_strs = MUST(line.split(' '));
if (value_strs.empty())
continue;
BAN::Vector<BAN::Vector<i64>> values;
MUST(values.emplace_back(value_strs.size() + 1, 0));
for (size_t i = 0; i < value_strs.size(); i++)
values.back()[i] = parse_i64(value_strs[i]);
bool all_zeroes = false;
while (!all_zeroes)
{
size_t last = values.size();
MUST(values.emplace_back(values.back().size() - 1, 0));
all_zeroes = true;
for (size_t i = 0; i < values.back().size() - 1; i++)
{
i64 lhs = values[last - 1][i + 0];
i64 rhs = values[last - 1][i + 1];
values[last][i] = rhs - lhs;
if (values[last][i] != 0)
all_zeroes = false;
}
}
for (size_t i = values.size() - 1; i > 0; i--)
{
const size_t len = values[i].size();
i64 lhs = values[i - 1][len - 1];
i64 res = values[i][len - 1];
values[i - 1][len - 0] = res + lhs;
}
result += values.front().back();
}
return result;
}
i64 puzzle2(FILE* fp)
{
i64 result = 0;
char buffer[128];
while (fgets(buffer, sizeof(buffer), fp))
{
BAN::StringView line(buffer);
auto value_strs = MUST(line.split(' '));
if (value_strs.empty())
continue;
BAN::Vector<BAN::Vector<i64>> values;
MUST(values.emplace_back(value_strs.size() + 1, 0));
for (size_t i = 0; i < value_strs.size(); i++)
values.back()[i + 1] = parse_i64(value_strs[i]);
bool all_zeroes = false;
while (!all_zeroes)
{
size_t last = values.size();
MUST(values.emplace_back(values.back().size() - 1, 0));
all_zeroes = true;
for (size_t i = 1; i < values.back().size(); i++)
{
i64 lhs = values[last - 1][i + 0];
i64 rhs = values[last - 1][i + 1];
values[last][i] = rhs - lhs;
if (values[last][i] != 0)
all_zeroes = false;
}
}
for (size_t i = values.size() - 1; i > 0; i--)
{
i64 rhs = values[i - 1][1];
i64 res = values[i][0];
values[i - 1][0] = rhs - res;
}
result += values.front().front();
}
return result;
}
int main(int argc, char** argv)
{
const char* file_path = "/usr/share/aoc2023/day9_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);
}