advent2025

Advent of Code 2025 Solutions
git clone git://bsandro.tech/advent2025
Log | Files | Refs | LICENSE

commit 47634b6c8a003dbee3444f5521e3a40dc433ae53
parent 3056359427c68f4690a0b8848a468c2b4593715c
Author: bsandro <email@bsandro.tech>
Date:   Tue,  2 Dec 2025 07:59:40 +0200

day02 p1

Diffstat:
MMakefile | 2+-
Aday02.c | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -1,7 +1,7 @@ .SUFFIXES: MAKEFLAGS+=-rR CFLAGS=-O3 -std=gnu23 -Werror -Wall -Wextra -ffast-math -march=native -mtune=native -I. -LDFLAGS=-lc +LDFLAGS=-lc -lm CC=cc .PHONY: clean diff --git a/day02.c b/day02.c @@ -0,0 +1,60 @@ +#include <stdio.h> +#include <inttypes.h> +#include <math.h> +#include "cputime.h" + +typedef struct Range { + uint64_t first; + uint64_t last; +} Range; + +inline uint64_t btoi(uint64_t buf[32], int bufs) { + uint64_t num = 0; + for (int i=0;i<bufs;++i) num = num*10+buf[i]; + return num; +} + +inline int digits(uint64_t num) { + return log10l(num)+1; +} + +inline bool is_silly(uint64_t num) { + int d = digits(num); + if (d%2==0) { + long shift = powl(10, d/2); + uint64_t top = num/shift; + uint64_t bottom = num-(top*shift); + return top==bottom; + } + return false; +} + +inline uint64_t sum_silly(Range range) { + uint64_t sum = 0; + for (uint64_t i=range.first;i<=range.last;++i) { + if (is_silly(i)) sum+=i; + } + return sum; +} + +int main(void) { + uint64_t buf[32] = {0}; + int bufs = 0; + Range range = {0}; + uint64_t p1 = 0; + for (int c=getchar();c!=EOF;c=getchar()) { + if (c>='0'&&c<='9') { + buf[bufs++] = c-'0'; + } else if (c=='-') { + range.first = btoi(buf, bufs); + bufs = 0; + } else if (c==','||c=='\n') { + range.last = btoi(buf, bufs); + bufs = 0; + //printf("%"PRIu64"->%"PRIu64"\n", range.first, range.last); + p1 += sum_silly(range); + } + } + printf("p1: %" PRIu64 "\n", p1); + return 0; +}