advent2019

Advent of Code 2019 (C99)
git clone git://bsandro.tech/advent2019
Log | Files | Refs | LICENSE

commit 1abca040b7e39f5ab2f8fc5799071195442d866e
parent 44696ec42c0aebc2ccc5fc61f13a7ff143657a5a
Author: bsandro <email@bsandro.tech>
Date:   Sun,  7 Dec 2025 00:42:00 +0200

Day 01

Diffstat:
Acputime.h | 12++++++++++++
Aday01.c | 34++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/cputime.h b/cputime.h @@ -0,0 +1,12 @@ +#pragma once +#include <time.h> +static volatile double time_started = 0; + +void __attribute((constructor)) begin() { + time_started = clock(); +} + +void __attribute((destructor)) end() { + double elapsed = clock() - time_started; + printf("Elapsed: %f s\n", elapsed/CLOCKS_PER_SEC); +} diff --git a/day01.c b/day01.c @@ -0,0 +1,34 @@ +#include <stdio.h> +#include <inttypes.h> +#include "cputime.h" + +typedef struct { + char data[16]; + int len; +} Buffer; + +int btoi(Buffer buf) { + int num = 0; + for (int i=0;i<buf.len;++i) num = num*10+buf.data[i]; + return num; +} + +int main(void) { + Buffer buf = {0}; + uint64_t p1 = 0; + uint64_t p2 = 0; + for (int c=getchar();c!=EOF;c=getchar()) { + if (c>='0'&&c<='9') { + buf.data[buf.len++] = c-'0'; + } else { + int num = btoi(buf); + buf.len = 0; + p1 += num/3-2; + for (int fuel=num/3-2; fuel>0; fuel=fuel/3-2) { + p2 += fuel; + } + } + } + printf("p1: %"PRIu64"\np2: %"PRIu64"\n", p1, p2); + return 0; +}