Compare commits
2 Commits
9c2fcd745c
...
d6b170e274
Author | SHA1 | Date |
---|---|---|
Bananymous | d6b170e274 | |
Bananymous | 11cac7a646 |
|
@ -1,6 +1,7 @@
|
|||
add_custom_target(userspace)
|
||||
|
||||
#add_subdirectory(aoc2023)
|
||||
#add_subdirectory(aoc2024)
|
||||
add_subdirectory(libraries)
|
||||
add_subdirectory(programs)
|
||||
add_subdirectory(tests)
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
set(AOC2024_PROJECTS
|
||||
day1
|
||||
full
|
||||
)
|
||||
|
||||
set(BANAN_AOC2024_BIN ${CMAKE_INSTALL_BINDIR}/aoc2024)
|
||||
set(BANAN_AOC2024_INPUT ${BANAN_SHARE}/aoc2024)
|
||||
|
||||
set(CMAKE_INSTALL_BINDIR ${BANAN_AOC2024_BIN})
|
||||
|
||||
add_custom_target(aoc2024)
|
||||
|
||||
file(GLOB_RECURSE input_files "input/*")
|
||||
foreach(file ${input_files})
|
||||
install(FILES ${file} DESTINATION ${BANAN_AOC2024_INPUT})
|
||||
endforeach()
|
||||
|
||||
foreach(AOC2024_PROJECT ${AOC2024_PROJECTS})
|
||||
add_subdirectory(${AOC2024_PROJECT})
|
||||
add_dependencies(aoc2024 aoc2024_${AOC2024_PROJECT})
|
||||
endforeach()
|
||||
|
||||
add_dependencies(userspace aoc2024)
|
|
@ -0,0 +1,9 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(aoc2024_day-template ${SOURCES})
|
||||
banan_include_headers(aoc2024_day-template ban)
|
||||
banan_link_library(aoc2024_day-template libc)
|
||||
|
||||
install(TARGETS aoc2024_day-template OPTIONAL)
|
|
@ -0,0 +1,47 @@
|
|||
#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 part1(FILE* fp)
|
||||
{
|
||||
(void)fp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
i64 part2(FILE* fp)
|
||||
{
|
||||
(void)fp;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
const char* file_path = "/usr/share/aoc2024/day-template_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);
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(aoc2024_day1 ${SOURCES})
|
||||
banan_include_headers(aoc2024_day1 ban)
|
||||
banan_link_library(aoc2024_day1 libc)
|
||||
|
||||
install(TARGETS aoc2024_day1 OPTIONAL)
|
|
@ -0,0 +1,101 @@
|
|||
#include <BAN/HashMap.h>
|
||||
#include <BAN/Vector.h>
|
||||
#include <BAN/Sort.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 part1(FILE* fp)
|
||||
{
|
||||
BAN::Vector<i64> lhs;
|
||||
BAN::Vector<i64> rhs;
|
||||
|
||||
for (;;) {
|
||||
i64 l, r;
|
||||
if (fscanf(fp, "%" SCNd64 " %" SCNd64 "\n", &l, &r) != 2)
|
||||
break;
|
||||
MUST(lhs.push_back(l));
|
||||
MUST(rhs.push_back(r));
|
||||
}
|
||||
|
||||
BAN::sort::sort(lhs.begin(), lhs.end());
|
||||
BAN::sort::sort(rhs.begin(), rhs.end());
|
||||
|
||||
i64 result = 0;
|
||||
for (size_t i = 0; i < lhs.size(); i++)
|
||||
result += BAN::Math::abs(lhs[i] - rhs[i]);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
i64 part2(FILE* fp)
|
||||
{
|
||||
BAN::HashMap<i64, i64> lhs;
|
||||
BAN::HashMap<i64, i64> rhs;
|
||||
|
||||
for (;;) {
|
||||
i64 l, r;
|
||||
if (fscanf(fp, "%" SCNd64 " %" SCNd64 "\n", &l, &r) != 2)
|
||||
break;
|
||||
|
||||
{
|
||||
auto it = lhs.find(l);
|
||||
if (it == lhs.end())
|
||||
MUST(lhs.insert(l, 1));
|
||||
else
|
||||
it->value++;
|
||||
}
|
||||
|
||||
{
|
||||
auto it = rhs.find(r);
|
||||
if (it == rhs.end())
|
||||
MUST(rhs.insert(r, 1));
|
||||
else
|
||||
it->value++;
|
||||
}
|
||||
}
|
||||
|
||||
i64 result = 0;
|
||||
|
||||
for (auto [id, count] : lhs) {
|
||||
auto it = rhs.find(id);
|
||||
if (it == rhs.end())
|
||||
continue;
|
||||
result += id * count * it->value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
const char* file_path = "/usr/share/aoc2024/day1_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);
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
set(SOURCES
|
||||
main.cpp
|
||||
)
|
||||
|
||||
add_executable(aoc2024_full ${SOURCES})
|
||||
banan_include_headers(aoc2024_full ban)
|
||||
banan_link_library(aoc2024_full libc)
|
||||
|
||||
install(TARGETS aoc2024_full OPTIONAL)
|
|
@ -0,0 +1,22 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
for (int i = 1; i <= 25; i++)
|
||||
{
|
||||
char command[128];
|
||||
sprintf(command, "/bin/aoc2024/day%d", i);
|
||||
|
||||
struct stat st;
|
||||
if (stat(command, &st) == -1)
|
||||
continue;
|
||||
|
||||
printf("day%d\n", i);
|
||||
|
||||
system(command);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
#!/bin/bash
|
||||
|
||||
if [[ -z $1 ]]; then
|
||||
echo Please specify day number >& 2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -d day$1 ]]; then
|
||||
cp -r day-template day$1
|
||||
sed -i "s/day-template/day$1/g" day$1/*
|
||||
fi
|
||||
|
||||
if [[ ! -f input/day$1_input.txt ]]; then
|
||||
wget --no-cookies --header "Cookie: session=$AOC_SESSION" https://adventofcode.com/2024/day/$1/input -O input/day$1_input.txt
|
||||
fi
|
|
@ -0,0 +1 @@
|
|||
day*_input.txt
|
Loading…
Reference in New Issue