From 75b51d8fc85c70d17c73e2d9958a57f908c0543e Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Fri, 14 Dec 2018 00:47:34 -0500 Subject: Solve day 14 part 2 --- 2018/day14.c | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) (limited to '2018') diff --git a/2018/day14.c b/2018/day14.c index 9df7190..9d2bdd9 100644 --- a/2018/day14.c +++ b/2018/day14.c @@ -1,18 +1,17 @@ #include #include - -typedef unsigned uint; +#include struct Vec { size_t cap, len; - uint *ptr; + char *ptr; }; static struct Vec new(size_t cap) { struct Vec vec = { cap }; vec.ptr = malloc(sizeof(*vec.ptr) * cap); return vec; } -static void push(struct Vec *vec, uint val) { +static void push(struct Vec *vec, char val) { if (vec->len == vec->cap) { vec->cap *= 2; vec->ptr = realloc(vec->ptr, sizeof(*vec->ptr) * vec->cap); @@ -21,24 +20,33 @@ static void push(struct Vec *vec, uint val) { } int main() { - uint count; - scanf("%u", &count); + size_t count; + scanf("%zu", &count); + char *scores; + asprintf(&scores, "%zu", count); struct Vec vec = new(256); - push(&vec, 3); - push(&vec, 7); + push(&vec, '3'); + push(&vec, '7'); size_t elf[2] = { 0, 1 }; - for (uint i = 0; i < count + 10; ++i) { - uint sum = vec.ptr[elf[0]] + vec.ptr[elf[1]]; - if (sum / 10) push(&vec, sum / 10); - push(&vec, sum % 10); - elf[0] = (elf[0] + 1 + vec.ptr[elf[0]]) % vec.len; - elf[1] = (elf[1] + 1 + vec.ptr[elf[1]]) % vec.len; - } - - for (uint i = count; i < count + 10; ++i) { - printf("%u", vec.ptr[i]); + for (size_t i = 0; 1; ++i) { + if (i == count + 10) { + for (size_t j = count; j < count + 10; ++j) { + printf("%c", vec.ptr[j]); + } + printf("\n"); + } + char sum = (vec.ptr[elf[0]] - '0') + (vec.ptr[elf[1]] - '0'); + if (sum / 10) push(&vec, '0' + sum / 10); + push(&vec, '0' + sum % 10); + elf[0] = (elf[0] + 1 + vec.ptr[elf[0]] - '0') % vec.len; + elf[1] = (elf[1] + 1 + vec.ptr[elf[1]] - '0') % vec.len; + size_t len = strlen(scores); + if (vec.len < len) continue; + char *match = strnstr(&vec.ptr[vec.len - len - 1], scores, len + 1); + if (!match) continue; + printf("%zu\n", match - vec.ptr); + break; } - printf("\n"); } -- cgit 1.4.1 ='/src/log/?id=1dde71aa57ac73f9f15b8a9666c36b2173bb4592&showmsg=1&follow=1'>root/www/photo.causal.agency/2024-05-06/IMG_1998.txt (unfollow)
Commit message (Collapse)Author
2024-10-07Add photos from September 28June McEnroe
2024-10-07Add photos from September 22June McEnroe
2024-09-25Add photos from September 15June McEnroe
2024-09-24Add photos from September 14June McEnroe
2024-09-24Add photos from September 12June McEnroe
2024-09-24Add photos from September 7June McEnroe
2024-09-24Allow not having descriptionsJune McEnroe
I'm sorry, I can't keep writing descriptions. It makes posting photos take too long, I often don't know the words for what I'm looking at, and a good description is an entirely different work of art than the photo I took, and I'm just a photographer. It's visual art.
2024-09-23Automatically select the last used lens for a bodyJune McEnroe
2024-09-19Add photos from September 5June McEnroe
Had to prefix the folder number onto these file names manually because they must have come out of a different scanner or something.
2024-09-15Add some more film stocks to the listJune McEnroe
2024-09-13Add photos from September 2June McEnroe
2024-09-13Add Fomapan 200 to films listJune McEnroe
2024-09-10Add August 29 picnic photosJune McEnroe
2024-09-08Apply some bold to trips renderingJune McEnroe
This seems easier to visually scan. The only other thing I'd like is a nicer date rendering but JavaScript is useless for that.
2024-09-08Render trips hopefully more efficientlyJune McEnroe
2024-09-08Allow removing bodies and lensesJune McEnroe
2024-09-08Limit body width so it looks less silly on desktopJune McEnroe
2024-09-07Handle no film being loadedJune McEnroe
2024-09-07Fancy up the text a littleJune McEnroe