Skip to content

Commit dece3b2

Browse files
committed
2025 day 2: solve part 1
1 parent 0f13e36 commit dece3b2

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Day 2:
2+
1227775554

2025/input/day02/example1.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,
2+
1698522-1698528,446443-446449,38593856-38593862,565653-565659,
3+
824824821-824824827,2121212118-2121212124

2025/src/day02.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/******************************************************************************
2+
* File: day02.cpp
3+
*
4+
* Author: Eric T. Johnson (yut23)
5+
* Created: 2025-12-02
6+
*****************************************************************************/
7+
8+
#include "day02.hpp"
9+
#include "lib.hpp"
10+
#include <fstream> // for ifstream
11+
#include <iostream> // for cout
12+
13+
int main(int argc, char **argv) {
14+
std::ifstream infile = aoc::parse_args(argc, argv).infile;
15+
16+
std::string line;
17+
long part1 = 0;
18+
while (std::getline(infile, line, ',')) {
19+
part1 += aoc::day02::sum_invalid_ids(line);
20+
}
21+
std::cout << part1 << "\n";
22+
23+
return 0;
24+
}

2025/src/day02.hpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/******************************************************************************
2+
* File: day02.hpp
3+
*
4+
* Author: Eric T. Johnson (yut23)
5+
* Created: 2025-12-02
6+
*****************************************************************************/
7+
8+
#ifndef DAY02_HPP_YMBKSAFO
9+
#define DAY02_HPP_YMBKSAFO
10+
11+
#include "lib.hpp"
12+
#include "util/math.hpp" // for next_power_of_10, prev_power_of_10
13+
#include <cassert> // for assert
14+
#include <iostream> // for istream
15+
#include <string> // for string, getline
16+
17+
namespace aoc::day02 {
18+
19+
long sum_invalid_ids(const std::string &line) {
20+
constexpr auto POWERS_OF_10 = aoc::math::gen_powers_of_10<long>();
21+
22+
const char *ptr1 = line.c_str();
23+
const char *ptr2 = ptr1;
24+
// this const_cast is annoying
25+
long start = std::strtol(ptr1, const_cast<char **>(&ptr2), 10);
26+
std::size_t start_digits = ptr2 - ptr1;
27+
assert(*ptr2 == '-');
28+
ptr1 = ptr2 + 1;
29+
long end = std::strtol(ptr1, const_cast<char **>(&ptr2), 10);
30+
std::size_t end_digits = ptr2 - ptr1;
31+
if constexpr (aoc::DEBUG)
32+
std::cerr << "range " << start << "-" << end << ":";
33+
34+
if (start_digits % 2 == 1 && end_digits % 2 == 1) {
35+
// all values have an odd number of digits
36+
if constexpr (aoc::DEBUG)
37+
std::cerr << "\n";
38+
return 0;
39+
}
40+
41+
if (start_digits % 2 == 1) {
42+
start = aoc::math::next_power_of_10(start);
43+
++start_digits;
44+
if (start > end) {
45+
if constexpr (aoc::DEBUG)
46+
std::cerr << "\n";
47+
return 0;
48+
}
49+
}
50+
long start_upper = start / POWERS_OF_10[start_digits / 2];
51+
long start_lower = start % POWERS_OF_10[start_digits / 2];
52+
53+
if (end_digits % 2 == 1) {
54+
end = aoc::math::prev_power_of_10(end);
55+
--end_digits;
56+
if (start > end) {
57+
if constexpr (aoc::DEBUG)
58+
std::cerr << "\n";
59+
return 0;
60+
}
61+
}
62+
long end_upper = end / POWERS_OF_10[end_digits / 2];
63+
long end_lower = end % POWERS_OF_10[end_digits / 2];
64+
65+
constexpr auto value_for_upper = [](long x) {
66+
return x * aoc::math::next_power_of_10(x) + x;
67+
};
68+
69+
long sum = 0;
70+
if (start_lower <= start_upper &&
71+
(end_upper > start_upper || end_lower >= start_upper)) {
72+
auto x = value_for_upper(start_upper);
73+
if constexpr (aoc::DEBUG)
74+
std::cerr << " " << x;
75+
sum += x;
76+
}
77+
if (start_upper < end_upper) {
78+
for (long upper = start_upper + 1; upper <= end_upper - 1; ++upper) {
79+
auto x = value_for_upper(upper);
80+
if constexpr (aoc::DEBUG)
81+
std::cerr << " " << x;
82+
sum += x;
83+
}
84+
if (end_lower >= end_upper) {
85+
auto x = value_for_upper(end_upper);
86+
if constexpr (aoc::DEBUG)
87+
std::cerr << " " << x;
88+
sum += x;
89+
}
90+
}
91+
if constexpr (aoc::DEBUG)
92+
std::cerr << std::endl;
93+
94+
return sum;
95+
}
96+
97+
} // namespace aoc::day02
98+
99+
#endif /* end of include guard: DAY02_HPP_YMBKSAFO */

0 commit comments

Comments
 (0)