aoc2024: cleanup day1 solution

This commit is contained in:
Bananymous 2024-12-01 07:57:51 +02:00
parent 6662dc4a8d
commit 1ff6aa1748
1 changed files with 11 additions and 16 deletions

View File

@ -43,26 +43,21 @@ i64 part2(FILE* fp)
BAN::HashMap<i64, i64> lhs; BAN::HashMap<i64, i64> lhs;
BAN::HashMap<i64, i64> rhs; BAN::HashMap<i64, i64> rhs;
constexpr auto increment_count =
[](auto& map, i64 key) {
auto it = map.find(key);
if (it == map.end())
MUST(map.insert(key, 1));
else
it->value++;
};
for (;;) { for (;;) {
i64 l, r; i64 l, r;
if (fscanf(fp, "%" SCNd64 " %" SCNd64 "\n", &l, &r) != 2) if (fscanf(fp, "%" SCNd64 " %" SCNd64 "\n", &l, &r) != 2)
break; break;
increment_count(lhs, l);
{ increment_count(rhs, r);
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; i64 result = 0;