aoc2024: Implement day3 solution

This commit is contained in:
Bananymous 2024-12-03 16:27:07 +02:00
parent 86859267f0
commit 3a5a22511a
3 changed files with 162 additions and 0 deletions

View File

@ -1,6 +1,7 @@
set(AOC2024_PROJECTS set(AOC2024_PROJECTS
day1 day1
day2 day2
day3
full full
) )

View File

@ -0,0 +1,9 @@
set(SOURCES
main.cpp
)
add_executable(aoc2024_day3 ${SOURCES})
banan_include_headers(aoc2024_day3 ban)
banan_link_library(aoc2024_day3 libc)
install(TARGETS aoc2024_day3 OPTIONAL)

View File

@ -0,0 +1,152 @@
#include <BAN/String.h>
#include <inttypes.h>
#include <stdio.h>
#include <ctype.h>
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;
i64 part1(FILE* fp)
{
i64 result = 0;
BAN::String full_input;
{
char buffer[512];
while (fgets(buffer, sizeof(buffer), fp))
MUST(full_input.append(buffer));
}
result = 0;
for (size_t i = 0; i < full_input.size() - 4;)
{
if (!full_input.sv().substring(i).starts_with("mul("))
{
i++;
continue;
}
i += 4;
if (i >= full_input.size() || !isdigit(full_input[i]))
continue;
i64 lhs = atoll(&full_input[i]);
while (i < full_input.size() && isdigit(full_input[i]))
i++;
if (i >= full_input.size() || full_input[i] != ',')
continue;
i++;
if (i >= full_input.size() || !isdigit(full_input[i]))
continue;
int rhs = atoll(&full_input[i]);
while (i < full_input.size() && isdigit(full_input[i]))
i++;
if (i >= full_input.size() || full_input[i] != ')')
continue;
result += lhs * rhs;
}
return result;
}
i64 part2(FILE* fp)
{
i64 result = 0;
BAN::String full_input;
{
char buffer[512];
while (fgets(buffer, sizeof(buffer), fp))
MUST(full_input.append(buffer));
}
bool state = true;
result = 0;
for (size_t i = 0; i < full_input.size();)
{
if (full_input.sv().substring(i).starts_with("do()"_sv))
{
state = true;
i += 4;
continue;
}
if (full_input.sv().substring(i).starts_with("don't()"_sv))
{
state = false;
i += 6;
continue;
}
if (state == false || !full_input.sv().substring(i).starts_with("mul("))
{
i++;
continue;
}
i += 4;
if (i >= full_input.size() || !isdigit(full_input[i]))
continue;
i64 lhs = atoll(&full_input[i]);
while (i < full_input.size() && isdigit(full_input[i]))
i++;
if (i >= full_input.size() || full_input[i] != ',')
continue;
i++;
if (i >= full_input.size() || !isdigit(full_input[i]))
continue;
int rhs = atoll(&full_input[i]);
while (i < full_input.size() && isdigit(full_input[i]))
i++;
if (i >= full_input.size() || full_input[i] != ')')
continue;
result += lhs * rhs;
}
return result;
}
int main(int argc, char** argv)
{
const char* file_path = "/usr/share/aoc2024/day3_input.txt";
if (argc >= 2)
file_path = argv[1];
FILE* fp = fopen(file_path, "r");
if (fp == nullptr)
{
perror("fopen");
return 1;
}
printf("part1: %" PRId64 "\n", part1(fp));
fseek(fp, 0, SEEK_SET);
printf("part2: %" PRId64 "\n", part2(fp));
fclose(fp);
}