Created
December 2, 2025 14:09
-
-
Save tatut/59b56a39c4d76840136b47e39dd4d305 to your computer and use it in GitHub Desktop.
AoC 2025, day2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "aoc.h" // common libc includes and reading input file to memory | |
| bool is_invalid_p1(long num) { | |
| char buf[16]; | |
| size_t len = snprintf(buf, 16, "%ld", num); | |
| if(len % 2) return false; // not even length | |
| int hl = len/2; | |
| for(int i=0;i<hl;i++) { | |
| if(buf[i] != buf[i+hl]) return false; | |
| } | |
| return true; | |
| } | |
| bool is_invalid_p2(long num) { | |
| char buf[16]; | |
| size_t len = snprintf(buf, 16, "%ld", num); | |
| int hl = len/2; | |
| // check all possible lengths from 1 upto half | |
| for(int l=1; l <= hl; l++) { | |
| bool invalid = true; | |
| for(int i=l;i<len;i+=l) { | |
| if(memcmp(&buf[0], &buf[i], l) != 0) { | |
| invalid = false; | |
| continue; | |
| } | |
| } | |
| if(invalid) return true; | |
| } | |
| return false; | |
| } | |
| int main(int argc, char **argv) { | |
| size_t len; | |
| char *in = input("day2.txt", &len); | |
| char *end = in + len; | |
| long invalid_sum_p1 = 0, invalid_sum_p2 = 0; | |
| while(in < end) { | |
| long low = atol(in); | |
| while(*in != '-') in++; | |
| in++; | |
| long high = atol(in); | |
| while(*in != ',' && in < end) in++; | |
| in++; | |
| for(long n = low; n<=high; n++) { | |
| if(is_invalid_p1(n)) invalid_sum_p1 += n; | |
| if(is_invalid_p2(n)) invalid_sum_p2 += n; | |
| } | |
| } | |
| printf("Part1: %ld\nPart2: %ld\n", invalid_sum_p1, invalid_sum_p2); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment