AOC2023: Implement day15

This commit is contained in:
Bananymous 2023-12-19 11:27:28 +02:00
parent 3352640d09
commit 9ec733904f
4 changed files with 174 additions and 0 deletions

View File

@ -17,6 +17,7 @@ set(AOC2023_PROJECTS
day12 day12
day13 day13
day14 day14
day15
) )
set(BANAN_AOC2023_BIN ${BANAN_BIN}/aoc2023) set(BANAN_AOC2023_BIN ${BANAN_BIN}/aoc2023)

View File

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

View File

@ -0,0 +1,150 @@
#include <BAN/Array.h>
#include <BAN/LinkedList.h>
#include <BAN/String.h>
#include <BAN/StringView.h>
#include <BAN/Vector.h>
#include <ctype.h>
#include <inttypes.h>
#include <stdio.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 parse_i64(BAN::StringView string)
{
i64 result = 0;
for (char c : string)
result = (result * 10) + (c - '0');
return result;
}
BAN::Vector<BAN::StringView> parse_instructions(FILE* fp)
{
static BAN::String line;
char buffer[128];
while (fgets(buffer, sizeof(buffer), fp))
{
BAN::StringView buffer_sv(buffer);
MUST(line.append(buffer_sv));
if (buffer_sv.back() == '\n')
{
line.pop_back();
break;
}
}
return MUST(line.sv().split(','));
}
i64 calculate_hash(BAN::StringView string)
{
i64 result = 0;
for (char c : string)
result = ((result + c) * 17) % 256;
return result;
}
i64 puzzle1(FILE* fp)
{
auto instructions = parse_instructions(fp);
i64 result = 0;
for (auto insn : instructions)
result += calculate_hash(insn);
return result;
}
i64 puzzle2(FILE* fp)
{
struct Entry
{
BAN::String label;
i64 focal_length;
};
using Box = BAN::LinkedList<Entry>;
auto instructions = parse_instructions(fp);
BAN::Array<Box, 256> boxes;
for (auto insn : instructions)
{
if (insn.back() == '-')
{
auto label = insn.substring(0, insn.size() - 1);
i64 hash = calculate_hash(label);
for (auto it = boxes[hash].begin(); it != boxes[hash].end(); it++)
{
if (it->label == label)
{
boxes[hash].remove(it);
break;
}
}
}
else
{
auto temp = MUST(insn.split('='));
auto label = temp[0];
auto focal_length = parse_i64(temp[1]);
i64 hash = calculate_hash(label);
bool found = false;
for (auto it = boxes[hash].begin(); it != boxes[hash].end(); it++)
{
if (it->label == label)
{
it->focal_length = focal_length;
found = true;
break;
}
}
if (!found)
MUST(boxes[hash].emplace_back(label, focal_length));
}
}
i64 result = 0;
for (size_t i = 0; i < boxes.size(); i++)
{
size_t slot = 0;
for (auto it = boxes[i].begin(); it != boxes[i].end(); it++, slot++)
result += (i + 1) * (slot + 1) * it->focal_length;
}
return result;
}
int main(int argc, char** argv)
{
const char* file_path = "/usr/share/aoc2023/day15_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);
}

File diff suppressed because one or more lines are too long