aoc

Thing1's 2025 aoc
Log | Files | Refs

commit 8eb972081f50c5c671e191d385b6be96c05d8804
parent 44fc9c59b6365157be2c4f61eb093a020c84aacc
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Wed,  3 Dec 2025 10:08:17 +0000

day 2

Diffstat:
M.gitignore | 5+++--
A2/2a.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
A2/2b.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2/Makefile | 11+++++++++++
A2/input | 1+
A2/test | 2++
6 files changed, 132 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,2 +1,4 @@ 1/1a -1/1b -\ No newline at end of file +1/1b +2/2a +2/2b diff --git a/2/2a.c b/2/2a.c @@ -0,0 +1,51 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> + +typedef struct range { + long long int upper, lower; +} range; + +bool +readrange(FILE *in, range *r) { + fscanf(in, "%lld-%lld", &r->lower, &r->upper); + return (getc(in) == ',') ? 1 : 0; +} + +bool +isvalid(long long int i) { + char str[20] = {0}, str2[20] = {0}; + int mid = 0; + + snprintf(str, 20, "%lld", i); + + mid = strlen(str) / 2; + if ((strlen(str) % 2) != 0) + mid++; + + memcpy(str2, str, mid); + return (memcmp(str2, str + mid, mid) != 0); +} + +long long int +checkrange(range r) { + long long int total = 0; + for (long long int i = r.lower; i <= r.upper; i++) { + if (!isvalid(i)) total += i; + } + return total; +} + +int +main() { + range r; + long long int total = 0; + + while (readrange(stdin, &r)) + total += checkrange(r); + total += checkrange(r); + + printf("%lld\n", total); + return 0; +} +\ No newline at end of file diff --git a/2/2b.c b/2/2b.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> + +typedef struct range { + long long int upper, lower; +} range; + +bool +readrange(FILE *in, range *r) { + fscanf(in, "%lld-%lld", &r->lower, &r->upper); + return (getc(in) == ',') ? 1 : 0; +} + +bool +isrepeat(char *s1, char *s2, int i) { + do { + s1 += i; + if (s1[0] == 0) break; + if (memcmp(s1, s2, i) != 0) return false; + } while (s1[0] != 0); + return true; +} + +bool +isvalid(long long int i) { + char str[20] = {0}, str2[20] = {0}; + int mid = 0; + + snprintf(str, 20, "%lld", i); + + for (int i = 1; i < (strlen(str) / 2) + 1; i++) { + memset(str2, 0, 20); + memcpy(str2, str, i); + if (isrepeat(str, str2, i)) return false; + } + + return true; +} + +long long int +checkrange(range r) { + long long int total = 0; + for (long long int i = r.lower; i <= r.upper; i++) { + if (!isvalid(i)) total += i; + } + return total; +} + +int +main() { + range r; + long long int total = 0; + + while (readrange(stdin, &r)) + total += checkrange(r); + total += checkrange(r); + + printf("%lld\n", total); + return 0; +} +\ No newline at end of file diff --git a/2/Makefile b/2/Makefile @@ -0,0 +1,10 @@ +CFLAGS=-ggdb -fsanitize=signed-integer-overflow +LDFLAGS=-fsanitize=signed-integer-overflow + +all: 2a 2b + +2a: 2a.c +2b: 2b.c + +clean: + rm 2a 2b +\ No newline at end of file diff --git a/2/input b/2/input @@ -0,0 +1 @@ +92916254-92945956,5454498003-5454580069,28-45,4615-7998,4747396917-4747534264,272993-389376,36290651-36423050,177-310,3246326-3418616,48-93,894714-949755,952007-1003147,3-16,632-1029,420-581,585519115-585673174,1041-1698,27443-39304,71589003-71823870,97-142,2790995-2837912,579556301-579617006,653443-674678,1515120817-1515176202,13504-20701,1896-3566,8359-13220,51924-98061,505196-638209,67070129-67263432,694648-751703,8892865662-8892912125 diff --git a/2/test b/2/test @@ -0,0 +1 @@ +123122-123124 +\ No newline at end of file