From 1ff6aa17482b452480108cce9271cf4b9a26aa22 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Sun, 1 Dec 2024 07:57:51 +0200 Subject: [PATCH] aoc2024: cleanup day1 solution --- userspace/aoc2024/day1/main.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/userspace/aoc2024/day1/main.cpp b/userspace/aoc2024/day1/main.cpp index 39af86ca..a9b24300 100644 --- a/userspace/aoc2024/day1/main.cpp +++ b/userspace/aoc2024/day1/main.cpp @@ -43,26 +43,21 @@ i64 part2(FILE* fp) BAN::HashMap lhs; BAN::HashMap 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 (;;) { 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++; - } + increment_count(lhs, l); + increment_count(rhs, r); } i64 result = 0;