aocc22

Advent of Code 2022
git clone git://www.tkruger.se/aocc22.git
Log | Files | Refs | README

commit 5306063684cffd9fb3ac538ae5c93721c6970b0f
parent 920a400eeef874eea357335de03a0827823093f6
Author: olikru <olikru@tkruger.se>
Date:   Mon,  8 Jan 2024 16:14:10 +0100

clang format

Diffstat:
Mday1/common.c | 59+++++++++++++++++++++++++++++------------------------------
Mday1/common.h | 10+++++-----
Mday1/playa.c | 18+++++++++---------
Mday1/uppga.c | 41++++++++++++++++++++---------------------
Mday1/uppgb.c | 54+++++++++++++++++++++++++-----------------------------
Mday10/common.c | 18+++++++++---------
Mday10/common.h | 6+++---
Mday10/uppga.c | 11+++++------
Mday10/uppgb.c | 16++++++++--------
Mday11/common.c | 90+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mday11/common.h | 22+++++++++++-----------
Mday11/uppga.c | 44+++++++++++++++++++++-----------------------
Mday11/uppgb.c | 27+++++++++++++--------------
Mday12/common.c | 105+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mday12/common.h | 17+++++++++--------
Mday12/uppga.c | 21++++++++++-----------
Mday12/uppgb.c | 37++++++++++++++++++-------------------
Mday13/common.c | 167+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mday13/common.h | 18+++++++++---------
Mday13/uppga.c | 35+++++++++++++++++------------------
Mday13/uppgb.c | 60++++++++++++++++++++++++++++++------------------------------
Mday14/common.c | 106+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Mday14/common.h | 12++++++------
Mday14/uppga.c | 12++++++------
Mday14/uppgb.c | 14+++++++-------
Mday15/common.c | 62+++++++++++++++++++++++++++-----------------------------------
Mday15/common.h | 19+++++++++----------
Mday15/uppga.c | 18+++++++-----------
Mday15/uppgb.c | 97+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mday15/uppgb_old.c | 27+++++++++++++--------------
Mday16/common.c | 5+----
Mday16/common.h | 6+++---
Mday16/uppga.c | 362+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mday16/uppgb.c | 428+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mday17/common.c | 5+----
Mday17/common.h | 6+++---
Mday17/uppga.c | 141++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mday17/uppgb.c | 401+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mday17/uppgc.c | 151+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mday18/common.c | 5+----
Mday18/common.h | 6+++---
Mday18/uppga.c | 122++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mday18/uppgb.c | 424++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mday19/common.c | 33+++++++++++++++++----------------
Mday19/common.h | 10+++++-----
Mday19/uppga.c | 289++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mday19/uppgb.c | 438+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Mday2/uppga.c | 47++++++++++++++++++++++-------------------------
Mday2/uppgb.c | 35++++++++++++++++-------------------
Mday20/common.c | 9++++-----
Mday20/common.h | 6+++---
Mday20/uppga.c | 134++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mday20/uppgb.c | 140++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mday21/bbrute.c | 34+++++++++++++++++-----------------
Mday21/common.c | 222+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mday21/common.h | 28++++++++++++++--------------
Mday21/uppga.c | 9++++-----
Mday21/uppgb.c | 35+++++++++++++++++------------------
Mday22/common.c | 781+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mday22/common.h | 31++++++++++++++++---------------
Mday22/uppga.c | 23+++++++++++------------
Mday22/uppgb.c | 23+++++++++++------------
Mday23/common.c | 428+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mday23/common.h | 29+++++++++++++++--------------
Mday23/uppga.c | 29++++++++++++++---------------
Mday23/uppgb.c | 27+++++++++++++--------------
Mday24/common.c | 689++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mday24/common.h | 31++++++++++++++++---------------
Mday24/uppga.c | 164++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mday24/uppgb.c | 9++++-----
Mday25/common.c | 123+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mday25/common.h | 8++++----
Mday25/uppga.c | 19+++++++++----------
Mday3/common.c | 17+++++++----------
Mday3/common.h | 8++++----
Mday3/uppga.c | 44++++++++++++++++++++++++++------------------
Mday3/uppgb.c | 45+++++++++++++++++++++++++--------------------
Mday4/common.c | 17++++++++++-------
Mday4/common.h | 4++--
Mday4/uppga.c | 26++++++++++++--------------
Mday4/uppgb.c | 29+++++++++++++----------------
Mday5/common.c | 133++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mday5/common.h | 15++++++++-------
Mday5/uppga.c | 41++++++++++++++++++++---------------------
Mday5/uppgb.c | 35+++++++++++++++++------------------
Mday6/common.c | 12++++++------
Mday6/common.h | 6+++---
Mday6/uppga.c | 25++++++++++++-------------
Mday6/uppgb.c | 25++++++++++++-------------
Mday7/common.c | 191++++++++++++++++++++++++++++++++++++++-----------------------------------------
Mday7/common.h | 24++++++++++++------------
Mday7/uppga.c | 39+++++++++++++++++++--------------------
Mday7/uppgb.c | 43+++++++++++++++++++++----------------------
Mday8/common.c | 12++++++------
Mday8/common.h | 6+++---
Mday8/uppga.c | 62+++++++++++++++++++++++++++++++++-----------------------------
Mday8/uppgb.c | 53++++++++++++++++++++++++++++-------------------------
Mday9/common.c | 62++++++++++++++++++++++++++++++++++----------------------------
Mday9/common.h | 10+++++-----
Mday9/uppga.c | 79+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mday9/uppgb.c | 83+++++++++++++++++++++++++++++++++++++++----------------------------------------
Mtemplate/common.c | 5+----
Mtemplate/common.h | 6+++---
Mtemplate/uppga.c | 7++-----
Mtemplate/uppgb.c | 6++----
Mutils/ht.c | 24+++++++++---------------
Mutils/ht.h | 16++++++++--------
Mutils/reading.c | 182++++++++++++++++++++++++++++++++++++++++----------------------------------------
Mutils/reading.h | 19+++++++++----------
Mutils/sd.c | 78++++++++++++++++++++++++++++++++++--------------------------------------------
Mutils/sd.h | 52++++++++++++++++++++++++++--------------------------
Mutils/smallset.c | 123+++++++++++++++++++++++++++++++++++++------------------------------------------
Mutils/smallset.h | 26+++++++++++++-------------
Mutils/stack_sd.c | 78++++++++++++++++++++++++++++++++++--------------------------------------------
Mutils/stack_sd.h | 50+++++++++++++++++++++++++-------------------------
Mutils/stack_str.c | 56++++++++++++++++++++++++--------------------------------
Mutils/stack_str.h | 41+++++++++++++++++++++--------------------
Mutils/stack_u64.c | 56+++++++++++++++++++++++++-------------------------------
Mutils/stack_u64.h | 36++++++++++++++++++------------------
119 files changed, 4561 insertions(+), 4634 deletions(-)

diff --git a/day1/common.c b/day1/common.c @@ -1,44 +1,43 @@ #include "common.h" -size_t count_elves(char** lines, size_t nlines) -{ - size_t c = 0; - size_t i = 0; - for(i = 0; i < nlines; i++) { - if(lines[i][0] == '\n') { - c++; - } +size_t count_elves(char **lines, size_t nlines) { + size_t c = 0; + size_t i = 0; + for (i = 0; i < nlines; i++) { + if (lines[i][0] == '\n') { + c++; } + } - return c+1; + return c + 1; } -void get_estack(stack_u64* estack, size_t nelves, char** lines, size_t nlines) -{ - size_t i; - size_t celf = 0; +void get_estack(stack_u64 *estack, size_t nelves, char **lines, size_t nlines) { + size_t i; + size_t celf = 0; - if(nelves == 0) return; + if (nelves == 0) + return; - stack_u64_init(&estack[0]); + stack_u64_init(&estack[0]); - for(i = 0; i < nlines; i++) { - assert(celf < nelves); + for (i = 0; i < nlines; i++) { + assert(celf < nelves); - if(lines[i][0] == '\n') { - celf++; - stack_u64_init(&estack[celf]); - } else { - stack_u64_push(&estack[celf], strtoul(lines[i], NULL, 10)); - } + if (lines[i][0] == '\n') { + celf++; + stack_u64_init(&estack[celf]); + } else { + stack_u64_push(&estack[celf], strtoul(lines[i], NULL, 10)); } + } } -void comp_sums(uint64_t* sums, stack_u64* estack, size_t nelves) -{ - size_t i,j; - for(i = 0; i < nelves; i++) { - sums[i] = 0; - for(j = 0; j < estack[i].nmemb; j++) sums[i] += estack[i].data[j]; - } +void comp_sums(uint64_t *sums, stack_u64 *estack, size_t nelves) { + size_t i, j; + for (i = 0; i < nelves; i++) { + sums[i] = 0; + for (j = 0; j < estack[i].nmemb; j++) + sums[i] += estack[i].data[j]; + } } diff --git a/day1/common.h b/day1/common.h @@ -1,14 +1,14 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> #include <stack_u64.h> +#include <stdint.h> +#include <stdio.h> -size_t count_elves(char** lines, size_t nlines); -void get_estack(stack_u64* estack, size_t nelves, char** lines, size_t nlines); -void comp_sums(uint64_t* sums, stack_u64* estack, size_t nelves); +size_t count_elves(char **lines, size_t nlines); +void get_estack(stack_u64 *estack, size_t nelves, char **lines, size_t nlines); +void comp_sums(uint64_t *sums, stack_u64 *estack, size_t nelves); #endif diff --git a/day1/playa.c b/day1/playa.c @@ -1,21 +1,21 @@ #include <stdio.h> -int main() -{ - FILE* f = fopen("input.txt", "r"); +int main() { + FILE *f = fopen("input.txt", "r"); - int c, a=0, b=0, lc=0, ca=0, m=0; - while((c = getc(f)) != EOF) { - if(c == '\n') { - if(lc == '\n') { - if(m < b) m = b; + int c, a = 0, b = 0, lc = 0, ca = 0, m = 0; + while ((c = getc(f)) != EOF) { + if (c == '\n') { + if (lc == '\n') { + if (m < b) + m = b; b = 0; } else { b += ca; ca = 0; } } else { - ca = 10*ca + (int) (c - '0'); + ca = 10 * ca + (int)(c - '0'); } lc = c; } diff --git a/day1/uppga.c b/day1/uppga.c @@ -1,28 +1,27 @@ #include "common.h" -uint64_t max(uint64_t* sums, size_t nelves) -{ - size_t i; - uint64_t cmax = 0; - for(i = 0; i < nelves; i++) { - if(sums[i] > cmax) cmax = sums[i]; - } +uint64_t max(uint64_t *sums, size_t nelves) { + size_t i; + uint64_t cmax = 0; + for (i = 0; i < nelves; i++) { + if (sums[i] > cmax) + cmax = sums[i]; + } - return cmax; + return cmax; } -int main(int argc, char** argv) -{ - char** lines; - size_t nlines = readlines(&lines, "input.txt"); - printf("#lines: %zu\n", nlines); - size_t nelves = count_elves(lines, nlines); - printf("#elves: %zu\n", nelves); +int main(int argc, char **argv) { + char **lines; + size_t nlines = readlines(&lines, "input.txt"); + printf("#lines: %zu\n", nlines); + size_t nelves = count_elves(lines, nlines); + printf("#elves: %zu\n", nelves); - stack_u64 estack[nelves]; - get_estack(estack, nelves, lines, nlines); - uint64_t sums[nelves]; - comp_sums(sums, estack, nelves); - - printf("max: %llu\n", max(sums, nelves)); + stack_u64 estack[nelves]; + get_estack(estack, nelves, lines, nlines); + uint64_t sums[nelves]; + comp_sums(sums, estack, nelves); + + printf("max: %llu\n", max(sums, nelves)); } diff --git a/day1/uppgb.c b/day1/uppgb.c @@ -1,41 +1,37 @@ #include "common.h" -static inline int _rcmpu64(uint64_t n, uint64_t m) -{ - if(n < m) { - return -1; - } else if(n == m) { - return 0; - } +static inline int _rcmpu64(uint64_t n, uint64_t m) { + if (n < m) { + return -1; + } else if (n == m) { + return 0; + } - return 1; + return 1; } -static int cmpu64(const void *p1, const void *p2) -{ - return _rcmpu64(*(uint64_t*) p1, *(uint64_t*) p2); +static int cmpu64(const void *p1, const void *p2) { + return _rcmpu64(*(uint64_t *)p1, *(uint64_t *)p2); } -void sort(uint64_t* sums, size_t nelves) -{ - qsort(sums, nelves, sizeof(*sums), cmpu64); +void sort(uint64_t *sums, size_t nelves) { + qsort(sums, nelves, sizeof(*sums), cmpu64); } -int main(int argc, char** argv) -{ - char** lines; - size_t nlines = readlines(&lines, "input.txt"); - printf("#lines: %zu\n", nlines); - size_t nelves = count_elves(lines, nlines); - printf("#elves: %zu\n", nelves); +int main(int argc, char **argv) { + char **lines; + size_t nlines = readlines(&lines, "input.txt"); + printf("#lines: %zu\n", nlines); + size_t nelves = count_elves(lines, nlines); + printf("#elves: %zu\n", nelves); - stack_u64 estack[nelves]; - get_estack(estack, nelves, lines, nlines); - uint64_t sums[nelves]; - comp_sums(sums, estack, nelves); + stack_u64 estack[nelves]; + get_estack(estack, nelves, lines, nlines); + uint64_t sums[nelves]; + comp_sums(sums, estack, nelves); - sort(sums, nelves); - uint64_t* top = &sums[nelves-3]; - uint64_t tsum = top[0] + top[1] + top[2]; - printf("sum: %llu\n", tsum); + sort(sums, nelves); + uint64_t *top = &sums[nelves - 3]; + uint64_t tsum = top[0] + top[1] + top[2]; + printf("sum: %llu\n", tsum); } diff --git a/day10/common.c b/day10/common.c @@ -2,25 +2,25 @@ #define MAX_CYCLES 240 -int* parse(char** lines, size_t nlines) -{ - int* r = malloc(MAX_CYCLES * sizeof(*r) + 2); +int *parse(char **lines, size_t nlines) { + int *r = malloc(MAX_CYCLES * sizeof(*r) + 2); r[0] = 1; int t; size_t i, j, c = 0; - for(i = 0; i < nlines; i++) { - if(lines[i][0] == 'n') { - r[c+1] = r[c]; + for (i = 0; i < nlines; i++) { + if (lines[i][0] == 'n') { + r[c + 1] = r[c]; c++; } else { sscanf(lines[i], "addx %d\n", &t); - r[c+1] = r[c]; - r[c+2] = r[c] + t; + r[c + 1] = r[c]; + r[c + 2] = r[c] + t; c += 2; } - if(c >= 240) break; + if (c >= 240) + break; } return r; diff --git a/day10/common.h b/day10/common.h @@ -1,11 +1,11 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> -int* parse(char** lines, size_t nlines); +int *parse(char **lines, size_t nlines); #endif diff --git a/day10/uppga.c b/day10/uppga.c @@ -1,17 +1,16 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - int* r = parse(lines, nlines); + int *r = parse(lines, nlines); int strength = 0; size_t i; - for(i = 20; i < 221; i += 40) - strength += i * r[i-1]; + for (i = 20; i < 221; i += 40) + strength += i * r[i - 1]; printf("%d\n", strength); } diff --git a/day10/uppgb.c b/day10/uppgb.c @@ -1,16 +1,16 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - int* r = parse(lines, nlines); + int *r = parse(lines, nlines); - size_t i,j; - for(j = 0; j < 6; j++) { - for(i = 0; i < 40; i++) { - if(r[j*40+i] == i-1 || r[j*40+i] == i+1 || r[j*40+i] == i) + size_t i, j; + for (j = 0; j < 6; j++) { + for (i = 0; i < 40; i++) { + if (r[j * 40 + i] == i - 1 || r[j * 40 + i] == i + 1 || + r[j * 40 + i] == i) printf("#"); else printf("."); diff --git a/day11/common.c b/day11/common.c @@ -1,23 +1,24 @@ #include "common.h" -monkis parse_monkis(char** lines, size_t nlines, int mods) -{ +monkis parse_monkis(char **lines, size_t nlines, int mods) { monkis r; - size_t i,j; + size_t i, j; uint64_t t; r.mod = malloc(nlines * sizeof(*r.mod)); r.n = 0; r.nitems = 0; - for(i = 0; i < nlines; i++) { - if(lines[i][0] == 'M') r.n++; - else if(strlen(lines[i]) > 2) { - if(lines[i][2] == 'S') { - char* p = lines[i]; - while((p = sread_next_u64(&t, p)) != NULL) r.nitems++; - } else if(lines[i][2] == 'T') { - sread_next_u64(&(r.mod[r.n-1]), lines[i]); + for (i = 0; i < nlines; i++) { + if (lines[i][0] == 'M') + r.n++; + else if (strlen(lines[i]) > 2) { + if (lines[i][2] == 'S') { + char *p = lines[i]; + while ((p = sread_next_u64(&t, p)) != NULL) + r.nitems++; + } else if (lines[i][2] == 'T') { + sread_next_u64(&(r.mod[r.n - 1]), lines[i]); } } } @@ -26,7 +27,9 @@ monkis parse_monkis(char** lines, size_t nlines, int mods) r.rmod = malloc(r.n * sizeof(*r.rmod)); memcpy(r.rmod, r.mod, r.n * sizeof(*r.rmod)); - if(!mods) for(i = 0; i < r.n; i++) r.mod[i] = UINT64_MAX; + if (!mods) + for (i = 0; i < r.n; i++) + r.mod[i] = UINT64_MAX; r.fnext = malloc(r.n * sizeof(*r.fnext)); r.tnext = malloc(r.n * sizeof(*r.tnext)); @@ -37,20 +40,23 @@ monkis parse_monkis(char** lines, size_t nlines, int mods) r.items = malloc(r.nitems * r.n * sizeof(*r.items)); size_t cm = 0, ci = 0; - for(i = 0; i < nlines; i++) { - if(strlen(lines[i]) > 2) { - if(lines[i][4] == 'I') { + for (i = 0; i < nlines; i++) { + if (strlen(lines[i]) > 2) { + if (lines[i][4] == 'I') { sread_next_u64(&t, lines[i]); - if(lines[i][7] == 't') r.tnext[cm] = t; - else r.fnext[cm] = t; - } else if(lines[i][2] == 'S') { + if (lines[i][7] == 't') + r.tnext[cm] = t; + else + r.fnext[cm] = t; + } else if (lines[i][2] == 'S') { SIMPLEQ_INIT(&r.heads[cm]); - char* p = lines[i]; - while((p = sread_next_u64(&t, p)) != NULL) { - struct entry* new_entry = malloc(sizeof(*new_entry)); + char *p = lines[i]; + while ((p = sread_next_u64(&t, p)) != NULL) { + struct entry *new_entry = malloc(sizeof(*new_entry)); new_entry->index = ci; SIMPLEQ_INSERT_TAIL(&r.heads[cm], new_entry, entries); - for(j = 0; j < r.n; j++) r.items[ci * r.n + j] = t % r.mod[j]; + for (j = 0; j < r.n; j++) + r.items[ci * r.n + j] = t % r.mod[j]; ci++; } } @@ -61,18 +67,18 @@ monkis parse_monkis(char** lines, size_t nlines, int mods) return r; } -void mmonkey(monkis m, size_t i) -{ - uint64_t* tmp; - struct entry* np; +void mmonkey(monkis m, size_t i) { + uint64_t *tmp; + struct entry *np; size_t j; - while(!SIMPLEQ_EMPTY(&m.heads[i])) { + while (!SIMPLEQ_EMPTY(&m.heads[i])) { m.inspections[i]++; np = SIMPLEQ_FIRST(&m.heads[i]); SIMPLEQ_REMOVE_HEAD(&m.heads[i], entries); tmp = &(m.items[np->index * m.n]); - for(j = 0; j < m.n; j++) tmp[j] = (m.operate)(tmp[j], i) % m.mod[j]; + for (j = 0; j < m.n; j++) + tmp[j] = (m.operate)(tmp[j], i) % m.mod[j]; size_t new_index = (tmp[i] % m.rmod[i]) == 0 ? m.tnext[i] : m.fnext[i]; @@ -80,29 +86,27 @@ void mmonkey(monkis m, size_t i) } } -void mround(monkis m) -{ +void mround(monkis m) { size_t i; - for(i = 0; i < m.n; i++) mmonkey(m, i); + for (i = 0; i < m.n; i++) + mmonkey(m, i); } -uint64_t get_result(monkis m) -{ - uint64_t tmax[2] = {0,0}; +uint64_t get_result(monkis m) { + uint64_t tmax[2] = {0, 0}; size_t i; - for(i = 0; i < m.n; i++) { - if(m.inspections[i] > tmax[0]) + for (i = 0; i < m.n; i++) { + if (m.inspections[i] > tmax[0]) tmax[0] = m.inspections[i]; - if(tmax[0] > tmax[1]) + if (tmax[0] > tmax[1]) tmax[0] ^= tmax[1] ^= tmax[0] ^= tmax[1]; } - return tmax[0]*tmax[1]; + return tmax[0] * tmax[1]; } -void let_the_monkis_out(monkis m) -{ +void let_the_monkis_out(monkis m) { free(m.mod); free(m.tnext); free(m.fnext); @@ -110,9 +114,9 @@ void let_the_monkis_out(monkis m) free(m.items); size_t i; - struct entry* t; - for(i = 0; i < m.n; i++) { - while(!SIMPLEQ_EMPTY(&m.heads[i])) { + struct entry *t; + for (i = 0; i < m.n; i++) { + while (!SIMPLEQ_EMPTY(&m.heads[i])) { t = SIMPLEQ_FIRST(&m.heads[i]); SIMPLEQ_REMOVE_HEAD(&m.heads[i], entries); free(t); diff --git a/day11/common.h b/day11/common.h @@ -1,10 +1,10 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> #include <sys/queue.h> struct entry { @@ -14,23 +14,23 @@ struct entry { typedef struct { size_t n; - uint64_t* mod; - uint64_t* rmod; - size_t* fnext; - size_t* tnext; + uint64_t *mod; + uint64_t *rmod; + size_t *fnext; + size_t *tnext; - uint64_t* inspections; + uint64_t *inspections; size_t nitems; - uint64_t* items; + uint64_t *items; - SIMPLEQ_HEAD(listhead, entry)* heads; + SIMPLEQ_HEAD(listhead, entry) * heads; uint64_t (*operate)(uint64_t, size_t); -} monkis; +} monkis; void print_monkis(monkis m); -monkis parse_monkis(char** lines, size_t nlines, int mods); +monkis parse_monkis(char **lines, size_t nlines, int mods); void mmonkey(monkis m, size_t i); void mround(monkis m); uint64_t get_result(monkis m); diff --git a/day11/uppga.c b/day11/uppga.c @@ -1,38 +1,36 @@ #include "common.h" // I really don't want to parse this from input -uint64_t operation(uint64_t x, size_t i) -{ - if(i == 0) { - return (x * 7)/3; - } else if(i == 1) { - return (x * 13)/3; - } else if(i == 2) { - return (x + 1)/3; - } else if(i == 3) { - return (x * x)/3; - } else if(i == 4) { - return (x + 7)/3; - } else if(i == 5) { - return (x + 6)/3; - } else if(i == 6) { - return (x + 4)/3; +uint64_t operation(uint64_t x, size_t i) { + if (i == 0) { + return (x * 7) / 3; + } else if (i == 1) { + return (x * 13) / 3; + } else if (i == 2) { + return (x + 1) / 3; + } else if (i == 3) { + return (x * x) / 3; + } else if (i == 4) { + return (x + 7) / 3; + } else if (i == 5) { + return (x + 6) / 3; + } else if (i == 6) { + return (x + 4) / 3; } - return (x + 8)/3; + return (x + 8) / 3; } - -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); monkis m = parse_monkis(lines, nlines, 0); m.operate = operation; size_t i; - for(i = 0; i < 20; i++) mround(m); - + for (i = 0; i < 20; i++) + mround(m); + printf("%llu\n", get_result(m)); let_the_monkis_out(m); diff --git a/day11/uppgb.c b/day11/uppgb.c @@ -1,37 +1,36 @@ #include "common.h" // I really don't want to parse this from input -uint64_t operation(uint64_t x, size_t i) -{ - if(i == 0) { +uint64_t operation(uint64_t x, size_t i) { + if (i == 0) { return x * 7; - } else if(i == 1) { + } else if (i == 1) { return x * 13; - } else if(i == 2) { + } else if (i == 2) { return x + 1; - } else if(i == 3) { + } else if (i == 3) { return x * x; - } else if(i == 4) { + } else if (i == 4) { return x + 7; - } else if(i == 5) { + } else if (i == 5) { return x + 6; - } else if(i == 6) { + } else if (i == 6) { return x + 4; } return x + 8; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); monkis m = parse_monkis(lines, nlines, 1); m.operate = operation; size_t i; - for(i = 0; i < 10000; i++) mround(m); - + for (i = 0; i < 10000; i++) + mround(m); + printf("%llu\n", get_result(m)); let_the_monkis_out(m); diff --git a/day12/common.c b/day12/common.c @@ -1,62 +1,65 @@ #include "common.h" -uint8_t** parse(size_t* h, size_t* w, size_t* pos, char** lines, size_t nlines) -{ - size_t i,j; +uint8_t **parse(size_t *h, size_t *w, size_t *pos, char **lines, + size_t nlines) { + size_t i, j; *h = nlines; - *w = strlen(lines[0])-1; - - uint8_t** grid = malloc(*h * sizeof(*grid)); - for(i = 0; i < *h; i++) - grid[i] = malloc(*w * sizeof(**grid)); - - for(i = 0; i < nlines; i++) { - for(j = 0; j < *w; j++) { - if(lines[i][j] == 'S') { - pos[0] = i; pos[1] = j; - lines[i][j] = 'a'; - } else if(lines[i][j] == 'E') { - pos[2] = i; pos[3] = j; - lines[i][j] = 'z'; - } - grid[i][j] = (uint8_t) (lines[i][j] - 'a'); - } - } - - return grid; -} + *w = strlen(lines[0]) - 1; + + uint8_t **grid = malloc(*h * sizeof(*grid)); + for (i = 0; i < *h; i++) + grid[i] = malloc(*w * sizeof(**grid)); + for (i = 0; i < nlines; i++) { + for (j = 0; j < *w; j++) { + if (lines[i][j] == 'S') { + pos[0] = i; + pos[1] = j; + lines[i][j] = 'a'; + } else if (lines[i][j] == 'E') { + pos[2] = i; + pos[3] = j; + lines[i][j] = 'z'; + } + grid[i][j] = (uint8_t)(lines[i][j] - 'a'); + } + } + + return grid; +} -void calculate_steps(uint64_t* stepsto, size_t ex, size_t ey, size_t h, size_t w, uint8_t** grid) -{ - size_t i; - SIMPLEQ_HEAD(listhead, entry) head = SIMPLEQ_HEAD_INITIALIZER(head); +void calculate_steps(uint64_t *stepsto, size_t ex, size_t ey, size_t h, + size_t w, uint8_t **grid) { + size_t i; + SIMPLEQ_HEAD(listhead, entry) head = SIMPLEQ_HEAD_INITIALIZER(head); - struct entry *pos = malloc(sizeof(*pos)); - pos->x = ex; pos->y = ey; - SIMPLEQ_INSERT_TAIL(&head, pos, entries); + struct entry *pos = malloc(sizeof(*pos)); + pos->x = ex; + pos->y = ey; + SIMPLEQ_INSERT_TAIL(&head, pos, entries); - while (!SIMPLEQ_EMPTY(&head)) { - pos = SIMPLEQ_FIRST(&head); - size_t x = pos->x, y = pos->y; + while (!SIMPLEQ_EMPTY(&head)) { + pos = SIMPLEQ_FIRST(&head); + size_t x = pos->x, y = pos->y; SIMPLEQ_REMOVE_HEAD(&head, entries); - int delta[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; - for(i = 0; i < 4; i++) { - int cx = (int) x + delta[i][0]; - int cy = (int) y + delta[i][1]; - if(cx < 0 || cy < 0 || cx >= h || cy >= w) { - continue; - } - if(grid[cx][cy] + 1 >= grid[x][y]) { - if(stepsto[x * w + y] + 1 < stepsto[cx * w + cy]) { - stepsto[cx * w + cy] = stepsto[x * w + y] + 1; - struct entry *npos = malloc(sizeof(*npos)); - npos->x = cx; npos->y = cy; - SIMPLEQ_INSERT_TAIL(&head, npos, entries); - } - } - } + int delta[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + for (i = 0; i < 4; i++) { + int cx = (int)x + delta[i][0]; + int cy = (int)y + delta[i][1]; + if (cx < 0 || cy < 0 || cx >= h || cy >= w) { + continue; + } + if (grid[cx][cy] + 1 >= grid[x][y]) { + if (stepsto[x * w + y] + 1 < stepsto[cx * w + cy]) { + stepsto[cx * w + cy] = stepsto[x * w + y] + 1; + struct entry *npos = malloc(sizeof(*npos)); + npos->x = cx; + npos->y = cy; + SIMPLEQ_INSERT_TAIL(&head, npos, entries); + } + } + } free(pos); - } + } } diff --git a/day12/common.h b/day12/common.h @@ -1,20 +1,21 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> -#include <sys/queue.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> +#include <sys/queue.h> struct entry { - size_t x; - size_t y; + size_t x; + size_t y; - SIMPLEQ_ENTRY(entry) entries; + SIMPLEQ_ENTRY(entry) entries; }; -uint8_t** parse(size_t* h, size_t* w, size_t* pos, char** lines, size_t nlines); -void calculate_steps(uint64_t* stepsto, size_t ex, size_t ey, size_t h, size_t w, uint8_t** grid); +uint8_t **parse(size_t *h, size_t *w, size_t *pos, char **lines, size_t nlines); +void calculate_steps(uint64_t *stepsto, size_t ex, size_t ey, size_t h, + size_t w, uint8_t **grid); #endif diff --git a/day12/uppga.c b/day12/uppga.c @@ -1,19 +1,18 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - size_t h,w; - size_t pos[4]; - uint8_t** grid = parse(&h, &w, pos, lines, nlines); + size_t h, w; + size_t pos[4]; + uint8_t **grid = parse(&h, &w, pos, lines, nlines); - uint64_t* stepsto = malloc(h * w * sizeof(*stepsto)); - memset(stepsto, 0xff, h * w * sizeof(*stepsto)); + uint64_t *stepsto = malloc(h * w * sizeof(*stepsto)); + memset(stepsto, 0xff, h * w * sizeof(*stepsto)); - stepsto[pos[2] * w + pos[3]] = 0; - calculate_steps(stepsto, pos[2], pos[3], h, w, grid); + stepsto[pos[2] * w + pos[3]] = 0; + calculate_steps(stepsto, pos[2], pos[3], h, w, grid); - printf("%llu\n", stepsto[pos[0] * w + pos[1]]); + printf("%llu\n", stepsto[pos[0] * w + pos[1]]); } diff --git a/day12/uppgb.c b/day12/uppgb.c @@ -1,28 +1,27 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - size_t h,w; - size_t pos[4]; - uint8_t** grid = parse(&h, &w, pos, lines, nlines); + size_t h, w; + size_t pos[4]; + uint8_t **grid = parse(&h, &w, pos, lines, nlines); - uint64_t* stepsto = malloc(h * w * sizeof(*stepsto)); - memset(stepsto, 0xff, h * w * sizeof(*stepsto)); + uint64_t *stepsto = malloc(h * w * sizeof(*stepsto)); + memset(stepsto, 0xff, h * w * sizeof(*stepsto)); - stepsto[pos[2] * w + pos[3]] = 0; - calculate_steps(stepsto, pos[2], pos[3], h, w, grid); + stepsto[pos[2] * w + pos[3]] = 0; + calculate_steps(stepsto, pos[2], pos[3], h, w, grid); - uint64_t minsteps = UINT64_MAX; - size_t i,j; - for(i = 0; i < h; i++) { - for(j = 0; j < w; j++) { - if(grid[i][j] == 0 && minsteps > stepsto[i * w + j]) - minsteps = stepsto[i * w + j]; - } - } + uint64_t minsteps = UINT64_MAX; + size_t i, j; + for (i = 0; i < h; i++) { + for (j = 0; j < w; j++) { + if (grid[i][j] == 0 && minsteps > stepsto[i * w + j]) + minsteps = stepsto[i * w + j]; + } + } - printf("%llu\n", minsteps); + printf("%llu\n", minsteps); } diff --git a/day13/common.c b/day13/common.c @@ -1,92 +1,97 @@ #include "common.h" -int compare(const lori* a, const lori* b) -{ - if(a->head != NULL && b->head != NULL) { - size_t i = 0; - lori* af = SIMPLEQ_FIRST(a->head); - lori* bf = SIMPLEQ_FIRST(b->head); - while(af != NULL && bf != NULL) { - int comp = compare(af, bf); - if(comp != 0) return comp; - af = SIMPLEQ_NEXT(af, entries); - bf = SIMPLEQ_NEXT(bf, entries); - } - if(af != NULL) return 1; - else if(bf != NULL) return -1; - return 0; - } else if(a->head != NULL || b->head != NULL) { - int comp = 0; - if(a->head != NULL) { - lori* af = SIMPLEQ_FIRST(a->head); - if(af == NULL) return -1; - comp = compare(af, b); - if(comp != 0) return comp; - if(SIMPLEQ_NEXT(af, entries) == NULL) return 0; - return 1; - } else { - lori* bf = SIMPLEQ_FIRST(b->head); - if(bf == NULL) return 1; - comp = compare(a, bf); - if(comp != 0) return comp; - if(SIMPLEQ_NEXT(bf, entries) == NULL) return 0; - return -1; - } - return comp; - } - if(a->v < b->v) { - return -1; - } else if(b->v < a->v) { - return 1; - } - return 0; +int compare(const lori *a, const lori *b) { + if (a->head != NULL && b->head != NULL) { + size_t i = 0; + lori *af = SIMPLEQ_FIRST(a->head); + lori *bf = SIMPLEQ_FIRST(b->head); + while (af != NULL && bf != NULL) { + int comp = compare(af, bf); + if (comp != 0) + return comp; + af = SIMPLEQ_NEXT(af, entries); + bf = SIMPLEQ_NEXT(bf, entries); + } + if (af != NULL) + return 1; + else if (bf != NULL) + return -1; + return 0; + } else if (a->head != NULL || b->head != NULL) { + int comp = 0; + if (a->head != NULL) { + lori *af = SIMPLEQ_FIRST(a->head); + if (af == NULL) + return -1; + comp = compare(af, b); + if (comp != 0) + return comp; + if (SIMPLEQ_NEXT(af, entries) == NULL) + return 0; + return 1; + } else { + lori *bf = SIMPLEQ_FIRST(b->head); + if (bf == NULL) + return 1; + comp = compare(a, bf); + if (comp != 0) + return comp; + if (SIMPLEQ_NEXT(bf, entries) == NULL) + return 0; + return -1; + } + return comp; + } + if (a->v < b->v) { + return -1; + } else if (b->v < a->v) { + return 1; + } + return 0; } -void printlori(const lori* x) -{ - if(x->head != NULL) { - printf("["); - lori* np; - SIMPLEQ_FOREACH(np, x->head, entries) { - printlori(np); - } - printf("]"); - } else { - printf("%llu,", x->v); - } +void printlori(const lori *x) { + if (x->head != NULL) { + printf("["); + lori *np; + SIMPLEQ_FOREACH(np, x->head, entries) { printlori(np); } + printf("]"); + } else { + printf("%llu,", x->v); + } } -lori* parseline(char* line, char** next) -{ - if(line == NULL || line[0] == '\0') { - next = NULL; - return NULL; - } +lori *parseline(char *line, char **next) { + if (line == NULL || line[0] == '\0') { + next = NULL; + return NULL; + } - lori* r = malloc(sizeof(*r)); + lori *r = malloc(sizeof(*r)); - if('0' <= line[0] && line[0] <= '9') { - r->head = NULL; - *next = sread_next_u64(&(r->v), line); - } else if(line[0] == ',') { - *next = line+1; - free(r); - return NULL; - } else { - r->v = UINT64_MAX; - r->head = malloc(sizeof(*r->head)); - SIMPLEQ_INIT(r->head); + if ('0' <= line[0] && line[0] <= '9') { + r->head = NULL; + *next = sread_next_u64(&(r->v), line); + } else if (line[0] == ',') { + *next = line + 1; + free(r); + return NULL; + } else { + r->v = UINT64_MAX; + r->head = malloc(sizeof(*r->head)); + SIMPLEQ_INIT(r->head); - lori* e; - line++; - char* nn = &line[1]; - while(line[0] != ']') { - e = parseline(line, &nn); - if(e != NULL) SIMPLEQ_INSERT_TAIL(r->head, e, entries); - line = nn; - } - *next = line+1; - } + lori *e; + line++; + char *nn = &line[1]; + while (line[0] != ']') { + e = parseline(line, &nn); + if (e != NULL) + SIMPLEQ_INSERT_TAIL(r->head, e, entries); + line = nn; + } + *next = line + 1; + } - return r; + return r; } diff --git a/day13/common.h b/day13/common.h @@ -1,21 +1,21 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> -#include <sys/queue.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> +#include <sys/queue.h> typedef struct entry { - uint64_t v; - SIMPLEQ_HEAD(listhead, entry) *head; + uint64_t v; + SIMPLEQ_HEAD(listhead, entry) * head; - SIMPLEQ_ENTRY(entry) entries; + SIMPLEQ_ENTRY(entry) entries; } lori; -void printlori(const lori* x); -lori* parseline(char* line, char** next); -int compare(const lori* a, const lori* b); +void printlori(const lori *x); +lori *parseline(char *line, char **next); +int compare(const lori *a, const lori *b); #endif diff --git a/day13/uppga.c b/day13/uppga.c @@ -1,25 +1,24 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - size_t i; - char* next = lines[0]; - lori* loris[nlines]; - size_t c = 0; - for(i = 0; i < nlines; i++) { - if(strlen(lines[i]) > 1) { - loris[c] = parseline(lines[i], &next); - c++; - } - } + size_t i; + char *next = lines[0]; + lori *loris[nlines]; + size_t c = 0; + for (i = 0; i < nlines; i++) { + if (strlen(lines[i]) > 1) { + loris[c] = parseline(lines[i], &next); + c++; + } + } - uint64_t sum = 0; - for(i = 0; i < c; i+=2) - if(compare(loris[i], loris[i+1]) < 0) - sum += i/2 + 1; + uint64_t sum = 0; + for (i = 0; i < c; i += 2) + if (compare(loris[i], loris[i + 1]) < 0) + sum += i / 2 + 1; - printf("%llu\n", sum); + printf("%llu\n", sum); } diff --git a/day13/uppgb.c b/day13/uppgb.c @@ -1,42 +1,42 @@ #include "common.h" -int cmp(const void* a, const void* b) -{ - lori* al = *((lori**) a); - lori* bl = *((lori**) b); - int c = compare(al, bl); - return c; +int cmp(const void *a, const void *b) { + lori *al = *((lori **)a); + lori *bl = *((lori **)b); + int c = compare(al, bl); + return c; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - lori* loris[nlines + 2]; - lori* sig[2]; + lori *loris[nlines + 2]; + lori *sig[2]; - char* next = lines[0]; - loris[0] = parseline("[[2]]", &next); - loris[1] = parseline("[[6]]", &next); - sig[0] = parseline("[[2]]", &next); - sig[1] = parseline("[[6]]", &next); + char *next = lines[0]; + loris[0] = parseline("[[2]]", &next); + loris[1] = parseline("[[6]]", &next); + sig[0] = parseline("[[2]]", &next); + sig[1] = parseline("[[6]]", &next); - size_t i; - size_t c = 2; - for(i = 0; i < nlines; i++) { - if(strlen(lines[i]) > 1) { - loris[c] = parseline(lines[i], &next); - c++; - } - } + size_t i; + size_t c = 2; + for (i = 0; i < nlines; i++) { + if (strlen(lines[i]) > 1) { + loris[c] = parseline(lines[i], &next); + c++; + } + } - uint64_t sigidx[2] = {UINT64_MAX}; + uint64_t sigidx[2] = {UINT64_MAX}; qsort(loris, c, sizeof(*loris), cmp); - for(i = 0; i < c; i++) { - if(compare(loris[i], sig[0]) == 0) sigidx[0] = i; - else if(compare(loris[i], sig[1]) == 0) sigidx[1] = i; - } - printf("%llu\n", (sigidx[0]+1) * (sigidx[1]+1)); + for (i = 0; i < c; i++) { + if (compare(loris[i], sig[0]) == 0) + sigidx[0] = i; + else if (compare(loris[i], sig[1]) == 0) + sigidx[1] = i; + } + printf("%llu\n", (sigidx[0] + 1) * (sigidx[1] + 1)); } diff --git a/day14/common.c b/day14/common.c @@ -1,73 +1,78 @@ #include "common.h" -void printgd(gd* g) -{ - size_t i,j; - for(j = 0; j < g->maxy; j++) { - for(i = 494; i < 504; i++) { +void printgd(gd *g) { + size_t i, j; + for (j = 0; j < g->maxy; j++) { + for (i = 494; i < 504; i++) { uint8_t v = g->grid[i * g->h + j]; - if(v == 0) printf("."); - else if(v == 1) printf("#"); - else printf("o"); + if (v == 0) + printf("."); + else if (v == 1) + printf("#"); + else + printf("o"); } printf("\n"); } } -int sandfall(gd* g) -{ +int sandfall(gd *g) { size_t x = 500, y = 0; - while(y < g->maxy) { + while (y < g->maxy) { assert(x > 0); - if(g->grid[x * g->h + (y+1)] == 0) { - y++; - } else if(g->grid[(x-1) * g->h + (y + 1)] == 0) { + if (g->grid[x * g->h + (y + 1)] == 0) { + y++; + } else if (g->grid[(x - 1) * g->h + (y + 1)] == 0) { x--; y++; - } else if(g->grid[(x+1) * g->h + (y + 1)] == 0) { + } else if (g->grid[(x + 1) * g->h + (y + 1)] == 0) { x++; y++; } else { g->grid[x * g->h + y] = 2; - if(x == 500 && y == 0) return 3; + if (x == 500 && y == 0) + return 3; return 0; } } return 1; } -gd* parse(char** lines, size_t nlines, int floor) -{ - stack_u64 xs[nlines+1], ys[nlines+1]; - char* p; - size_t i,j; +gd *parse(char **lines, size_t nlines, int floor) { + stack_u64 xs[nlines + 1], ys[nlines + 1]; + char *p; + size_t i, j; uint64_t minx = UINT64_MAX, maxx = 0, miny = UINT64_MAX, maxy = 0; - for(i = 0; i < nlines; i++) { + for (i = 0; i < nlines; i++) { stack_u64_init(&xs[i]); stack_u64_init(&ys[i]); uint64_t t; size_t c = 0; p = lines[i]; - while((p = sread_next_u64(&t, p)) != NULL) { - if(c % 2 == 0) { + while ((p = sread_next_u64(&t, p)) != NULL) { + if (c % 2 == 0) { stack_u64_push(&xs[i], t); - if(t < minx) minx = t; - if(t > maxx) maxx = t; + if (t < minx) + minx = t; + if (t > maxx) + maxx = t; } else { stack_u64_push(&ys[i], t); - if(t < miny) miny = t; - if(t > maxy) maxy = t; + if (t < miny) + miny = t; + if (t > maxy) + maxy = t; } c++; } } - if(floor) { + if (floor) { maxy += 2; - maxx += maxy+1; // at most diagonal + maxx += maxy + 1; // at most diagonal stack_u64_init(&xs[nlines]); stack_u64_init(&ys[nlines]); stack_u64_push(&xs[nlines], 0); @@ -80,37 +85,42 @@ gd* parse(char** lines, size_t nlines, int floor) assert(minx > 0); // otherwise we would have to shift assert(miny > 0); - uint8_t* grid = calloc((maxx+1) * (maxy+1), sizeof(*grid)); + uint8_t *grid = calloc((maxx + 1) * (maxy + 1), sizeof(*grid)); - for(i = 0; i < nlines; i++) { + for (i = 0; i < nlines; i++) { assert(xs[i].nmemb == ys[i].nmemb); - for(j = 0; j < xs[i].nmemb-1; j++) { - assert(xs[i].data[j] == xs[i].data[j+1] || ys[i].data[j] == ys[i].data[j+1]); - if(xs[i].data[j] == xs[i].data[j+1]) { + for (j = 0; j < xs[i].nmemb - 1; j++) { + assert(xs[i].data[j] == xs[i].data[j + 1] || + ys[i].data[j] == ys[i].data[j + 1]); + if (xs[i].data[j] == xs[i].data[j + 1]) { uint64_t x = xs[i].data[j]; - uint64_t yfrom = ys[i].data[j] < ys[i].data[j+1] ? ys[i].data[j] : ys[i].data[j+1]; - uint64_t yto = ys[i].data[j] < ys[i].data[j+1] ? ys[i].data[j+1] : ys[i].data[j]; - while(yfrom != yto) { - grid[x * (maxy+1) + yfrom] = 1; + uint64_t yfrom = ys[i].data[j] < ys[i].data[j + 1] ? ys[i].data[j] + : ys[i].data[j + 1]; + uint64_t yto = ys[i].data[j] < ys[i].data[j + 1] ? ys[i].data[j + 1] + : ys[i].data[j]; + while (yfrom != yto) { + grid[x * (maxy + 1) + yfrom] = 1; yfrom++; } - grid[x * (maxy+1) + yfrom] = 1; + grid[x * (maxy + 1) + yfrom] = 1; } else { uint64_t y = ys[i].data[j]; - uint64_t xfrom = xs[i].data[j] < xs[i].data[j+1] ? xs[i].data[j] : xs[i].data[j+1]; - uint64_t xto = xs[i].data[j] < xs[i].data[j+1] ? xs[i].data[j+1] : xs[i].data[j]; - while(xfrom != xto) { - grid[xfrom * (maxy+1) + y] = 1; + uint64_t xfrom = xs[i].data[j] < xs[i].data[j + 1] ? xs[i].data[j] + : xs[i].data[j + 1]; + uint64_t xto = xs[i].data[j] < xs[i].data[j + 1] ? xs[i].data[j + 1] + : xs[i].data[j]; + while (xfrom != xto) { + grid[xfrom * (maxy + 1) + y] = 1; xfrom++; } - grid[xfrom * (maxy+1) + y] = 1; + grid[xfrom * (maxy + 1) + y] = 1; } } } - gd* r = malloc(sizeof(*r)); - r->h = (size_t) maxy+1; - r->w = (size_t) maxx+1; + gd *r = malloc(sizeof(*r)); + r->h = (size_t)maxy + 1; + r->w = (size_t)maxx + 1; r->minx = minx; r->miny = miny; r->maxx = maxx; diff --git a/day14/common.h b/day14/common.h @@ -1,11 +1,11 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> #include <stack_u64.h> +#include <stdint.h> +#include <stdio.h> typedef struct { size_t h; @@ -16,11 +16,11 @@ typedef struct { uint64_t maxx; uint64_t maxy; - uint8_t* grid; + uint8_t *grid; } gd; -gd* parse(char** lines, size_t nlines, int floor); -int sandfall(gd* g); -void printgd(gd* g); +gd *parse(char **lines, size_t nlines, int floor); +int sandfall(gd *g); +void printgd(gd *g); #endif diff --git a/day14/uppga.c b/day14/uppga.c @@ -1,15 +1,15 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - - gd* g = parse(lines, nlines, 0); + + gd *g = parse(lines, nlines, 0); int r; size_t c = 0; - while((r = sandfall(g)) == 0) c++; + while ((r = sandfall(g)) == 0) + c++; printf("%zu\n", c); } diff --git a/day14/uppgb.c b/day14/uppgb.c @@ -1,16 +1,16 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - - gd* g = parse(lines, nlines, 1); + + gd *g = parse(lines, nlines, 1); int r; size_t c = 0; - while((r = sandfall(g)) == 0) c++; + while ((r = sandfall(g)) == 0) + c++; assert(r == 3); // reached proper end - printf("%zu\n", c+1); + printf("%zu\n", c + 1); } diff --git a/day15/common.c b/day15/common.c @@ -1,37 +1,28 @@ #include "common.h" -void parse(int* coords, char** lines, size_t nlines) -{ +void parse(int *coords, char **lines, size_t nlines) { size_t i; - for(i = 0; i < nlines; i++) { + for (i = 0; i < nlines; i++) { sscanf(lines[i], "Sensor at x=%d, y=%d: closest beacon is at x=%d, y=%d", - &coords[4*i], &coords[4*i+1], &coords[4*i+2], &coords[4*i + 3]); + &coords[4 * i], &coords[4 * i + 1], &coords[4 * i + 2], + &coords[4 * i + 3]); } } -static inline int min(const int x, const int y) -{ - return x < y ? x : y; -} +static inline int min(const int x, const int y) { return x < y ? x : y; } -static inline int max(const int x, const int y) -{ - return x < y ? y : x; -} +static inline int max(const int x, const int y) { return x < y ? y : x; } -static inline int overlap(const iv *x, const iv *y) -{ - return (x->a <= y->b && y->b <= x->b) || - (y->a <= x->b && x->b <= y->b); +static inline int overlap(const iv *x, const iv *y) { + return (x->a <= y->b && y->b <= x->b) || (y->a <= x->b && x->b <= y->b); } -void insert_interval(lh* head, iv* t) -{ +void insert_interval(lh *head, iv *t) { assert(t->a <= t->b); - iv* k; + iv *k; LIST_FOREACH(k, head, entries) { - if(overlap(k, t)) { + if (overlap(k, t)) { t->a = min(k->a, t->a); t->b = max(k->b, t->b); LIST_REMOVE(k, entries); @@ -42,20 +33,22 @@ void insert_interval(lh* head, iv* t) LIST_INSERT_HEAD(head, t, entries); } -lh* compintervals(int* coords, size_t n, int yl) -{ - int x0,y0,x1,y1,d; +lh *compintervals(int *coords, size_t n, int yl) { + int x0, y0, x1, y1, d; - lh* ret = malloc(sizeof(*ret)); + lh *ret = malloc(sizeof(*ret)); LIST_INIT(ret); - iv* t; + iv *t; size_t i; - for(i = 0; i < n; i++) { - x0 = coords[4*i]; y0 = coords[4*i+1]; x1 = coords[4*i+2]; y1 = coords[4*i+3]; - d = abs(x0-x1) + abs(y0-y1); - if(abs(y0 - yl) <= d) { + for (i = 0; i < n; i++) { + x0 = coords[4 * i]; + y0 = coords[4 * i + 1]; + x1 = coords[4 * i + 2]; + y1 = coords[4 * i + 3]; + d = abs(x0 - x1) + abs(y0 - y1); + if (abs(y0 - yl) <= d) { t = malloc(sizeof(*t)); t->a = x0 - d + abs(y0 - yl); t->b = x0 + d - abs(y0 - yl); @@ -66,13 +59,12 @@ lh* compintervals(int* coords, size_t n, int yl) return ret; } -void clear_lh(lh* head) -{ - iv* t; +void clear_lh(lh *head) { + iv *t; while (!LIST_EMPTY(head)) { - t = LIST_FIRST(head); - LIST_REMOVE(t, entries); - free(t); + t = LIST_FIRST(head); + LIST_REMOVE(t, entries); + free(t); } free(head); } \ No newline at end of file diff --git a/day15/common.h b/day15/common.h @@ -1,23 +1,22 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> -#include <sys/queue.h> #include <reading.h> - +#include <stdint.h> +#include <stdio.h> +#include <sys/queue.h> typedef struct entry { - int a; - int b; + int a; + int b; - LIST_ENTRY(entry) entries; + LIST_ENTRY(entry) entries; } iv; typedef LIST_HEAD(listhead, entry) lh; -void parse(int* coords, char** lines, size_t nlines); -lh* compintervals(int* coords, size_t n, int yl); -void clear_lh(lh* head); +void parse(int *coords, char **lines, size_t nlines); +lh *compintervals(int *coords, size_t n, int yl); +void clear_lh(lh *head); #endif diff --git a/day15/uppga.c b/day15/uppga.c @@ -1,25 +1,21 @@ #include "common.h" -int sumup(lh* head) -{ +int sumup(lh *head) { int sum = 0; - iv* t; + iv *t; - LIST_FOREACH(t, head, entries) { - sum += (t->b - t->a); - } + LIST_FOREACH(t, head, entries) { sum += (t->b - t->a); } return sum; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - int coords[nlines*4]; + int coords[nlines * 4]; parse(coords, lines, nlines); - lh* h = compintervals(coords, nlines, 2000000); + lh *h = compintervals(coords, nlines, 2000000); int sum = sumup(h); printf("%d\n", sum); diff --git a/day15/uppgb.c b/day15/uppgb.c @@ -8,70 +8,69 @@ typedef struct { #define LMT 4000000 -static inline int d(boll a, boll b) -{ - return abs(a.x - b.x) + abs(a.y - b.y); -} +static inline int d(boll a, boll b) { return abs(a.x - b.x) + abs(a.y - b.y); } -static inline int inaball(int x, int y, boll* bs, size_t n) { - size_t i; - for(i = 0; i < n; i++) - if(abs(bs[i].x - x) + abs(bs[i].y - y) <= bs[i].d) return 1; - return 0; +static inline int inaball(int x, int y, boll *bs, size_t n) { + size_t i; + for (i = 0; i < n; i++) + if (abs(bs[i].x - x) + abs(bs[i].y - y) <= bs[i].d) + return 1; + return 0; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); boll bs[nlines]; int x1, y1; - size_t i,j; - for(i = 0; i < nlines; i++) { + size_t i, j; + for (i = 0; i < nlines; i++) { sscanf(lines[i], "Sensor at x=%d, y=%d: closest beacon is at x=%d, y=%d", - &bs[i].x, &bs[i].y, &x1, &y1); + &bs[i].x, &bs[i].y, &x1, &y1); bs[i].d = abs(bs[i].x - x1) + abs(bs[i].y - y1); - } + } - size_t ndownlines = 0; - int downline[4*nlines]; - size_t nuplines = 0; - int upline[4*nlines]; + size_t ndownlines = 0; + int downline[4 * nlines]; + size_t nuplines = 0; + int upline[4 * nlines]; - for(i = 0; i < nlines; i++) { - for(j = i+1; j < nlines; j++) { - if(d(bs[i], bs[j]) == bs[i].d + bs[j].d + 2) { - if(abs(bs[i].x - bs[j].x + bs[i].y - bs[j].y) == bs[i].d + bs[j].d + 2) { - if(bs[i].x - bs[j].x + bs[i].y - bs[j].y == bs[i].d + bs[j].d + 2) { - downline[ndownlines] = bs[i].x + bs[i].y - bs[i].d - 1; - } else { - downline[ndownlines] = bs[i].x + bs[i].y - bs[i].d + 1; - } - ndownlines++; - } else { - if(bs[j].x - bs[i].x + bs[i].y - bs[j].y == bs[i].d + bs[j].d + 2) { - upline[nuplines] = - bs[i].y + bs[i].x + bs[i].d + 1; - } else { - upline[nuplines] = - bs[i].y + bs[i].x - bs[i].d - 1; - } - nuplines++; - } + for (i = 0; i < nlines; i++) { + for (j = i + 1; j < nlines; j++) { + if (d(bs[i], bs[j]) == bs[i].d + bs[j].d + 2) { + if (abs(bs[i].x - bs[j].x + bs[i].y - bs[j].y) == + bs[i].d + bs[j].d + 2) { + if (bs[i].x - bs[j].x + bs[i].y - bs[j].y == bs[i].d + bs[j].d + 2) { + downline[ndownlines] = bs[i].x + bs[i].y - bs[i].d - 1; + } else { + downline[ndownlines] = bs[i].x + bs[i].y - bs[i].d + 1; + } + ndownlines++; + } else { + if (bs[j].x - bs[i].x + bs[i].y - bs[j].y == bs[i].d + bs[j].d + 2) { + upline[nuplines] = -bs[i].y + bs[i].x + bs[i].d + 1; + } else { + upline[nuplines] = -bs[i].y + bs[i].x - bs[i].d - 1; + } + nuplines++; + } } } } - for(i = 0; i < ndownlines; i++) { - for(j = 0; j < nuplines; j++) { - int x, y; - x = (downline[i] + upline[j])/2; - y = (downline[i] - upline[j])/2; - if(x >= 0 && y >= 0 && x <= LMT && y <= LMT && !inaball(x,y,bs,nlines)) { - printf("%llu\n", ((uint64_t) x)*((uint64_t) LMT) + ((uint64_t) y)); - return 0; - } - } - } + for (i = 0; i < ndownlines; i++) { + for (j = 0; j < nuplines; j++) { + int x, y; + x = (downline[i] + upline[j]) / 2; + y = (downline[i] - upline[j]) / 2; + if (x >= 0 && y >= 0 && x <= LMT && y <= LMT && + !inaball(x, y, bs, nlines)) { + printf("%llu\n", ((uint64_t)x) * ((uint64_t)LMT) + ((uint64_t)y)); + return 0; + } + } + } return 0; } diff --git a/day15/uppgb_old.c b/day15/uppgb_old.c @@ -2,37 +2,36 @@ #define LMT 4000000 -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - int coords[nlines*4]; + int coords[nlines * 4]; parse(coords, lines, nlines); uint64_t xc = UINT64_MAX; uint64_t yc = UINT64_MAX; int i; - for(i = 0; i < LMT; i++) { - lh* h = compintervals(coords, nlines, i); - iv* t; + for (i = 0; i < LMT; i++) { + lh *h = compintervals(coords, nlines, i); + iv *t; int found = 0; LIST_FOREACH(t, h, entries) { - if(t->a <= 0 && t->b >= LMT) { + if (t->a <= 0 && t->b >= LMT) { found = 1; break; } } - if(found) { + if (found) { continue; } else { - //this must be the line + // this must be the line LIST_FOREACH(t, h, entries) { - if(t->a <= 0 && t->b >= 0) { - xc = (uint64_t) (t->b + 1); - yc = (uint64_t) i; + if (t->a <= 0 && t->b >= 0) { + xc = (uint64_t)(t->b + 1); + yc = (uint64_t)i; break; } } @@ -42,5 +41,5 @@ int main(int argc, char** argv) clear_lh(h); } - printf("%lu\n", LMT*xc + yc); + printf("%lu\n", LMT * xc + yc); } \ No newline at end of file diff --git a/day16/common.c b/day16/common.c @@ -1,6 +1,3 @@ #include "common.h" -size_t fn(char** lines, size_t nlines) -{ - return 0; -} +size_t fn(char **lines, size_t nlines) { return 0; } diff --git a/day16/common.h b/day16/common.h @@ -1,11 +1,11 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> -size_t fn(char** lines, size_t nlines); +size_t fn(char **lines, size_t nlines); #endif diff --git a/day16/uppga.c b/day16/uppga.c @@ -4,228 +4,226 @@ #define NOTCOMPUTED UINT64_MAX #define NONBR SIZE_MAX -#define ASIZE ((uint64_t)('Z'-'A'+1)) -#define MAP_SIZE (ASIZE*ASIZE) +#define ASIZE ((uint64_t)('Z' - 'A' + 1)) +#define MAP_SIZE (ASIZE * ASIZE) typedef struct { - size_t n; - size_t m; + size_t n; + size_t m; - size_t nv; - uint64_t* v; // store computed values + size_t nv; + uint64_t *v; // store computed values - uint64_t* flows; - size_t* nzflows; // maps position to index of nonzero flow (in e.g. state) - size_t** nbrs; + uint64_t *flows; + size_t *nzflows; // maps position to index of nonzero flow (in e.g. state) + size_t **nbrs; } valves; -static inline size_t statelen(valves v) { - return v.m + 2; -} +static inline size_t statelen(valves v) { return v.m + 2; } static inline size_t statemax(valves v) { - return ((v.n)*(SEC+1) << v.m)-1; + return ((v.n) * (SEC + 1) << v.m) - 1; } static inline int notime(uint64_t fs, valves v) { - return (fs >> v.m) % (SEC+1) == 0; + return (fs >> v.m) % (SEC + 1) == 0; } -static inline char* goto_nbrs(char* s) -{ - size_t i=0, c=0; - while(c != 9) { - if(s[i] == ' ') c++; - i++; - } - return &(s[i]); +static inline char *goto_nbrs(char *s) { + size_t i = 0, c = 0; + while (c != 9) { + if (s[i] == ' ') + c++; + i++; + } + return &(s[i]); } -static inline size_t map_index(char* x) -{ - return ASIZE * ((uint64_t)(x[0] - 'A')) + ((uint64_t)(x[1] - 'A')); +static inline size_t map_index(char *x) { + return ASIZE * ((uint64_t)(x[0] - 'A')) + ((uint64_t)(x[1] - 'A')); } -void init_valves(valves* v, char** lines, const size_t n) -{ - v->n = n; - v->m = 0; - - v->flows = malloc((v->n) * sizeof(*(v->flows))); - v->nzflows = malloc((v->n) * sizeof(*(v->nzflows))); - v->nbrs = malloc((v->n) * sizeof(*(v->nbrs))); - - size_t i,j; - size_t map[MAP_SIZE]; - for(i = 0; i < MAP_SIZE; i++) map[i] = SIZE_MAX; - for(i = 0; i < n; i++) { - char* line = lines[i]; - map[map_index(&line[6])] = i; - } - - for(i = 0; i < n; i++) { - char* line = lines[i]; - - char* flowstr = &line[23]; - uint64_t flow; - sread_next_u64(&flow, flowstr); - v->flows[i] = flow; - if(flow != 0) { - v->nzflows[i] = v->m+2; - v->m++; - } else { - v->nzflows[i] = SIZE_MAX; - } - - v->nbrs[i] = malloc((v->n) * sizeof(*(v->nbrs[i]))); - char* nbrs = goto_nbrs(line); - char* cn = nbrs; - j = 0; - while(cn[0] != '\n' && cn[0] != '\0') { - assert(cn[0] <= 'Z' && cn[0] >= 'A'); - assert(cn[1] <= 'Z' && cn[1] >= 'A'); - v->nbrs[i][j] = map[map_index(cn)]; - if(strlen(cn) < 4) { - j++; - break; - } - j++; - cn = &cn[4]; - } - while(j < v->n) { - v->nbrs[i][j] = NONBR; - j++; - } - } - - v->nv = statemax(*v) + 1; - v->v = malloc((v->nv) * sizeof(*(v->v))); - for(i = 0; i < v->nv; i++) v->v[i] = NOTCOMPUTED; +void init_valves(valves *v, char **lines, const size_t n) { + v->n = n; + v->m = 0; + + v->flows = malloc((v->n) * sizeof(*(v->flows))); + v->nzflows = malloc((v->n) * sizeof(*(v->nzflows))); + v->nbrs = malloc((v->n) * sizeof(*(v->nbrs))); + + size_t i, j; + size_t map[MAP_SIZE]; + for (i = 0; i < MAP_SIZE; i++) + map[i] = SIZE_MAX; + for (i = 0; i < n; i++) { + char *line = lines[i]; + map[map_index(&line[6])] = i; + } + + for (i = 0; i < n; i++) { + char *line = lines[i]; + + char *flowstr = &line[23]; + uint64_t flow; + sread_next_u64(&flow, flowstr); + v->flows[i] = flow; + if (flow != 0) { + v->nzflows[i] = v->m + 2; + v->m++; + } else { + v->nzflows[i] = SIZE_MAX; + } + + v->nbrs[i] = malloc((v->n) * sizeof(*(v->nbrs[i]))); + char *nbrs = goto_nbrs(line); + char *cn = nbrs; + j = 0; + while (cn[0] != '\n' && cn[0] != '\0') { + assert(cn[0] <= 'Z' && cn[0] >= 'A'); + assert(cn[1] <= 'Z' && cn[1] >= 'A'); + v->nbrs[i][j] = map[map_index(cn)]; + if (strlen(cn) < 4) { + j++; + break; + } + j++; + cn = &cn[4]; + } + while (j < v->n) { + v->nbrs[i][j] = NONBR; + j++; + } + } + + v->nv = statemax(*v) + 1; + v->v = malloc((v->nv) * sizeof(*(v->v))); + for (i = 0; i < v->nv; i++) + v->v[i] = NOTCOMPUTED; } -size_t f(const uint8_t* state, const valves* v) { - size_t r = ((unsigned long) state[0])*(SEC+1) + (unsigned long) state[1]; - size_t i; - for(i = 0; i < v->m; i++) { - r <<= 1; - if(state[i+2]) r++; - } - return r; +size_t f(const uint8_t *state, const valves *v) { + size_t r = ((unsigned long)state[0]) * (SEC + 1) + (unsigned long)state[1]; + size_t i; + for (i = 0; i < v->m; i++) { + r <<= 1; + if (state[i + 2]) + r++; + } + return r; } -void f_inv(uint8_t* state, const size_t y, const valves* v) { - size_t i; - size_t slen = statelen(*v); +void f_inv(uint8_t *state, const size_t y, const valves *v) { + size_t i; + size_t slen = statelen(*v); - size_t x = y; + size_t x = y; - for(i = 1; i <= v->m; i++) { - if((x & 0x01) != 0) { - state[slen-i] = 1; - } else { - state[slen-i] = 0; - } - x >>= 1; - } + for (i = 1; i <= v->m; i++) { + if ((x & 0x01) != 0) { + state[slen - i] = 1; + } else { + state[slen - i] = 0; + } + x >>= 1; + } - assert(x < (SEC+1)*(v->n)); + assert(x < (SEC + 1) * (v->n)); - state[1] = (uint8_t) (x % (SEC+1)); - state[0] = (uint8_t) (x / (SEC+1)); + state[1] = (uint8_t)(x % (SEC + 1)); + state[0] = (uint8_t)(x / (SEC + 1)); } -uint8_t* init_starting_state(valves v) -{ - uint8_t* r = malloc(statelen(v)); +uint8_t *init_starting_state(valves v) { + uint8_t *r = malloc(statelen(v)); + + r[0] = 0; // posituon AA == 0 + r[1] = SEC; // 30 secs remaining - r[0] = 0; // posituon AA == 0 - r[1] = SEC; // 30 secs remaining - - size_t i; - for(i = 2; i < v.m+2; i++) r[i] = 0; // no valves on + size_t i; + for (i = 2; i < v.m + 2; i++) + r[i] = 0; // no valves on - return r; + return r; } -static inline uint64_t stateflow(const uint8_t* state, const valves* v) -{ - uint64_t r = 0; - size_t i; - for(i = 0; i < v->n; i++) { - if(v->flows[i] != 0) { - r += ((uint64_t) state[v->nzflows[i]]) * v->flows[i]; - } - } - return r; +static inline uint64_t stateflow(const uint8_t *state, const valves *v) { + uint64_t r = 0; + size_t i; + for (i = 0; i < v->n; i++) { + if (v->flows[i] != 0) { + r += ((uint64_t)state[v->nzflows[i]]) * v->flows[i]; + } + } + return r; } -void print_state(const uint8_t* state, const valves* v) -{ - printf("%u : %u : ", state[0], state[1]); - size_t i; - for(i = 0; i < v->m; i++) { - printf("%u ", state[i+2]); - } - printf("\n"); +void print_state(const uint8_t *state, const valves *v) { + printf("%u : %u : ", state[0], state[1]); + size_t i; + for (i = 0; i < v->m; i++) { + printf("%u ", state[i + 2]); + } + printf("\n"); } -uint64_t recurse(const uint64_t fs, valves* v, int level) -{ - if(v->v[fs] != NOTCOMPUTED) return v->v[fs]; - uint64_t r = 0; - - if(notime(fs, *v)) { - v->v[fs] = r; - return r; - } - - uint8_t state[statelen(*v)]; - f_inv(state, fs, v); - - size_t pos = (size_t) state[0]; - - // nonzero flow - if(v->flows[pos] != 0) { - if(state[v->nzflows[pos]] == 0) { - uint8_t new_state[statelen(*v)]; - memcpy(new_state, state, statelen(*v)); - new_state[v->nzflows[pos]] = 1; - new_state[1]--; - uint64_t t = stateflow(state, v) + recurse(f(new_state, v), v, level+1); - if(t > r) r = t; - } - } - - // neighbours - size_t i = 0; - while(v->nbrs[pos][i] != NONBR) { - uint8_t new_state[statelen(*v)]; - memcpy(new_state, state, statelen(*v)); - new_state[1]--; - new_state[0] = (uint8_t) v->nbrs[pos][i]; - uint64_t t = stateflow(state, v) + recurse(f(new_state, v), v, level+1); - if(t > r) r = t; - i++; - } - - v->v[fs] = r; - return r; +uint64_t recurse(const uint64_t fs, valves *v, int level) { + if (v->v[fs] != NOTCOMPUTED) + return v->v[fs]; + uint64_t r = 0; + + if (notime(fs, *v)) { + v->v[fs] = r; + return r; + } + + uint8_t state[statelen(*v)]; + f_inv(state, fs, v); + + size_t pos = (size_t)state[0]; + + // nonzero flow + if (v->flows[pos] != 0) { + if (state[v->nzflows[pos]] == 0) { + uint8_t new_state[statelen(*v)]; + memcpy(new_state, state, statelen(*v)); + new_state[v->nzflows[pos]] = 1; + new_state[1]--; + uint64_t t = stateflow(state, v) + recurse(f(new_state, v), v, level + 1); + if (t > r) + r = t; + } + } + + // neighbours + size_t i = 0; + while (v->nbrs[pos][i] != NONBR) { + uint8_t new_state[statelen(*v)]; + memcpy(new_state, state, statelen(*v)); + new_state[1]--; + new_state[0] = (uint8_t)v->nbrs[pos][i]; + uint64_t t = stateflow(state, v) + recurse(f(new_state, v), v, level + 1); + if (t > r) + r = t; + i++; + } + + v->v[fs] = r; + return r; } -int main(int argc, char** argv) -{ - char** lines; - valves v; +int main(int argc, char **argv) { + char **lines; + valves v; size_t nlines = readlines(&lines, "sinput"); - init_valves(&v, lines, nlines); + init_valves(&v, lines, nlines); - uint8_t* starting_state = init_starting_state(v); + uint8_t *starting_state = init_starting_state(v); - size_t f_ss = f(starting_state, &v); - uint8_t* tmp_state = malloc(statelen(v)); - f_inv(tmp_state, f_ss, &v); + size_t f_ss = f(starting_state, &v); + uint8_t *tmp_state = malloc(statelen(v)); + f_inv(tmp_state, f_ss, &v); - uint64_t r = recurse(f_ss, &v, 0); - printf("%llu\n", r); + uint64_t r = recurse(f_ss, &v, 0); + printf("%llu\n", r); } diff --git a/day16/uppgb.c b/day16/uppgb.c @@ -4,257 +4,255 @@ #define NOTCOMPUTED UINT64_MAX #define NONBR SIZE_MAX -#define ASIZE ((uint64_t)('Z'-'A'+1)) -#define MAP_SIZE (ASIZE*ASIZE) +#define ASIZE ((uint64_t)('Z' - 'A' + 1)) +#define MAP_SIZE (ASIZE * ASIZE) typedef struct { - size_t n; - size_t m; + size_t n; + size_t m; - size_t nv; - uint64_t* v; // store computed values + size_t nv; + uint64_t *v; // store computed values - uint64_t* flows; - size_t* nzflows; // maps position to index of nonzero flow (in e.g. state) - size_t** nbrs; + uint64_t *flows; + size_t *nzflows; // maps position to index of nonzero flow (in e.g. state) + size_t **nbrs; } valves; -static inline size_t statelen(valves v) { - return v.m + 2; -} +static inline size_t statelen(valves v) { return v.m + 2; } -static inline size_t statemax(valves v) { - return ((v.n)*(v.n) << v.m)-1; -} +static inline size_t statemax(valves v) { return ((v.n) * (v.n) << v.m) - 1; } -static inline char* goto_nbrs(char* s) -{ - size_t i=0, c=0; - while(c != 9) { - if(s[i] == ' ') c++; - i++; - } - return &(s[i]); +static inline char *goto_nbrs(char *s) { + size_t i = 0, c = 0; + while (c != 9) { + if (s[i] == ' ') + c++; + i++; + } + return &(s[i]); } -static inline size_t map_index(char* x) -{ - return ASIZE * ((uint64_t)(x[0] - 'A')) + ((uint64_t)(x[1] - 'A')); +static inline size_t map_index(char *x) { + return ASIZE * ((uint64_t)(x[0] - 'A')) + ((uint64_t)(x[1] - 'A')); } -void init_valves(valves* v, char** lines, const size_t n) -{ - v->n = n; - v->m = 0; - - v->flows = malloc((v->n) * sizeof(*(v->flows))); - v->nzflows = malloc((v->n) * sizeof(*(v->nzflows))); - v->nbrs = malloc((v->n) * sizeof(*(v->nbrs))); - - size_t i,j; - size_t map[MAP_SIZE]; - for(i = 0; i < MAP_SIZE; i++) map[i] = SIZE_MAX; - for(i = 0; i < n; i++) { - char* line = lines[i]; - map[map_index(&line[6])] = i; - } - - for(i = 0; i < n; i++) { - char* line = lines[i]; - - char* flowstr = &line[23]; - uint64_t flow; - sread_next_u64(&flow, flowstr); - v->flows[i] = flow; - if(flow != 0) { - v->nzflows[i] = v->m; - v->m++; - } else { - v->nzflows[i] = SIZE_MAX; - } - - v->nbrs[i] = malloc((v->n) * sizeof(*(v->nbrs[i]))); - char* nbrs = goto_nbrs(line); - char* cn = nbrs; - j = 0; - while(cn[0] != '\n' && cn[0] != '\0') { - assert(cn[0] <= 'Z' && cn[0] >= 'A'); - assert(cn[1] <= 'Z' && cn[1] >= 'A'); - v->nbrs[i][j] = map[map_index(cn)]; - if(strlen(cn) < 4) { - j++; - break; - } - j++; - cn = &cn[4]; - } - while(j < v->n) { - v->nbrs[i][j] = NONBR; - j++; - } - } - - v->nv = statemax(*v) + 1; - v->v = malloc((v->nv) * sizeof(*(v->v))); +void init_valves(valves *v, char **lines, const size_t n) { + v->n = n; + v->m = 0; + + v->flows = malloc((v->n) * sizeof(*(v->flows))); + v->nzflows = malloc((v->n) * sizeof(*(v->nzflows))); + v->nbrs = malloc((v->n) * sizeof(*(v->nbrs))); + + size_t i, j; + size_t map[MAP_SIZE]; + for (i = 0; i < MAP_SIZE; i++) + map[i] = SIZE_MAX; + for (i = 0; i < n; i++) { + char *line = lines[i]; + map[map_index(&line[6])] = i; + } + + for (i = 0; i < n; i++) { + char *line = lines[i]; + + char *flowstr = &line[23]; + uint64_t flow; + sread_next_u64(&flow, flowstr); + v->flows[i] = flow; + if (flow != 0) { + v->nzflows[i] = v->m; + v->m++; + } else { + v->nzflows[i] = SIZE_MAX; + } + + v->nbrs[i] = malloc((v->n) * sizeof(*(v->nbrs[i]))); + char *nbrs = goto_nbrs(line); + char *cn = nbrs; + j = 0; + while (cn[0] != '\n' && cn[0] != '\0') { + assert(cn[0] <= 'Z' && cn[0] >= 'A'); + assert(cn[1] <= 'Z' && cn[1] >= 'A'); + v->nbrs[i][j] = map[map_index(cn)]; + if (strlen(cn) < 4) { + j++; + break; + } + j++; + cn = &cn[4]; + } + while (j < v->n) { + v->nbrs[i][j] = NONBR; + j++; + } + } + + v->nv = statemax(*v) + 1; + v->v = malloc((v->nv) * sizeof(*(v->v))); } -size_t f(const uint8_t* state, const valves* v) { - size_t r = ((unsigned long) state[0])*(v->n) + (unsigned long) state[1]; - size_t i; - for(i = 0; i < v->m; i++) { - r <<= 1; - if(state[i+2]) r++; - } - return r; +size_t f(const uint8_t *state, const valves *v) { + size_t r = ((unsigned long)state[0]) * (v->n) + (unsigned long)state[1]; + size_t i; + for (i = 0; i < v->m; i++) { + r <<= 1; + if (state[i + 2]) + r++; + } + return r; } -void f_inv(uint8_t* state, const size_t y, const valves* v) { - size_t i; - size_t slen = statelen(*v); +void f_inv(uint8_t *state, const size_t y, const valves *v) { + size_t i; + size_t slen = statelen(*v); - size_t x = y; + size_t x = y; - for(i = 1; i <= v->m; i++) { - if((x & 0x01) != 0) { - state[slen-i] = 1; - } else { - state[slen-i] = 0; - } - x >>= 1; - } + for (i = 1; i <= v->m; i++) { + if ((x & 0x01) != 0) { + state[slen - i] = 1; + } else { + state[slen - i] = 0; + } + x >>= 1; + } - assert(x < (v->n)*(v->n)); + assert(x < (v->n) * (v->n)); - state[1] = (uint8_t) (x % (v->n)); - state[0] = (uint8_t) (x / (v->n)); + state[1] = (uint8_t)(x % (v->n)); + state[0] = (uint8_t)(x / (v->n)); } -uint8_t* init_starting_state(valves v) -{ - uint8_t* r = malloc(statelen(v)); +uint8_t *init_starting_state(valves v) { + uint8_t *r = malloc(statelen(v)); + + r[0] = 0; // posituon AA == 0 + r[1] = 0; // elephant position same - r[0] = 0; // posituon AA == 0 - r[1] = 0; // elephant position same - - size_t i; - for(i = 2; i < v.m+2; i++) r[i] = 0; // no valves on + size_t i; + for (i = 2; i < v.m + 2; i++) + r[i] = 0; // no valves on - return r; + return r; } -static inline uint64_t stateflow(uint64_t b, const valves* v) -{ - uint64_t r = 0; - size_t i; - for(i = 0; i < v->n; i++) { - if(v->flows[i] != 0 && (b & (0x1 << v->nzflows[i])) != 0) { - r += v->flows[i]; - } - } - return r; +static inline uint64_t stateflow(uint64_t b, const valves *v) { + uint64_t r = 0; + size_t i; + for (i = 0; i < v->n; i++) { + if (v->flows[i] != 0 && (b & (0x1 << v->nzflows[i])) != 0) { + r += v->flows[i]; + } + } + return r; } -void print_state(const uint8_t* state, const valves* v) -{ - printf("%u : %u : ", state[0], state[1]); - size_t i; - for(i = 0; i < v->m; i++) { - printf("%u ", state[i+2]); - } - printf("\n"); +void print_state(const uint8_t *state, const valves *v) { + printf("%u : %u : ", state[0], state[1]); + size_t i; + for (i = 0; i < v->m; i++) { + printf("%u ", state[i + 2]); + } + printf("\n"); } -static inline uint64_t vss(size_t s, valves* v) { - return 0x01 << (v->m - s - 1); +static inline uint64_t vss(size_t s, valves *v) { + return 0x01 << (v->m - s - 1); } -static inline uint64_t fss(uint64_t py, uint64_t pe, uint64_t b, valves* v) -{ - uint64_t r = py * (v->n) + pe; - size_t i; - for(i = 0; i < v->m; i++) { - r <<= 1; - if((b & vss(i, v)) != 0) r++; - } - return r; +static inline uint64_t fss(uint64_t py, uint64_t pe, uint64_t b, valves *v) { + uint64_t r = py * (v->n) + pe; + size_t i; + for (i = 0; i < v->m; i++) { + r <<= 1; + if ((b & vss(i, v)) != 0) + r++; + } + return r; } -uint64_t* compute_level(valves* v, int level) -{ - size_t i,j; - uint64_t b; - uint64_t* new = malloc((v->nv) * sizeof(*new)); - - if(level <= 0) { - for(i = 0; i < v->nv; i++) new[i] = 0; - } else { - size_t blim = (1 << v->m); - for(i = 0; i < v->n; i++) { // you pos - for(j = 0; j < v->n; j++) { // E pos - for(b = 0; b < blim; b++) { - uint64_t best = 0; - if(v->flows[i] != 0 && v->flows[j] != 0) { - best = v->v[fss(i, j, (b | (0x01 << v->nzflows[i]) | (0x01 << v->nzflows[j])), v)]; - } - - uint64_t bprim; - size_t k = 0, l; - uint64_t t; - if(v->flows[i] != 0) { - k = 0; - bprim = b | (0x01 << v->nzflows[i]); - while(v->nbrs[j][k] != NONBR) { - t = v->v[fss(i, v->nbrs[j][k], bprim, v)]; - if(t > best) best = t; - k++; - } - } - - if(v->flows[j] != 0) { - bprim = b | (0x01 << v->nzflows[j]); - k = 0; - while(v->nbrs[i][k] != NONBR) { - t = v->v[fss(v->nbrs[i][k], j, bprim, v)]; - if(t > best) best = t; - k++; - } - } - - k = 0; - while(v->nbrs[i][k] != NONBR) { - l = 0; - while(v->nbrs[j][l] != NONBR) { - t = v->v[fss(v->nbrs[i][k], v->nbrs[j][l], b, v)]; - if(t > best) best = t; - l++; - } - k++; - } - - new[fss(i,j,b,v)] = stateflow(b,v) + best; - } - } - } - } - - return new; +uint64_t *compute_level(valves *v, int level) { + size_t i, j; + uint64_t b; + uint64_t *new = malloc((v->nv) * sizeof(*new)); + + if (level <= 0) { + for (i = 0; i < v->nv; i++) + new[i] = 0; + } else { + size_t blim = (1 << v->m); + for (i = 0; i < v->n; i++) { // you pos + for (j = 0; j < v->n; j++) { // E pos + for (b = 0; b < blim; b++) { + uint64_t best = 0; + if (v->flows[i] != 0 && v->flows[j] != 0) { + best = v->v[fss( + i, j, (b | (0x01 << v->nzflows[i]) | (0x01 << v->nzflows[j])), + v)]; + } + + uint64_t bprim; + size_t k = 0, l; + uint64_t t; + if (v->flows[i] != 0) { + k = 0; + bprim = b | (0x01 << v->nzflows[i]); + while (v->nbrs[j][k] != NONBR) { + t = v->v[fss(i, v->nbrs[j][k], bprim, v)]; + if (t > best) + best = t; + k++; + } + } + + if (v->flows[j] != 0) { + bprim = b | (0x01 << v->nzflows[j]); + k = 0; + while (v->nbrs[i][k] != NONBR) { + t = v->v[fss(v->nbrs[i][k], j, bprim, v)]; + if (t > best) + best = t; + k++; + } + } + + k = 0; + while (v->nbrs[i][k] != NONBR) { + l = 0; + while (v->nbrs[j][l] != NONBR) { + t = v->v[fss(v->nbrs[i][k], v->nbrs[j][l], b, v)]; + if (t > best) + best = t; + l++; + } + k++; + } + + new[fss(i, j, b, v)] = stateflow(b, v) + best; + } + } + } + } + + return new; } -int main(int argc, char** argv) -{ - char** lines; - valves v; +int main(int argc, char **argv) { + char **lines; + valves v; size_t nlines = readlines(&lines, "sinput"); - init_valves(&v, lines, nlines); - uint64_t* r; - size_t i; - for(i = 0; i < 27; i++) { - printf("--- LEVEL %zu ---\n", i); - r = compute_level(&v, i); - free(v.v); - v.v = r; - } - - printf("%llu\n", v.v[fss(0,0,0,&v)]); + init_valves(&v, lines, nlines); + uint64_t *r; + size_t i; + for (i = 0; i < 27; i++) { + printf("--- LEVEL %zu ---\n", i); + r = compute_level(&v, i); + free(v.v); + v.v = r; + } + + printf("%llu\n", v.v[fss(0, 0, 0, &v)]); } diff --git a/day17/common.c b/day17/common.c @@ -1,6 +1,3 @@ #include "common.h" -size_t fn(char** lines, size_t nlines) -{ - return 0; -} +size_t fn(char **lines, size_t nlines) { return 0; } diff --git a/day17/common.h b/day17/common.h @@ -1,11 +1,11 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> -size_t fn(char** lines, size_t nlines); +size_t fn(char **lines, size_t nlines); #endif diff --git a/day17/uppga.c b/day17/uppga.c @@ -5,7 +5,7 @@ typedef struct { size_t nalloc; long cmax; - uint8_t* bd; + uint8_t *bd; } board; typedef struct { @@ -13,31 +13,28 @@ typedef struct { uint8_t bd[4]; } piece; -static inline int isset(uint8_t row, size_t i) -{ +static inline int isset(uint8_t row, size_t i) { assert(i < 7); - if((row & (0x40 >> i)) != 0) return 1; + if ((row & (0x40 >> i)) != 0) + return 1; return 0; } -static inline void set(uint8_t* row, size_t i) -{ +static inline void set(uint8_t *row, size_t i) { assert(i < 7); *row |= (0x40 >> i); } -void init_board(board* b) -{ +void init_board(board *b) { b->nalloc = DEFAULT_BOARD_SIZE; b->cmax = -1; b->bd = calloc(b->nalloc, sizeof(*b->bd)); } -static inline void print_line(uint8_t v) -{ +static inline void print_line(uint8_t v) { size_t j; - for(j = 0; j < 7; j++) { - if(isset(v, j)) { + for (j = 0; j < 7; j++) { + if (isset(v, j)) { printf("#"); } else { printf("."); @@ -45,38 +42,35 @@ static inline void print_line(uint8_t v) } } -void print_piece(piece p) -{ +void print_piece(piece p) { printf("piece> h = %zu\n", p.height); size_t i; - for(i = 0; i < p.height; i++) { + for (i = 0; i < p.height; i++) { print_line(p.bd[i]); printf("\n"); } } -void print_board(board* b) -{ +void print_board(board *b) { int i; printf("+-------+\n"); - for(i = 0; i < b->cmax+2; i++) { + for (i = 0; i < b->cmax + 2; i++) { printf("|"); print_line(b->bd[i]); printf("|\n"); } } -void print_board_with_piece(board* b, piece* p, long height) -{ +void print_board_with_piece(board *b, piece *p, long height) { size_t i; printf("+-------+\n"); - for(i = 0; i < height+4; i++) { + for (i = 0; i < height + 4; i++) { uint8_t d = 0; - if(i < b->nalloc) { + if (i < b->nalloc) { d |= b->bd[i]; } - if(i >= height && (i-height) < p->height){ - d |= p->bd[i-height]; + if (i >= height && (i - height) < p->height) { + d |= p->bd[i - height]; } printf("|"); print_line(d); @@ -84,93 +78,95 @@ void print_board_with_piece(board* b, piece* p, long height) } } -#define SWAP(x,y) (x^=y^=x^=y) +#define SWAP(x, y) (x ^= y ^= x ^= y) -void read_pieces(piece* pcs, size_t npcs, char* filename) -{ - char** lines; +void read_pieces(piece *pcs, size_t npcs, char *filename) { + char **lines; size_t nlines = readlines(&lines, filename); size_t cp = 0; size_t cpi = 0; size_t i, j; - for(i = 0; i < npcs; i++) + for (i = 0; i < npcs; i++) memset(pcs[i].bd, 0, 4); - for(i = 0; i < nlines; i++) { - if(strlen(lines[i]) < 2) { + for (i = 0; i < nlines; i++) { + if (strlen(lines[i]) < 2) { pcs[cp].height = cpi; cp++; cpi = 0; continue; } - for(j = 0; j < strlen(lines[i]); j++) { - if(lines[i][j] == '#') { + for (j = 0; j < strlen(lines[i]); j++) { + if (lines[i][j] == '#') { set(&(pcs[cp].bd[cpi]), j); - } + } } cpi++; } pcs[cp].height = cpi; - for(i = 0; i < npcs; i++) { - for(j = 0; j < pcs[i].height/2; j++) { - SWAP(pcs[i].bd[j], pcs[i].bd[pcs[i].height-1-j]); + for (i = 0; i < npcs; i++) { + for (j = 0; j < pcs[i].height / 2; j++) { + SWAP(pcs[i].bd[j], pcs[i].bd[pcs[i].height - 1 - j]); } - for(j = 0; j < pcs[i].height; j++) pcs[i].bd[j] >>= 2; + for (j = 0; j < pcs[i].height; j++) + pcs[i].bd[j] >>= 2; } } -static inline void r_push_piece(piece* p, board* b, long h) -{ +static inline void r_push_piece(piece *p, board *b, long h) { size_t i; - for(i = 0; i < p->height; i++) { - if((p->bd[i] & 0x01) != 0) return; // cannot push - if(((p->bd[i] >> 1) & b->bd[h + i]) != 0) return; + for (i = 0; i < p->height; i++) { + if ((p->bd[i] & 0x01) != 0) + return; // cannot push + if (((p->bd[i] >> 1) & b->bd[h + i]) != 0) + return; } // pushable - for(i = 0; i < p->height; i++) + for (i = 0; i < p->height; i++) p->bd[i] >>= 1; } -static inline void l_push_piece(piece* p, board* b, long h) -{ +static inline void l_push_piece(piece *p, board *b, long h) { size_t i; - for(i = 0; i < p->height; i++) { - if((p->bd[i] & 0x40) != 0) return; // cannot - if(((p->bd[i] << 1) & b->bd[h + i]) != 0) return; + for (i = 0; i < p->height; i++) { + if ((p->bd[i] & 0x40) != 0) + return; // cannot + if (((p->bd[i] << 1) & b->bd[h + i]) != 0) + return; } // pushable - for(i = 0; i < p->height; i++) + for (i = 0; i < p->height; i++) p->bd[i] <<= 1; } -static inline int can_move_up(piece* p, board* b, long cheight) -{ +static inline int can_move_up(piece *p, board *b, long cheight) { size_t i; - if(cheight <= 0) return 0; - for(i = 0; i < p->height; i++) { - if((b->bd[i + cheight - 1] & p->bd[i]) != 0) return 0; + if (cheight <= 0) + return 0; + for (i = 0; i < p->height; i++) { + if ((b->bd[i + cheight - 1] & p->bd[i]) != 0) + return 0; } return 1; } -static inline void draw_piece(piece* p, board* b, long height) -{ +static inline void draw_piece(piece *p, board *b, long height) { size_t i; - for(i = 0; i < p->height; i++) { + for (i = 0; i < p->height; i++) { b->bd[i + height] |= p->bd[i]; } -// printf("> drawing piece of h %zu at h %ld\n", p->height, height); + // printf("> drawing piece of h %zu at h %ld\n", p->height, height); long nmax = height + p->height - 1; b->cmax = nmax > b->cmax ? nmax : b->cmax; } -static inline void try_lr_move(piece* p, board* b, long h, char* moves, size_t nmoves, size_t* i) -{ - if(moves[*i % nmoves] == '>') { +static inline void try_lr_move(piece *p, board *b, long h, char *moves, + size_t nmoves, size_t *i) { + if (moves[*i % nmoves] == '>') { r_push_piece(p, b, h); } else { l_push_piece(p, b, h); @@ -178,11 +174,10 @@ static inline void try_lr_move(piece* p, board* b, long h, char* moves, size_t n (*i)++; } -void drop_piece(board* b, piece p, char* moves, size_t nmoves, size_t* i) -{ +void drop_piece(board *b, piece p, char *moves, size_t nmoves, size_t *i) { long cheight = b->cmax + 4; try_lr_move(&p, b, cheight, moves, nmoves, i); - while(can_move_up(&p, b, cheight)) { + while (can_move_up(&p, b, cheight)) { cheight--; try_lr_move(&p, b, cheight, moves, nmoves, i); } @@ -190,17 +185,17 @@ void drop_piece(board* b, piece p, char* moves, size_t nmoves, size_t* i) draw_piece(&p, b, cheight); } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "einput"); printf("#lines: %lu\n", nlines); assert(nlines == 1); - char* moves = lines[0]; + char *moves = lines[0]; size_t nmoves = strlen(moves); - if(moves[nmoves-1] == '\n') nmoves--; + if (moves[nmoves - 1] == '\n') + nmoves--; printf("#moves: %zu\n", nmoves); @@ -211,14 +206,14 @@ int main(int argc, char** argv) read_pieces(pcs, 5, "pieces"); size_t i; - for(i = 0; i < 5; i++) { + for (i = 0; i < 5; i++) { print_piece(pcs[i]); } i = 0; size_t j; - for(j = 0; j < 2022; j++) { + for (j = 0; j < 2022; j++) { drop_piece(&b, pcs[j % 5], moves, nmoves, &i); } - printf("(%zu): %lu\n", j, b.cmax+1); + printf("(%zu): %lu\n", j, b.cmax + 1); } diff --git a/day17/uppgb.c b/day17/uppgb.c @@ -10,7 +10,7 @@ typedef struct { size_t nalloc; long cmax; - uint8_t* bd; + uint8_t *bd; } board; typedef struct { @@ -19,10 +19,10 @@ typedef struct { } piece; typedef struct { - uint8_t bd[TOP_SIZE]; - char moves[5*TOP_SIZE]; - size_t midiff; - long cmax; + uint8_t bd[TOP_SIZE]; + char moves[5 * TOP_SIZE]; + size_t midiff; + long cmax; } top; static top tops[MAX_NTOPS]; @@ -32,59 +32,57 @@ static size_t ntops = 0; static size_t cycle_length = 0; static long cycle_height = 0; -static inline int cmp_top(top a, top b) -{ - size_t i,j; - for(i = 0; i < TOP_SIZE; i++) { - if(a.bd[i] != b.bd[i]) return 1; - for(j = 0; j < 5; j++) { - if(a.moves[5*i+j] != b.moves[5*i+j]) return 1; - } - } - return 0; +static inline int cmp_top(top a, top b) { + size_t i, j; + for (i = 0; i < TOP_SIZE; i++) { + if (a.bd[i] != b.bd[i]) + return 1; + for (j = 0; j < 5; j++) { + if (a.moves[5 * i + j] != b.moves[5 * i + j]) + return 1; + } + } + return 0; } -static inline top get_top(board* b, char* moves, size_t nmoves, size_t mi, size_t midiff) -{ - top r; - size_t i; - size_t si = b->cmax >= TOP_SIZE ? b->cmax - TOP_SIZE : 0; - for(i = 0; i < TOP_SIZE; i++) { - r.bd[i] = b->bd[si + i]; - r.moves[i] = moves[(mi + i) % nmoves]; +static inline top get_top(board *b, char *moves, size_t nmoves, size_t mi, + size_t midiff) { + top r; + size_t i; + size_t si = b->cmax >= TOP_SIZE ? b->cmax - TOP_SIZE : 0; + for (i = 0; i < TOP_SIZE; i++) { + r.bd[i] = b->bd[si + i]; + r.moves[i] = moves[(mi + i) % nmoves]; + } + for (i = 0; i < 5 * TOP_SIZE; i++) { + r.moves[i] = moves[(mi + i) % nmoves]; } - for(i = 0; i < 5*TOP_SIZE; i++) { - r.moves[i] = moves[(mi + i) % nmoves]; - } - r.midiff = midiff; - return r; + r.midiff = midiff; + return r; } -static inline int isset(uint8_t row, size_t i) -{ +static inline int isset(uint8_t row, size_t i) { assert(i < 7); - if((row & (0x40 >> i)) != 0) return 1; + if ((row & (0x40 >> i)) != 0) + return 1; return 0; } -static inline void set(uint8_t* row, size_t i) -{ +static inline void set(uint8_t *row, size_t i) { assert(i < 7); *row |= (0x40 >> i); } -void init_board(board* b) -{ +void init_board(board *b) { b->nalloc = DEFAULT_BOARD_SIZE; b->cmax = -1; b->bd = calloc(b->nalloc, sizeof(*b->bd)); } -static inline void print_line(uint8_t v) -{ +static inline void print_line(uint8_t v) { size_t j; - for(j = 0; j < 7; j++) { - if(isset(v, j)) { + for (j = 0; j < 7; j++) { + if (isset(v, j)) { printf("#"); } else { printf("."); @@ -92,49 +90,45 @@ static inline void print_line(uint8_t v) } } -void print_piece(piece p) -{ +void print_piece(piece p) { printf("piece> h = %zu\n", p.height); size_t i; - for(i = 0; i < p.height; i++) { + for (i = 0; i < p.height; i++) { print_line(p.bd[i]); printf("\n"); } } -void print_board(board* b) -{ +void print_board(board *b) { int i; printf("+-------+\n"); - for(i = 0; i < b->cmax+2; i++) { + for (i = 0; i < b->cmax + 2; i++) { printf("|"); print_line(b->bd[i]); printf("|\n"); } } -void print_top(top t) -{ +void print_top(top t) { int i; - printf("+-------+ (%c..%c)\n", t.moves[0], t.moves[TOP_SIZE-1]); - for(i = 0; i < TOP_SIZE; i++) { + printf("+-------+ (%c..%c)\n", t.moves[0], t.moves[TOP_SIZE - 1]); + for (i = 0; i < TOP_SIZE; i++) { printf("|"); print_line(t.bd[i]); printf("|\n"); } } -void print_board_with_piece(board* b, piece* p, long height) -{ +void print_board_with_piece(board *b, piece *p, long height) { size_t i; printf("+-------+\n"); - for(i = 0; i < height+4; i++) { + for (i = 0; i < height + 4; i++) { uint8_t d = 0; - if(i < b->nalloc) { + if (i < b->nalloc) { d |= b->bd[i]; } - if(i >= height && (i-height) < p->height){ - d |= p->bd[i-height]; + if (i >= height && (i - height) < p->height) { + d |= p->bd[i - height]; } printf("|"); print_line(d); @@ -142,92 +136,94 @@ void print_board_with_piece(board* b, piece* p, long height) } } -#define SWAP(x,y) (x^=y^=x^=y) +#define SWAP(x, y) (x ^= y ^= x ^= y) -void read_pieces(piece* pcs, size_t npcs, char* filename) -{ - char** lines; +void read_pieces(piece *pcs, size_t npcs, char *filename) { + char **lines; size_t nlines = readlines(&lines, filename); size_t cp = 0; size_t cpi = 0; size_t i, j; - for(i = 0; i < npcs; i++) + for (i = 0; i < npcs; i++) memset(pcs[i].bd, 0, 4); - for(i = 0; i < nlines; i++) { - if(strlen(lines[i]) < 2) { + for (i = 0; i < nlines; i++) { + if (strlen(lines[i]) < 2) { pcs[cp].height = cpi; cp++; cpi = 0; continue; } - for(j = 0; j < strlen(lines[i]); j++) { - if(lines[i][j] == '#') { + for (j = 0; j < strlen(lines[i]); j++) { + if (lines[i][j] == '#') { set(&(pcs[cp].bd[cpi]), j); - } + } } cpi++; } pcs[cp].height = cpi; - for(i = 0; i < npcs; i++) { - for(j = 0; j < pcs[i].height/2; j++) { - SWAP(pcs[i].bd[j], pcs[i].bd[pcs[i].height-1-j]); + for (i = 0; i < npcs; i++) { + for (j = 0; j < pcs[i].height / 2; j++) { + SWAP(pcs[i].bd[j], pcs[i].bd[pcs[i].height - 1 - j]); } - for(j = 0; j < pcs[i].height; j++) pcs[i].bd[j] >>= 2; + for (j = 0; j < pcs[i].height; j++) + pcs[i].bd[j] >>= 2; } } -static inline void r_push_piece(piece* p, board* b, long h) -{ +static inline void r_push_piece(piece *p, board *b, long h) { size_t i; - for(i = 0; i < p->height; i++) { - if((p->bd[i] & 0x01) != 0) return; // cannot push - if(((p->bd[i] >> 1) & b->bd[h + i]) != 0) return; + for (i = 0; i < p->height; i++) { + if ((p->bd[i] & 0x01) != 0) + return; // cannot push + if (((p->bd[i] >> 1) & b->bd[h + i]) != 0) + return; } // pushable - for(i = 0; i < p->height; i++) + for (i = 0; i < p->height; i++) p->bd[i] >>= 1; } -static inline void l_push_piece(piece* p, board* b, long h) -{ +static inline void l_push_piece(piece *p, board *b, long h) { size_t i; - for(i = 0; i < p->height; i++) { - if((p->bd[i] & 0x40) != 0) return; // cannot - if(((p->bd[i] << 1) & b->bd[h + i]) != 0) return; + for (i = 0; i < p->height; i++) { + if ((p->bd[i] & 0x40) != 0) + return; // cannot + if (((p->bd[i] << 1) & b->bd[h + i]) != 0) + return; } // pushable - for(i = 0; i < p->height; i++) + for (i = 0; i < p->height; i++) p->bd[i] <<= 1; } -static inline int can_move_up(piece* p, board* b, long cheight) -{ +static inline int can_move_up(piece *p, board *b, long cheight) { size_t i; - if(cheight <= 0) return 0; - for(i = 0; i < p->height; i++) { - if((b->bd[i + cheight - 1] & p->bd[i]) != 0) return 0; + if (cheight <= 0) + return 0; + for (i = 0; i < p->height; i++) { + if ((b->bd[i + cheight - 1] & p->bd[i]) != 0) + return 0; } return 1; } -static inline void draw_piece(piece* p, board* b, long height) -{ +static inline void draw_piece(piece *p, board *b, long height) { size_t i; - for(i = 0; i < p->height; i++) { + for (i = 0; i < p->height; i++) { b->bd[i + height] |= p->bd[i]; } long nmax = height + p->height - 1; b->cmax = nmax > b->cmax ? nmax : b->cmax; } -static inline void try_lr_move(piece* p, board* b, long h, char* moves, size_t nmoves, size_t* i) -{ - if(moves[*i % nmoves] == '>') { +static inline void try_lr_move(piece *p, board *b, long h, char *moves, + size_t nmoves, size_t *i) { + if (moves[*i % nmoves] == '>') { r_push_piece(p, b, h); } else { l_push_piece(p, b, h); @@ -235,36 +231,35 @@ static inline void try_lr_move(piece* p, board* b, long h, char* moves, size_t n (*i)++; } -static inline size_t add_top(board* b, char* moves, size_t nmoves, size_t *i, size_t midiff, top t) -{ - size_t j; - t.midiff = midiff; - - int seen = 0; - for(j = 0; j < ntops; j++) { - if(cmp_top(t, tops[j]) == 0) { - seen = 1; - break; - } - } - if(seen) { - printf(">> seen top (ntops = %zu)\n", ntops); - return j; - } else { - printf(">> new top (ntops = %zu)\n", ntops); - assert(ntops < MAX_NTOPS); - tops[ntops] = t; - ntops++; - } - - return ntops-1; +static inline size_t add_top(board *b, char *moves, size_t nmoves, size_t *i, + size_t midiff, top t) { + size_t j; + t.midiff = midiff; + + int seen = 0; + for (j = 0; j < ntops; j++) { + if (cmp_top(t, tops[j]) == 0) { + seen = 1; + break; + } + } + if (seen) { + printf(">> seen top (ntops = %zu)\n", ntops); + return j; + } else { + printf(">> new top (ntops = %zu)\n", ntops); + assert(ntops < MAX_NTOPS); + tops[ntops] = t; + ntops++; + } + + return ntops - 1; } -void drop_piece(board* b, piece p, char* moves, size_t nmoves, size_t* i) -{ +void drop_piece(board *b, piece p, char *moves, size_t nmoves, size_t *i) { long cheight = b->cmax + 4; try_lr_move(&p, b, cheight, moves, nmoves, i); - while(can_move_up(&p, b, cheight)) { + while (can_move_up(&p, b, cheight)) { cheight--; try_lr_move(&p, b, cheight, moves, nmoves, i); } @@ -272,76 +267,76 @@ void drop_piece(board* b, piece p, char* moves, size_t nmoves, size_t* i) draw_piece(&p, b, cheight); } -static inline size_t find_top(top* t, board* b, char* moves, size_t nmoves, size_t* mi) -{ - *t = get_top(b, moves, nmoves, *mi, 0); - - print_top(*t); - - printf("next> "); - size_t j; - for(j = 0; j < ntops; j++) printf("%zu ", next[j]); - printf("\n"); - - int seen = 0; - for(j = 0; j < ntops; j++) { - if(cmp_top(*t, tops[j]) == 0) { - seen = 1; - break; - } - } - if(seen) { - printf(">> seen top (ntops = %zu)\n", ntops); - return j; - } - - return NO_TOP; +static inline size_t find_top(top *t, board *b, char *moves, size_t nmoves, + size_t *mi) { + *t = get_top(b, moves, nmoves, *mi, 0); + + print_top(*t); + + printf("next> "); + size_t j; + for (j = 0; j < ntops; j++) + printf("%zu ", next[j]); + printf("\n"); + + int seen = 0; + for (j = 0; j < ntops; j++) { + if (cmp_top(*t, tops[j]) == 0) { + seen = 1; + break; + } + } + if (seen) { + printf(">> seen top (ntops = %zu)\n", ntops); + return j; + } + + return NO_TOP; } -void drop_round(board* b, piece* pcs, char* moves, size_t nmoves, size_t* mi) -{ - top t; - if(cycle_length == 0) { +void drop_round(board *b, piece *pcs, char *moves, size_t nmoves, size_t *mi) { + top t; + if (cycle_length == 0) { size_t ti = find_top(&t, b, moves, nmoves, mi); - if(ti != NO_TOP) { - printf("ti = %zu\n", ti); - cycle_length = ntops - ti; - cycle_height = b->cmax - tops[ti].cmax; - printf("cycle length = %ld\n", cycle_length); - printf("cycle height = %ld\n", cycle_height); - } - } - - size_t last_mi = *mi; - size_t i; - for(i = 0; i < 5; i++) { + if (ti != NO_TOP) { + printf("ti = %zu\n", ti); + cycle_length = ntops - ti; + cycle_height = b->cmax - tops[ti].cmax; + printf("cycle length = %ld\n", cycle_length); + printf("cycle height = %ld\n", cycle_height); + } + } + + size_t last_mi = *mi; + size_t i; + for (i = 0; i < 5; i++) { drop_piece(b, pcs[i % 5], moves, nmoves, mi); - } - - if(cycle_length == 0) { - assert(*mi >= last_mi); - t.cmax = b->cmax; - size_t new_top_idx = add_top(b, moves, nmoves, &i, *mi-last_mi, t); - if(lastnext != SIZE_MAX) { - next[lastnext] = new_top_idx; - lastnext = new_top_idx; - } else { - lastnext = new_top_idx; - } - } + } + + if (cycle_length == 0) { + assert(*mi >= last_mi); + t.cmax = b->cmax; + size_t new_top_idx = add_top(b, moves, nmoves, &i, *mi - last_mi, t); + if (lastnext != SIZE_MAX) { + next[lastnext] = new_top_idx; + lastnext = new_top_idx; + } else { + lastnext = new_top_idx; + } + } } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); assert(nlines == 1); - char* moves = lines[0]; + char *moves = lines[0]; size_t nmoves = strlen(moves); - if(moves[nmoves-1] == '\n') nmoves--; + if (moves[nmoves - 1] == '\n') + nmoves--; printf("#moves: %zu\n", nmoves); @@ -352,38 +347,38 @@ int main(int argc, char** argv) read_pieces(pcs, 5, "pieces"); size_t i; - for(i = 0; i < 5; i++) { + for (i = 0; i < 5; i++) { print_piece(pcs[i]); } size_t j; - size_t mi = 0; - i = 0; + size_t mi = 0; + i = 0; top t; - size_t is[MAX_NTOPS]; - long skipval = 0; - for(j = MAXJ; j > 0; j--) { + size_t is[MAX_NTOPS]; + long skipval = 0; + for (j = MAXJ; j > 0; j--) { drop_piece(&b, pcs[i % 5], moves, nmoves, &mi); - if(skipval == 0 && i % 5 == 0) { - size_t ti = find_top(&t, &b, moves, nmoves, &mi); - if(ti == NO_TOP) { - is[ntops] = i; - add_top(&b, moves, nmoves, &mi, 0, t); - } else { - printf("ti = %zu\n", ti); - long diff = b.cmax - tops[ti].cmax; - printf("diff = %ld\n", diff); - long idiff = i - is[ti]; - printf("idiff = %ld\n", idiff); - size_t jold = j; - j = j % (i - is[ti]); - printf("skipping %ld js\n", (jold - j)/idiff); - skipval = ((jold - j)/idiff)*diff; - } - } - i++; - } - - printf("%lu\n", skipval + b.cmax + 1); + if (skipval == 0 && i % 5 == 0) { + size_t ti = find_top(&t, &b, moves, nmoves, &mi); + if (ti == NO_TOP) { + is[ntops] = i; + add_top(&b, moves, nmoves, &mi, 0, t); + } else { + printf("ti = %zu\n", ti); + long diff = b.cmax - tops[ti].cmax; + printf("diff = %ld\n", diff); + long idiff = i - is[ti]; + printf("idiff = %ld\n", idiff); + size_t jold = j; + j = j % (i - is[ti]); + printf("skipping %ld js\n", (jold - j) / idiff); + skipval = ((jold - j) / idiff) * diff; + } + } + i++; + } + + printf("%lu\n", skipval + b.cmax + 1); } diff --git a/day17/uppgc.c b/day17/uppgc.c @@ -8,7 +8,7 @@ typedef struct { size_t nalloc; long cmax; - uint8_t* bd; + uint8_t *bd; } board; typedef struct { @@ -16,31 +16,28 @@ typedef struct { uint8_t bd[4]; } piece; -static inline int isset(uint8_t row, size_t i) -{ +static inline int isset(uint8_t row, size_t i) { assert(i < 7); - if((row & (0x40 >> i)) != 0) return 1; + if ((row & (0x40 >> i)) != 0) + return 1; return 0; } -static inline void set(uint8_t* row, size_t i) -{ +static inline void set(uint8_t *row, size_t i) { assert(i < 7); *row |= (0x40 >> i); } -void init_board(board* b) -{ +void init_board(board *b) { b->nalloc = DEFAULT_BOARD_SIZE; b->cmax = -1; b->bd = calloc(b->nalloc, sizeof(*b->bd)); } -static inline void print_line(uint8_t v) -{ +static inline void print_line(uint8_t v) { size_t j; - for(j = 0; j < 7; j++) { - if(isset(v, j)) { + for (j = 0; j < 7; j++) { + if (isset(v, j)) { printf("#"); } else { printf("."); @@ -48,38 +45,35 @@ static inline void print_line(uint8_t v) } } -void print_piece(piece p) -{ +void print_piece(piece p) { printf("piece> h = %zu\n", p.height); size_t i; - for(i = 0; i < p.height; i++) { + for (i = 0; i < p.height; i++) { print_line(p.bd[i]); printf("\n"); } } -void print_board(board* b) -{ +void print_board(board *b) { int i; printf("+-------+\n"); - for(i = 0; i < b->cmax+2; i++) { + for (i = 0; i < b->cmax + 2; i++) { printf("|"); print_line(b->bd[i]); printf("|\n"); } } -void print_board_with_piece(board* b, piece* p, long height) -{ +void print_board_with_piece(board *b, piece *p, long height) { size_t i; printf("+-------+\n"); - for(i = 0; i < height+4; i++) { + for (i = 0; i < height + 4; i++) { uint8_t d = 0; - if(i < b->nalloc) { + if (i < b->nalloc) { d |= b->bd[i]; } - if(i >= height && (i-height) < p->height){ - d |= p->bd[i-height]; + if (i >= height && (i - height) < p->height) { + d |= p->bd[i - height]; } printf("|"); print_line(d); @@ -87,93 +81,95 @@ void print_board_with_piece(board* b, piece* p, long height) } } -#define SWAP(x,y) (x^=y^=x^=y) +#define SWAP(x, y) (x ^= y ^= x ^= y) -void read_pieces(piece* pcs, size_t npcs, char* filename) -{ - char** lines; +void read_pieces(piece *pcs, size_t npcs, char *filename) { + char **lines; size_t nlines = readlines(&lines, filename); size_t cp = 0; size_t cpi = 0; size_t i, j; - for(i = 0; i < npcs; i++) + for (i = 0; i < npcs; i++) memset(pcs[i].bd, 0, 4); - for(i = 0; i < nlines; i++) { - if(strlen(lines[i]) < 2) { + for (i = 0; i < nlines; i++) { + if (strlen(lines[i]) < 2) { pcs[cp].height = cpi; cp++; cpi = 0; continue; } - for(j = 0; j < strlen(lines[i]); j++) { - if(lines[i][j] == '#') { + for (j = 0; j < strlen(lines[i]); j++) { + if (lines[i][j] == '#') { set(&(pcs[cp].bd[cpi]), j); - } + } } cpi++; } pcs[cp].height = cpi; - for(i = 0; i < npcs; i++) { - for(j = 0; j < pcs[i].height/2; j++) { - SWAP(pcs[i].bd[j], pcs[i].bd[pcs[i].height-1-j]); + for (i = 0; i < npcs; i++) { + for (j = 0; j < pcs[i].height / 2; j++) { + SWAP(pcs[i].bd[j], pcs[i].bd[pcs[i].height - 1 - j]); } - for(j = 0; j < pcs[i].height; j++) pcs[i].bd[j] >>= 2; + for (j = 0; j < pcs[i].height; j++) + pcs[i].bd[j] >>= 2; } } -static inline void r_push_piece(piece* p, board* b, long h) -{ +static inline void r_push_piece(piece *p, board *b, long h) { size_t i; - for(i = 0; i < p->height; i++) { - if((p->bd[i] & 0x01) != 0) return; // cannot push - if(((p->bd[i] >> 1) & b->bd[h + i]) != 0) return; + for (i = 0; i < p->height; i++) { + if ((p->bd[i] & 0x01) != 0) + return; // cannot push + if (((p->bd[i] >> 1) & b->bd[h + i]) != 0) + return; } // pushable - for(i = 0; i < p->height; i++) + for (i = 0; i < p->height; i++) p->bd[i] >>= 1; } -static inline void l_push_piece(piece* p, board* b, long h) -{ +static inline void l_push_piece(piece *p, board *b, long h) { size_t i; - for(i = 0; i < p->height; i++) { - if((p->bd[i] & 0x40) != 0) return; // cannot - if(((p->bd[i] << 1) & b->bd[h + i]) != 0) return; + for (i = 0; i < p->height; i++) { + if ((p->bd[i] & 0x40) != 0) + return; // cannot + if (((p->bd[i] << 1) & b->bd[h + i]) != 0) + return; } // pushable - for(i = 0; i < p->height; i++) + for (i = 0; i < p->height; i++) p->bd[i] <<= 1; } -static inline int can_move_up(piece* p, board* b, long cheight) -{ +static inline int can_move_up(piece *p, board *b, long cheight) { size_t i; - if(cheight <= 0) return 0; - for(i = 0; i < p->height; i++) { - if((b->bd[i + cheight - 1] & p->bd[i]) != 0) return 0; + if (cheight <= 0) + return 0; + for (i = 0; i < p->height; i++) { + if ((b->bd[i + cheight - 1] & p->bd[i]) != 0) + return 0; } return 1; } -static inline void draw_piece(piece* p, board* b, long height) -{ +static inline void draw_piece(piece *p, board *b, long height) { size_t i; - for(i = 0; i < p->height; i++) { + for (i = 0; i < p->height; i++) { b->bd[i + height] |= p->bd[i]; } -// printf("> drawing piece of h %zu at h %ld\n", p->height, height); + // printf("> drawing piece of h %zu at h %ld\n", p->height, height); long nmax = height + p->height - 1; b->cmax = nmax > b->cmax ? nmax : b->cmax; } -static inline void try_lr_move(piece* p, board* b, long h, char* moves, size_t nmoves, size_t* i) -{ - if(moves[*i % nmoves] == '>') { +static inline void try_lr_move(piece *p, board *b, long h, char *moves, + size_t nmoves, size_t *i) { + if (moves[*i % nmoves] == '>') { r_push_piece(p, b, h); } else { l_push_piece(p, b, h); @@ -181,11 +177,11 @@ static inline void try_lr_move(piece* p, board* b, long h, char* moves, size_t n (*i)++; } -static inline void drop_piece(board* b, piece p, char* moves, size_t nmoves, size_t* i) -{ +static inline void drop_piece(board *b, piece p, char *moves, size_t nmoves, + size_t *i) { long cheight = b->cmax + 4; try_lr_move(&p, b, cheight, moves, nmoves, i); - while(can_move_up(&p, b, cheight)) { + while (can_move_up(&p, b, cheight)) { cheight--; try_lr_move(&p, b, cheight, moves, nmoves, i); } @@ -193,25 +189,25 @@ static inline void drop_piece(board* b, piece p, char* moves, size_t nmoves, siz draw_piece(&p, b, cheight); } -static inline long chomp_board(board* b) { - long chomping = DEFAULT_BOARD_SIZE/2; +static inline long chomp_board(board *b) { + long chomping = DEFAULT_BOARD_SIZE / 2; b->cmax -= chomping; memcpy(b->bd, &(b->bd[chomping]), chomping); bzero(&(b->bd[chomping]), chomping); return chomping; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); assert(nlines == 1); - char* moves = lines[0]; + char *moves = lines[0]; size_t nmoves = strlen(moves); - if(moves[nmoves-1] == '\n') nmoves--; + if (moves[nmoves - 1] == '\n') + nmoves--; printf("#moves: %zu\n", nmoves); @@ -222,19 +218,20 @@ int main(int argc, char** argv) read_pieces(pcs, 5, "pieces"); size_t i; - for(i = 0; i < 5; i++) { + for (i = 0; i < 5; i++) { print_piece(pcs[i]); } i = 0; size_t j; long chompd = 0; - for(j = 0; j < MAXJ; j++) { + for (j = 0; j < MAXJ; j++) { drop_piece(&b, pcs[j % 5], moves, nmoves, &i); - if(b.cmax + 5 > DEFAULT_BOARD_SIZE) + if (b.cmax + 5 > DEFAULT_BOARD_SIZE) chompd += chomp_board(&b); - if((j % (MAXJ/1000)) == 0) printf("%lu\n", j / (MAXJ/1000)); + if ((j % (MAXJ / 1000)) == 0) + printf("%lu\n", j / (MAXJ / 1000)); } - printf("(%zu): %lu\n", j, chompd+b.cmax+1); + printf("(%zu): %lu\n", j, chompd + b.cmax + 1); printf("chompd %lu\n", chompd); } diff --git a/day18/common.c b/day18/common.c @@ -1,6 +1,3 @@ #include "common.h" -size_t fn(char** lines, size_t nlines) -{ - return 0; -} +size_t fn(char **lines, size_t nlines) { return 0; } diff --git a/day18/common.h b/day18/common.h @@ -1,11 +1,11 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> -size_t fn(char** lines, size_t nlines); +size_t fn(char **lines, size_t nlines); #endif diff --git a/day18/uppga.c b/day18/uppga.c @@ -2,87 +2,83 @@ typedef struct cube { uint64_t coord[3]; - size_t nnbrs; - size_t* nbrs; + size_t nnbrs; + size_t *nbrs; } cube; -void print_cube(cube* c) -{ - printf("(%llu, %llu, %llu)\n", c->coord[0], c->coord[1], c->coord[2]); - - size_t i; - printf(" |->"); - for(i = 0; i < c->nnbrs; i++) { - printf(" %zu", c->nbrs[i]); - } - printf("\n"); +void print_cube(cube *c) { + printf("(%llu, %llu, %llu)\n", c->coord[0], c->coord[1], c->coord[2]); + + size_t i; + printf(" |->"); + for (i = 0; i < c->nnbrs; i++) { + printf(" %zu", c->nbrs[i]); + } + printf("\n"); } -int adjacent(cube* a, cube* b) -{ - int x1 = a->coord[0]; - int x2 = b->coord[0]; - int y1 = a->coord[1]; - int y2 = b->coord[1]; - int z1 = a->coord[2]; - int z2 = b->coord[2]; - return (abs(x1 - x2) <= 1 && y1 == y2 && z1 == z2) - || (abs(y1 - y2) <= 1 && x1 == x2 && z1 == z2) - || (abs(z1 - z2) <= 1 && x1 == x2 && y1 == y2); +int adjacent(cube *a, cube *b) { + int x1 = a->coord[0]; + int x2 = b->coord[0]; + int y1 = a->coord[1]; + int y2 = b->coord[1]; + int z1 = a->coord[2]; + int z2 = b->coord[2]; + return (abs(x1 - x2) <= 1 && y1 == y2 && z1 == z2) || + (abs(y1 - y2) <= 1 && x1 == x2 && z1 == z2) || + (abs(z1 - z2) <= 1 && x1 == x2 && y1 == y2); } -void add_nbr(cube* x, size_t nbr_index) -{ -// printf("adding nbrs to cube (%llu, %llu, %llu)\n", x->coord[0], x->coord[1], x->coord[2]); - x->nbrs[x->nnbrs] = nbr_index; - x->nnbrs++; +void add_nbr(cube *x, size_t nbr_index) { + // printf("adding nbrs to cube (%llu, %llu, %llu)\n", x->coord[0], + //x->coord[1], x->coord[2]); + x->nbrs[x->nnbrs] = nbr_index; + x->nnbrs++; } -uint64_t e(cube* cubes, size_t ncubes) -{ - uint64_t c = 0; +uint64_t e(cube *cubes, size_t ncubes) { + uint64_t c = 0; - size_t i; - for(i = 0; i < ncubes; i++) - c += cubes[i].nnbrs; + size_t i; + for (i = 0; i < ncubes; i++) + c += cubes[i].nnbrs; - return c/2; + return c / 2; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - cube cubes[nlines]; - size_t i,j; - for(i = 0; i < nlines; i++) { - char* line = lines[i]; - line = sread_next_u64(&cubes[i].coord[0], line); - line = sread_next_u64(&cubes[i].coord[1], line); - line = sread_next_u64(&cubes[i].coord[2], line); + cube cubes[nlines]; + size_t i, j; + for (i = 0; i < nlines; i++) { + char *line = lines[i]; + line = sread_next_u64(&cubes[i].coord[0], line); + line = sread_next_u64(&cubes[i].coord[1], line); + line = sread_next_u64(&cubes[i].coord[2], line); - cubes[i].nnbrs = 0; - cubes[i].nbrs = malloc(nlines * sizeof(*(cubes[i].nbrs))); - } + cubes[i].nnbrs = 0; + cubes[i].nbrs = malloc(nlines * sizeof(*(cubes[i].nbrs))); + } - for(i = 0; i < nlines; i++) { - for(j = i+1; j < nlines; j++) { - if(adjacent(&cubes[i], &cubes[j])) { - add_nbr(&cubes[i], j); - add_nbr(&cubes[j], i); - } - } - } + for (i = 0; i < nlines; i++) { + for (j = i + 1; j < nlines; j++) { + if (adjacent(&cubes[i], &cubes[j])) { + add_nbr(&cubes[i], j); + add_nbr(&cubes[j], i); + } + } + } - uint64_t ee = e(cubes, nlines); - printf("%llu\n", ee); + uint64_t ee = e(cubes, nlines); + printf("%llu\n", ee); - for(i = 0; i < nlines; i++) { - print_cube(&cubes[i]); - } + for (i = 0; i < nlines; i++) { + print_cube(&cubes[i]); + } - assert(6*nlines >= ee); - printf("%llu\n", 6*nlines - 2*ee); + assert(6 * nlines >= ee); + printf("%llu\n", 6 * nlines - 2 * ee); } diff --git a/day18/uppgb.c b/day18/uppgb.c @@ -4,227 +4,249 @@ typedef struct cube { uint64_t coord[3]; - size_t nnbrs; - size_t* nbrs; + size_t nnbrs; + size_t *nbrs; } cube; -void print_cube(cube* c) -{ - printf("(%llu, %llu, %llu)\n", c->coord[0], c->coord[1], c->coord[2]); +void print_cube(cube *c) { + printf("(%llu, %llu, %llu)\n", c->coord[0], c->coord[1], c->coord[2]); } typedef struct entry { - uint64_t x; - uint64_t y; - uint64_t z; + uint64_t x; + uint64_t y; + uint64_t z; STAILQ_ENTRY(entry) entries; } entry; -int adjacent(cube* a, cube* b) -{ - int x1 = a->coord[0]; - int x2 = b->coord[0]; - int y1 = a->coord[1]; - int y2 = b->coord[1]; - int z1 = a->coord[2]; - int z2 = b->coord[2]; - return (abs(x1 - x2) <= 1 && y1 == y2 && z1 == z2) - || (abs(y1 - y2) <= 1 && x1 == x2 && z1 == z2) - || (abs(z1 - z2) <= 1 && x1 == x2 && y1 == y2); +int adjacent(cube *a, cube *b) { + int x1 = a->coord[0]; + int x2 = b->coord[0]; + int y1 = a->coord[1]; + int y2 = b->coord[1]; + int z1 = a->coord[2]; + int z2 = b->coord[2]; + return (abs(x1 - x2) <= 1 && y1 == y2 && z1 == z2) || + (abs(y1 - y2) <= 1 && x1 == x2 && z1 == z2) || + (abs(z1 - z2) <= 1 && x1 == x2 && y1 == y2); } -void add_nbr(cube* x, size_t nbr_index) -{ -// printf("adding nbrs to cube (%llu, %llu, %llu)\n", x->coord[0], x->coord[1], x->coord[2]); - x->nbrs[x->nnbrs] = nbr_index; - x->nnbrs++; +void add_nbr(cube *x, size_t nbr_index) { + // printf("adding nbrs to cube (%llu, %llu, %llu)\n", x->coord[0], + //x->coord[1], x->coord[2]); + x->nbrs[x->nnbrs] = nbr_index; + x->nnbrs++; } -uint64_t e(cube* cubes, size_t ncubes) -{ - uint64_t c = 0; +uint64_t e(cube *cubes, size_t ncubes) { + uint64_t c = 0; - size_t i; - for(i = 0; i < ncubes; i++) - c += cubes[i].nnbrs; + size_t i; + for (i = 0; i < ncubes; i++) + c += cubes[i].nnbrs; - return c/2; + return c / 2; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - cube cubes[nlines]; - size_t i,j,k; - for(i = 0; i < nlines; i++) { - char* line = lines[i]; - line = sread_next_u64(&cubes[i].coord[0], line); - line = sread_next_u64(&cubes[i].coord[1], line); - line = sread_next_u64(&cubes[i].coord[2], line); - } - - uint64_t maxx = 0, maxy = 0, maxz = 0; - for(i = 0; i < nlines; i++) { - if(maxx < cubes[i].coord[0]) maxx = cubes[i].coord[0]; - if(maxy < cubes[i].coord[1]) maxy = cubes[i].coord[1]; - if(maxz < cubes[i].coord[2]) maxz = cubes[i].coord[2]; - } - - printf("maxx = %llu, maxy = %llu, maxz = %llu\n", maxx, maxy, maxz); - - uint8_t filler[maxx+1][maxy+1][maxz+1]; - memset(filler, 0, (maxx+1)*(maxy+1)*(maxz+1)); - - for(i = 0; i < nlines; i++) { - filler[cubes[i].coord[0]][cubes[i].coord[1]][cubes[i].coord[2]] = 2; - } - - size_t filled = 0; - STAILQ_HEAD(listhead, entry) head = STAILQ_HEAD_INITIALIZER(head); - - - entry *elt; - - for(i = 0; i < maxy+1; i++) { - for(j = 0; j < maxz+1; j++) { - if(filler[0][i][j] == 0) { - elt = malloc(sizeof(*elt)); - elt->x = 0; elt->y = i; elt->z = j; - filler[elt->x][elt->y][elt->z] = 1; - STAILQ_INSERT_TAIL(&head, elt, entries); - } - if(filler[maxx][i][j] == 0) { - elt = malloc(sizeof(*elt)); - elt->x = maxx; elt->y = i; elt->z = j; - filler[elt->x][elt->y][elt->z] = 1; - STAILQ_INSERT_TAIL(&head, elt, entries); - } - } - } - - for(i = 0; i < maxx+1; i++) { - for(j = 0; j < maxz+1; j++) { - if(filler[i][0][j] == 0) { - elt = malloc(sizeof(*elt)); - elt->x = i; elt->y = 0; elt->z = j; - filler[elt->x][elt->y][elt->z] = 1; - STAILQ_INSERT_TAIL(&head, elt, entries); - } - if(filler[i][maxy][j] == 0) { - elt = malloc(sizeof(*elt)); - elt->x = i; elt->y = maxy; elt->z = j; - filler[elt->x][elt->y][elt->z] = 1; - STAILQ_INSERT_TAIL(&head, elt, entries); - } - } - } - - for(i = 0; i < maxx+1; i++) { - for(j = 0; j < maxy+1; j++) { - if(filler[i][j][0] == 0) { - elt = malloc(sizeof(*elt)); - elt->x = i; elt->y = j; elt->z = 0; - filler[elt->x][elt->y][elt->z] = 1; - STAILQ_INSERT_TAIL(&head, elt, entries); - } - if(filler[i][j][maxz] == 0) { - elt = malloc(sizeof(*elt)); - elt->x = i; elt->y = j; elt->z = maxz; - filler[elt->x][elt->y][elt->z] = 1; - STAILQ_INSERT_TAIL(&head, elt, entries); - } - } - } - - while(!STAILQ_EMPTY(&head)) { - elt = STAILQ_FIRST(&head); - STAILQ_REMOVE_HEAD(&head, entries); - filled++; - - if(elt->x > 0 && filler[elt->x-1][elt->y][elt->z] == 0) { - entry* new = malloc(sizeof(*new)); - new->x = elt->x-1; new->y = elt->y; new->z = elt->z; - filler[new->x][new->y][new->z] = 1; - STAILQ_INSERT_TAIL(&head, new, entries); - } - if(elt->x < maxx && filler[elt->x+1][elt->y][elt->z] == 0) { - entry* new = malloc(sizeof(*new)); - new->x = elt->x+1; new->y = elt->y; new->z = elt->z; - filler[new->x][new->y][new->z] = 1; - STAILQ_INSERT_TAIL(&head, new, entries); - } - - if(elt->y > 0 && filler[elt->x][elt->y-1][elt->z] == 0) { - entry* new = malloc(sizeof(*new)); - new->x = elt->x; new->y = elt->y-1; new->z = elt->z; - filler[new->x][new->y][new->z] = 1; - STAILQ_INSERT_TAIL(&head, new, entries); - } - if(elt->y < maxy && filler[elt->x][elt->y+1][elt->z] == 0) { - entry* new = malloc(sizeof(*new)); - new->x = elt->x; new->y = elt->y+1; new->z = elt->z; - filler[new->x][new->y][new->z] = 1; - STAILQ_INSERT_TAIL(&head, new, entries); - } - - if(elt->z > 0 && filler[elt->x][elt->y][elt->z-1] == 0) { - entry* new = malloc(sizeof(*new)); - new->x = elt->x; new->y = elt->y; new->z = elt->z-1; - filler[new->x][new->y][new->z] = 1; - STAILQ_INSERT_TAIL(&head, new, entries); - } - if(elt->z < maxz && filler[elt->x][elt->y][elt->z+1] == 0) { - entry* new = malloc(sizeof(*new)); - new->x = elt->x; new->y = elt->y; new->z = elt->z+1; - filler[new->x][new->y][new->z] = 1; - STAILQ_INSERT_TAIL(&head, new, entries); - } - - free(elt); + cube cubes[nlines]; + size_t i, j, k; + for (i = 0; i < nlines; i++) { + char *line = lines[i]; + line = sread_next_u64(&cubes[i].coord[0], line); + line = sread_next_u64(&cubes[i].coord[1], line); + line = sread_next_u64(&cubes[i].coord[2], line); } - size_t nucubes = (maxx+1)*(maxy+1)*(maxz+1) - filled; - printf("nucubes = %zu\n", nucubes); - - cube ucubes[nucubes]; - size_t c = 0; - for(i = 0; i < maxx+1; i++) { - for(j = 0; j < maxy+1; j++) { - for(k = 0; k < maxz+1; k++) { - if(filler[i][j][k] != 1) { - ucubes[c].coord[0] = i; - ucubes[c].coord[1] = j; - ucubes[c].coord[2] = k; - - ucubes[c].nnbrs = 0; - ucubes[c].nbrs = malloc(nucubes * sizeof(*(ucubes[c].nbrs))); - - c++; - } - } - } - } - - for(i = 0; i < nucubes; i++) { - for(j = i+1; j < nucubes; j++) { - if(adjacent(&ucubes[i], &ucubes[j])) { - add_nbr(&ucubes[i], j); - add_nbr(&ucubes[j], i); - } - } - } - - for(i = 0; i < nucubes; i++) { - print_cube(&ucubes[i]); - } - - uint64_t ee = e(ucubes, nucubes); - printf("ee = %llu\n", ee); - - assert(6*nucubes >= ee); - printf("%llu\n", 6*nucubes - 2*ee); + uint64_t maxx = 0, maxy = 0, maxz = 0; + for (i = 0; i < nlines; i++) { + if (maxx < cubes[i].coord[0]) + maxx = cubes[i].coord[0]; + if (maxy < cubes[i].coord[1]) + maxy = cubes[i].coord[1]; + if (maxz < cubes[i].coord[2]) + maxz = cubes[i].coord[2]; + } + + printf("maxx = %llu, maxy = %llu, maxz = %llu\n", maxx, maxy, maxz); + + uint8_t filler[maxx + 1][maxy + 1][maxz + 1]; + memset(filler, 0, (maxx + 1) * (maxy + 1) * (maxz + 1)); + + for (i = 0; i < nlines; i++) { + filler[cubes[i].coord[0]][cubes[i].coord[1]][cubes[i].coord[2]] = 2; + } + + size_t filled = 0; + STAILQ_HEAD(listhead, entry) head = STAILQ_HEAD_INITIALIZER(head); + + entry *elt; + + for (i = 0; i < maxy + 1; i++) { + for (j = 0; j < maxz + 1; j++) { + if (filler[0][i][j] == 0) { + elt = malloc(sizeof(*elt)); + elt->x = 0; + elt->y = i; + elt->z = j; + filler[elt->x][elt->y][elt->z] = 1; + STAILQ_INSERT_TAIL(&head, elt, entries); + } + if (filler[maxx][i][j] == 0) { + elt = malloc(sizeof(*elt)); + elt->x = maxx; + elt->y = i; + elt->z = j; + filler[elt->x][elt->y][elt->z] = 1; + STAILQ_INSERT_TAIL(&head, elt, entries); + } + } + } + + for (i = 0; i < maxx + 1; i++) { + for (j = 0; j < maxz + 1; j++) { + if (filler[i][0][j] == 0) { + elt = malloc(sizeof(*elt)); + elt->x = i; + elt->y = 0; + elt->z = j; + filler[elt->x][elt->y][elt->z] = 1; + STAILQ_INSERT_TAIL(&head, elt, entries); + } + if (filler[i][maxy][j] == 0) { + elt = malloc(sizeof(*elt)); + elt->x = i; + elt->y = maxy; + elt->z = j; + filler[elt->x][elt->y][elt->z] = 1; + STAILQ_INSERT_TAIL(&head, elt, entries); + } + } + } + + for (i = 0; i < maxx + 1; i++) { + for (j = 0; j < maxy + 1; j++) { + if (filler[i][j][0] == 0) { + elt = malloc(sizeof(*elt)); + elt->x = i; + elt->y = j; + elt->z = 0; + filler[elt->x][elt->y][elt->z] = 1; + STAILQ_INSERT_TAIL(&head, elt, entries); + } + if (filler[i][j][maxz] == 0) { + elt = malloc(sizeof(*elt)); + elt->x = i; + elt->y = j; + elt->z = maxz; + filler[elt->x][elt->y][elt->z] = 1; + STAILQ_INSERT_TAIL(&head, elt, entries); + } + } + } + + while (!STAILQ_EMPTY(&head)) { + elt = STAILQ_FIRST(&head); + STAILQ_REMOVE_HEAD(&head, entries); + filled++; + + if (elt->x > 0 && filler[elt->x - 1][elt->y][elt->z] == 0) { + entry *new = malloc(sizeof(*new)); + new->x = elt->x - 1; + new->y = elt->y; + new->z = elt->z; + filler[new->x][new->y][new->z] = 1; + STAILQ_INSERT_TAIL(&head, new, entries); + } + if (elt->x < maxx && filler[elt->x + 1][elt->y][elt->z] == 0) { + entry *new = malloc(sizeof(*new)); + new->x = elt->x + 1; + new->y = elt->y; + new->z = elt->z; + filler[new->x][new->y][new->z] = 1; + STAILQ_INSERT_TAIL(&head, new, entries); + } + + if (elt->y > 0 && filler[elt->x][elt->y - 1][elt->z] == 0) { + entry *new = malloc(sizeof(*new)); + new->x = elt->x; + new->y = elt->y - 1; + new->z = elt->z; + filler[new->x][new->y][new->z] = 1; + STAILQ_INSERT_TAIL(&head, new, entries); + } + if (elt->y < maxy && filler[elt->x][elt->y + 1][elt->z] == 0) { + entry *new = malloc(sizeof(*new)); + new->x = elt->x; + new->y = elt->y + 1; + new->z = elt->z; + filler[new->x][new->y][new->z] = 1; + STAILQ_INSERT_TAIL(&head, new, entries); + } + + if (elt->z > 0 && filler[elt->x][elt->y][elt->z - 1] == 0) { + entry *new = malloc(sizeof(*new)); + new->x = elt->x; + new->y = elt->y; + new->z = elt->z - 1; + filler[new->x][new->y][new->z] = 1; + STAILQ_INSERT_TAIL(&head, new, entries); + } + if (elt->z < maxz && filler[elt->x][elt->y][elt->z + 1] == 0) { + entry *new = malloc(sizeof(*new)); + new->x = elt->x; + new->y = elt->y; + new->z = elt->z + 1; + filler[new->x][new->y][new->z] = 1; + STAILQ_INSERT_TAIL(&head, new, entries); + } + + free(elt); + } + + size_t nucubes = (maxx + 1) * (maxy + 1) * (maxz + 1) - filled; + printf("nucubes = %zu\n", nucubes); + + cube ucubes[nucubes]; + size_t c = 0; + for (i = 0; i < maxx + 1; i++) { + for (j = 0; j < maxy + 1; j++) { + for (k = 0; k < maxz + 1; k++) { + if (filler[i][j][k] != 1) { + ucubes[c].coord[0] = i; + ucubes[c].coord[1] = j; + ucubes[c].coord[2] = k; + + ucubes[c].nnbrs = 0; + ucubes[c].nbrs = malloc(nucubes * sizeof(*(ucubes[c].nbrs))); + + c++; + } + } + } + } + + for (i = 0; i < nucubes; i++) { + for (j = i + 1; j < nucubes; j++) { + if (adjacent(&ucubes[i], &ucubes[j])) { + add_nbr(&ucubes[i], j); + add_nbr(&ucubes[j], i); + } + } + } + + for (i = 0; i < nucubes; i++) { + print_cube(&ucubes[i]); + } + + uint64_t ee = e(ucubes, nucubes); + printf("ee = %llu\n", ee); + + assert(6 * nucubes >= ee); + printf("%llu\n", 6 * nucubes - 2 * ee); } diff --git a/day19/common.c b/day19/common.c @@ -1,25 +1,23 @@ #include "common.h" -void print_blueprint(blueprint* bp) -{ - size_t i,j; - for(i = 0; i < RTYPES; i++) { +void print_blueprint(blueprint *bp) { + size_t i, j; + for (i = 0; i < RTYPES; i++) { printf("(%zu):\t", i); - for(j = 0; j < MTYPES; j++) { + for (j = 0; j < MTYPES; j++) { printf("%llu\t", bp->c[i][j]); } printf("\n"); } } -blueprint* parse(char** lines, size_t nlines) -{ +blueprint *parse(char **lines, size_t nlines) { uint64_t idx; - blueprint* bps = malloc(nlines * sizeof(*bps)); + blueprint *bps = malloc(nlines * sizeof(*bps)); size_t i; - for(i = 0; i < nlines; i++) { - char* line = lines[i]; + for (i = 0; i < nlines; i++) { + char *line = lines[i]; memset(bps[i].c, 0, RTYPES * MTYPES * sizeof(bps[i].c[0][0])); line = sread_next_u64(&idx, line); line = sread_next_u64(&bps[i].c[ORE][ORE], line); @@ -33,23 +31,26 @@ blueprint* parse(char** lines, size_t nlines) return bps; } -state start_state() -{ +state start_state() { state s; memset(s.nmin, 0, MTYPES * sizeof(s.nmin[0])); s.nrob[ORE] = 1; - s.nrob[CLA] = 0; s.nrob[OBS] = 0; s.nrob[GEO] = 0; + s.nrob[CLA] = 0; + s.nrob[OBS] = 0; + s.nrob[GEO] = 0; return s; } -state empty_state() -{ +state empty_state() { state s; memset(s.nmin, 0, MTYPES * sizeof(s.nmin[0])); - s.nrob[CLA] = 0; s.nrob[OBS] = 0; s.nrob[GEO] = 0; s.nrob[ORE] = 0; + s.nrob[CLA] = 0; + s.nrob[OBS] = 0; + s.nrob[GEO] = 0; + s.nrob[ORE] = 0; return s; } diff --git a/day19/common.h b/day19/common.h @@ -1,10 +1,10 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> #define RTYPES 4 // ore, clay, obsidian, geode #define MTYPES 4 // ore, clay, obsidian, geode @@ -27,11 +27,11 @@ typedef struct { typedef struct { uint64_t nrob[RTYPES]; - uint64_t nmin[MTYPES]; + uint64_t nmin[MTYPES]; } state; -blueprint* parse(char** lines, size_t nlines); -void print_blueprint(blueprint* bp); +blueprint *parse(char **lines, size_t nlines); +void print_blueprint(blueprint *bp); state start_state(); state empty_state(); diff --git a/day19/uppga.c b/day19/uppga.c @@ -1,171 +1,174 @@ #include "common.h" -static inline void printopt(uint8_t* opt, size_t nopt) -{ - size_t i; - for(i = 0; i < nopt; i++) { - printf("%hhu ", opt[i]); - } - printf("\n"); +static inline void printopt(uint8_t *opt, size_t nopt) { + size_t i; + for (i = 0; i < nopt; i++) { + printf("%hhu ", opt[i]); + } + printf("\n"); } -static inline int affordable(size_t rtype, const blueprint* bp, state* s) -{ - size_t i; - for(i = 0; i < MTYPES; i++) { - if(s->nmin[i] < bp->c[rtype][i]) return 0; - } - return 1; +static inline int affordable(size_t rtype, const blueprint *bp, state *s) { + size_t i; + for (i = 0; i < MTYPES; i++) { + if (s->nmin[i] < bp->c[rtype][i]) + return 0; + } + return 1; } -static inline int verbose_affordable(size_t rtype, const blueprint* bp, state* s) -{ - size_t i; - for(i = 0; i < MTYPES; i++) { - printf("%llu ?< %llu\n", s->nmin[i], bp->c[rtype][i]); - if(s->nmin[i] < bp->c[rtype][i]) return 0; - } - return 1; +static inline int verbose_affordable(size_t rtype, const blueprint *bp, + state *s) { + size_t i; + for (i = 0; i < MTYPES; i++) { + printf("%llu ?< %llu\n", s->nmin[i], bp->c[rtype][i]); + if (s->nmin[i] < bp->c[rtype][i]) + return 0; + } + return 1; } -size_t try_opt(uint64_t* score, uint8_t* opt, size_t nopt, const blueprint* bp) -{ - size_t i,j; - state s = start_state(); - for(i = 0; i < nopt; i++) { - switch(opt[i]) { - case DONOTHING: - for(j = 0; j < MTYPES; j++) s.nmin[j] += s.nrob[j]; - break; - case BUILDORE: - if(affordable(ORE, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[ORE][j]; - } - s.nrob[ORE]++; - break; - case BUILDCLA: - if(affordable(CLA, bp, &s) == 0) { - return i; - } - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[CLA][j]; - } - s.nrob[CLA]++; - break; - case BUILDOBS: - if(affordable(OBS, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[OBS][j]; - } - s.nrob[OBS]++; - break; - case BUILDGEO: - if(affordable(GEO, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[GEO][j]; - } - s.nrob[GEO]++; - break; - default: - printopt(opt, nopt); - assert(1 == 2); - } - } - - *score = s.nmin[GEO]; -// printf("*score = %llu\n", *score); - return i; +size_t try_opt(uint64_t *score, uint8_t *opt, size_t nopt, + const blueprint *bp) { + size_t i, j; + state s = start_state(); + for (i = 0; i < nopt; i++) { + switch (opt[i]) { + case DONOTHING: + for (j = 0; j < MTYPES; j++) + s.nmin[j] += s.nrob[j]; + break; + case BUILDORE: + if (affordable(ORE, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[ORE][j]; + } + s.nrob[ORE]++; + break; + case BUILDCLA: + if (affordable(CLA, bp, &s) == 0) { + return i; + } + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[CLA][j]; + } + s.nrob[CLA]++; + break; + case BUILDOBS: + if (affordable(OBS, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[OBS][j]; + } + s.nrob[OBS]++; + break; + case BUILDGEO: + if (affordable(GEO, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[GEO][j]; + } + s.nrob[GEO]++; + break; + default: + printopt(opt, nopt); + assert(1 == 2); + } + } + + *score = s.nmin[GEO]; + // printf("*score = %llu\n", *score); + return i; } -static inline int increment(uint8_t* opt, size_t nopt) -{ - int i = nopt-1; - while(i >= 0 && opt[i] == NOPT-1) { - opt[i] = 0; - i--; - } +static inline int increment(uint8_t *opt, size_t nopt) { + int i = nopt - 1; + while (i >= 0 && opt[i] == NOPT - 1) { + opt[i] = 0; + i--; + } - if(i < 0) { - return -1; - } + if (i < 0) { + return -1; + } - opt[i]++; - return(0); + opt[i]++; + return (0); } -uint64_t wouldbescore(uint8_t* opt, size_t nopt) -{ - uint64_t r = 0; - uint64_t i; - for(i = 0; i < nopt; i++) { - if(opt[i] == BUILDGEO) { - r += (nopt-1-i); - } - } - return r; +uint64_t wouldbescore(uint8_t *opt, size_t nopt) { + uint64_t r = 0; + uint64_t i; + for (i = 0; i < nopt; i++) { + if (opt[i] == BUILDGEO) { + r += (nopt - 1 - i); + } + } + return r; } -static inline int decrement(uint8_t* opt, size_t nopt, size_t i, uint64_t max) -{ - do { - int j = i; - if(j == nopt) j--; - - while(j >= 0 && opt[j] == 0) j--; - if(j < 0) return -1; - - opt[j]--; - memset(&opt[j+1], NOPT-1, nopt-j-1); - } while(wouldbescore(opt, nopt) <= max); - - return 0; +static inline int decrement(uint8_t *opt, size_t nopt, size_t i, uint64_t max) { + do { + int j = i; + if (j == nopt) + j--; + + while (j >= 0 && opt[j] == 0) + j--; + if (j < 0) + return -1; + + opt[j]--; + memset(&opt[j + 1], NOPT - 1, nopt - j - 1); + } while (wouldbescore(opt, nopt) <= max); + + return 0; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - blueprint* bps = parse(lines, nlines); + blueprint *bps = parse(lines, nlines); size_t i; - for(i = 0; i < nlines; i++) { + for (i = 0; i < nlines; i++) { printf("--- %zu ---\n", i); print_blueprint(&bps[i]); } - uint64_t tot = 0; - - for(i = 0; i < nlines; i++) { - uint8_t opt[24]; - memset(opt, NOPT-1, 24); - size_t sk = 0; - uint64_t max = 0; - uint64_t tmp = 0; - do { - // printopt(opt, 24); - sk = try_opt(&tmp, opt, 24, &bps[i]); - if(tmp > max) { - max = tmp; - } - // printf("sk = %zu\n", sk); - // if(sk < 13) { - // printopt(opt, 24); - // printf("max = %llu\n", max); - // printf("sk = %zu\n", sk); - // } - // for(i = sk+1; i < 24; i++) opt[i] = NOPT-1; - } while(decrement(opt, 24, sk, max) != -1); - - printf("(%zu) max = %llu\n", i, max); - tot += (i+1)*max; - } - - printf("tot = %llu\n", tot); + uint64_t tot = 0; + + for (i = 0; i < nlines; i++) { + uint8_t opt[24]; + memset(opt, NOPT - 1, 24); + size_t sk = 0; + uint64_t max = 0; + uint64_t tmp = 0; + do { + // printopt(opt, 24); + sk = try_opt(&tmp, opt, 24, &bps[i]); + if (tmp > max) { + max = tmp; + } + // printf("sk = %zu\n", sk); + // if(sk < 13) { + // printopt(opt, 24); + // printf("max = %llu\n", max); + // printf("sk = %zu\n", sk); + // } + // for(i = sk+1; i < 24; i++) opt[i] = NOPT-1; + } while (decrement(opt, 24, sk, max) != -1); + + printf("(%zu) max = %llu\n", i, max); + tot += (i + 1) * max; + } + + printf("tot = %llu\n", tot); } diff --git a/day19/uppgb.c b/day19/uppgb.c @@ -2,244 +2,258 @@ #define PRECALCULATE_LEVELS 8 -#define MAX(x,y) (x<y?y:x) - -static inline void printopt(uint8_t* opt, size_t nopt) -{ - size_t i; - for(i = 0; i < nopt; i++) { - printf("%hhu ", opt[i]); - } - printf("\n"); -} +#define MAX(x, y) (x < y ? y : x) -static inline int affordable(size_t rtype, const blueprint* bp, state* s) -{ - size_t i; - for(i = 0; i < MTYPES; i++) { - if(s->nmin[i] < bp->c[rtype][i]) return 0; - } - return 1; +static inline void printopt(uint8_t *opt, size_t nopt) { + size_t i; + for (i = 0; i < nopt; i++) { + printf("%hhu ", opt[i]); + } + printf("\n"); } -size_t try_opt(uint64_t* score, uint8_t* opt, size_t nopt, const blueprint* bp, uint64_t* pc, size_t npc) -{ - size_t i,j; - state s = start_state(); - for(i = 0; i < nopt-PRECALCULATE_LEVELS; i++) { - switch(opt[i]) { - case DONOTHING: - for(j = 0; j < MTYPES; j++) s.nmin[j] += s.nrob[j]; - break; - case BUILDORE: - if(affordable(ORE, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[ORE][j]; - } - s.nrob[ORE]++; - break; - case BUILDCLA: - if(affordable(CLA, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[CLA][j]; - } - s.nrob[CLA]++; - break; - case BUILDOBS: - if(affordable(OBS, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[OBS][j]; - } - s.nrob[OBS]++; - break; - case BUILDGEO: - if(affordable(GEO, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[GEO][j]; - } - s.nrob[GEO]++; - break; - default: - printopt(opt, nopt); - assert(1 == 2); - } - } - - uint64_t lmax = 0; - for(i = 0; i < npc; i++) { - if(pc[i*(MTYPES+1) + 0] <= s.nmin[0] && pc[i*(MTYPES+1) + 1] <= s.nmin[1] && pc[i*(MTYPES+1) + 2] <= s.nmin[2]) { - lmax = MAX(lmax, pc[i*(MTYPES+1) + 4] + s.nrob[GEO]*PRECALCULATE_LEVELS); - } - } - - *score = lmax; -// printf("*score = %llu\n", *score); - return nopt-PRECALCULATE_LEVELS-1; +static inline int affordable(size_t rtype, const blueprint *bp, state *s) { + size_t i; + for (i = 0; i < MTYPES; i++) { + if (s->nmin[i] < bp->c[rtype][i]) + return 0; + } + return 1; } -size_t try_min(uint8_t* opt, size_t nopt, const blueprint* bp, size_t type, uint64_t v) -{ - size_t i,j; - state s = empty_state(); - for(i = 0; i < MTYPES; i++) s.nmin[i] = 10000000000L; - s.nmin[type] = v; - - for(i = 0; i < nopt; i++) { - switch(opt[i]) { - case DONOTHING: - for(j = 0; j < MTYPES; j++) s.nmin[j] += s.nrob[j]; - break; - case BUILDORE: - if(affordable(ORE, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[ORE][j]; - } - s.nrob[ORE]++; - break; - case BUILDCLA: - if(affordable(CLA, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[CLA][j]; - } - s.nrob[CLA]++; - break; - case BUILDOBS: - if(affordable(OBS, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[OBS][j]; - } - s.nrob[OBS]++; - break; - case BUILDGEO: - if(affordable(GEO, bp, &s) == 0) return i; - for(j = 0; j < MTYPES; j++) { - s.nmin[j] += s.nrob[j]; - s.nmin[j] -= bp->c[GEO][j]; - } - s.nrob[GEO]++; - break; - default: - printopt(opt, nopt); - assert(1 == 2); - } - } - - return i; -} +size_t try_opt(uint64_t *score, uint8_t *opt, size_t nopt, const blueprint *bp, + uint64_t *pc, size_t npc) { + size_t i, j; + state s = start_state(); + for (i = 0; i < nopt - PRECALCULATE_LEVELS; i++) { + switch (opt[i]) { + case DONOTHING: + for (j = 0; j < MTYPES; j++) + s.nmin[j] += s.nrob[j]; + break; + case BUILDORE: + if (affordable(ORE, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[ORE][j]; + } + s.nrob[ORE]++; + break; + case BUILDCLA: + if (affordable(CLA, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[CLA][j]; + } + s.nrob[CLA]++; + break; + case BUILDOBS: + if (affordable(OBS, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[OBS][j]; + } + s.nrob[OBS]++; + break; + case BUILDGEO: + if (affordable(GEO, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[GEO][j]; + } + s.nrob[GEO]++; + break; + default: + printopt(opt, nopt); + assert(1 == 2); + } + } + uint64_t lmax = 0; + for (i = 0; i < npc; i++) { + if (pc[i * (MTYPES + 1) + 0] <= s.nmin[0] && + pc[i * (MTYPES + 1) + 1] <= s.nmin[1] && + pc[i * (MTYPES + 1) + 2] <= s.nmin[2]) { + lmax = MAX(lmax, + pc[i * (MTYPES + 1) + 4] + s.nrob[GEO] * PRECALCULATE_LEVELS); + } + } -uint64_t minimum_needed(uint8_t* opt, size_t nopt, const blueprint* bp, size_t type) -{ - uint64_t nd = 0; - while(try_min(opt, nopt, bp, type, nd) < nopt) { - nd++; - } - return nd; + *score = lmax; + // printf("*score = %llu\n", *score); + return nopt - PRECALCULATE_LEVELS - 1; } -void precalculate(uint64_t* pc, uint8_t* opt, size_t nopt, const blueprint* bp) -{ - size_t i,j; +size_t try_min(uint8_t *opt, size_t nopt, const blueprint *bp, size_t type, + uint64_t v) { + size_t i, j; + state s = empty_state(); + for (i = 0; i < MTYPES; i++) + s.nmin[i] = 10000000000L; + s.nmin[type] = v; - for(i = 0; i < MTYPES; i++) { - pc[i] = minimum_needed(opt, nopt, bp, i); - } + for (i = 0; i < nopt; i++) { + switch (opt[i]) { + case DONOTHING: + for (j = 0; j < MTYPES; j++) + s.nmin[j] += s.nrob[j]; + break; + case BUILDORE: + if (affordable(ORE, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[ORE][j]; + } + s.nrob[ORE]++; + break; + case BUILDCLA: + if (affordable(CLA, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[CLA][j]; + } + s.nrob[CLA]++; + break; + case BUILDOBS: + if (affordable(OBS, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[OBS][j]; + } + s.nrob[OBS]++; + break; + case BUILDGEO: + if (affordable(GEO, bp, &s) == 0) + return i; + for (j = 0; j < MTYPES; j++) { + s.nmin[j] += s.nrob[j]; + s.nmin[j] -= bp->c[GEO][j]; + } + s.nrob[GEO]++; + break; + default: + printopt(opt, nopt); + assert(1 == 2); + } + } + + return i; +} - pc[MTYPES] = 0; - for(i = 0; i < nopt; i++) { - if(opt[i] == BUILDGEO) { - pc[MTYPES] += nopt-1-i; - } - } +uint64_t minimum_needed(uint8_t *opt, size_t nopt, const blueprint *bp, + size_t type) { + uint64_t nd = 0; + while (try_min(opt, nopt, bp, type, nd) < nopt) { + nd++; + } + return nd; } +void precalculate(uint64_t *pc, uint8_t *opt, size_t nopt, + const blueprint *bp) { + size_t i, j; + + for (i = 0; i < MTYPES; i++) { + pc[i] = minimum_needed(opt, nopt, bp, i); + } + + pc[MTYPES] = 0; + for (i = 0; i < nopt; i++) { + if (opt[i] == BUILDGEO) { + pc[MTYPES] += nopt - 1 - i; + } + } +} -uint64_t wouldbescore(uint8_t* opt, size_t nopt) -{ - uint64_t r = 0; - uint64_t i; - for(i = 0; i < nopt; i++) { - if(opt[i] == BUILDGEO) { - r += (nopt-1-i); - } - } - return r; +uint64_t wouldbescore(uint8_t *opt, size_t nopt) { + uint64_t r = 0; + uint64_t i; + for (i = 0; i < nopt; i++) { + if (opt[i] == BUILDGEO) { + r += (nopt - 1 - i); + } + } + return r; } -static inline int decrement(uint8_t* opt, size_t nopt, size_t i, uint64_t max) -{ - do { - int j = i; - if(j == nopt) j--; - - while(j >= 0 && opt[j] == 0) j--; - if(j < 0) return -1; - - opt[j]--; - memset(&opt[j+1], NOPT-1, nopt-j-1); - } while(wouldbescore(opt, nopt) <= max); - - return 0; +static inline int decrement(uint8_t *opt, size_t nopt, size_t i, uint64_t max) { + do { + int j = i; + if (j == nopt) + j--; + + while (j >= 0 && opt[j] == 0) + j--; + if (j < 0) + return -1; + + opt[j]--; + memset(&opt[j + 1], NOPT - 1, nopt - j - 1); + } while (wouldbescore(opt, nopt) <= max); + + return 0; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - blueprint* bps = parse(lines, nlines); + blueprint *bps = parse(lines, nlines); - size_t i,j; - for(i = 0; i < 3; i++) { + size_t i, j; + for (i = 0; i < 3; i++) { printf("--- %zu ---\n", i); print_blueprint(&bps[i]); } - uint64_t tot = 1; + uint64_t tot = 1; size_t precalculate_size = 1; - for(i = 0; i < PRECALCULATE_LEVELS; i++) precalculate_size *= NOPT; - printf("precalculate size = %zu\n", precalculate_size); - uint64_t pc[precalculate_size*(MTYPES+1)]; - for(i = 0; i < precalculate_size; i++) { - uint64_t ti = i; - uint8_t opt[PRECALCULATE_LEVELS]; - printf("precalculate\n"); - for(j = 0; j < PRECALCULATE_LEVELS; j++) { - opt[j] = ti % NOPT; - ti /= NOPT; - } - printopt(opt, PRECALCULATE_LEVELS); - precalculate(&pc[i*(MTYPES+1)], opt, PRECALCULATE_LEVELS, &bps[0]); - printf(">> %llu %llu %llu %llu %llu\n", pc[i*(MTYPES+1)], pc[i*(MTYPES+1)+1], pc[i*(MTYPES+1)+2], pc[i*(MTYPES+1)+3], pc[i*(MTYPES+1)+4]); - } - - for(i = 0; i < 3; i++) { - uint8_t opt[32]; - memset(opt, NOPT-1, 32); - size_t sk = 0; - uint64_t max = 0; - uint64_t tmp = 0; - do { -// printopt(opt, 24); - sk = try_opt(&tmp, opt, 32, &bps[i], pc, precalculate_size); - if(tmp > max) { - max = tmp; - printf("new max = %llu\n", max); - } - } while(decrement(opt, 32, sk, max) != -1); - - printf("(%zu) max = %llu\n", i, max); - tot *= max; - } - - printf("tot = %llu\n", tot); + for (i = 0; i < PRECALCULATE_LEVELS; i++) + precalculate_size *= NOPT; + printf("precalculate size = %zu\n", precalculate_size); + uint64_t pc[precalculate_size * (MTYPES + 1)]; + for (i = 0; i < precalculate_size; i++) { + uint64_t ti = i; + uint8_t opt[PRECALCULATE_LEVELS]; + printf("precalculate\n"); + for (j = 0; j < PRECALCULATE_LEVELS; j++) { + opt[j] = ti % NOPT; + ti /= NOPT; + } + printopt(opt, PRECALCULATE_LEVELS); + precalculate(&pc[i * (MTYPES + 1)], opt, PRECALCULATE_LEVELS, &bps[0]); + printf(">> %llu %llu %llu %llu %llu\n", pc[i * (MTYPES + 1)], + pc[i * (MTYPES + 1) + 1], pc[i * (MTYPES + 1) + 2], + pc[i * (MTYPES + 1) + 3], pc[i * (MTYPES + 1) + 4]); + } + + for (i = 0; i < 3; i++) { + uint8_t opt[32]; + memset(opt, NOPT - 1, 32); + size_t sk = 0; + uint64_t max = 0; + uint64_t tmp = 0; + do { + // printopt(opt, 24); + sk = try_opt(&tmp, opt, 32, &bps[i], pc, precalculate_size); + if (tmp > max) { + max = tmp; + printf("new max = %llu\n", max); + } + } while (decrement(opt, 32, sk, max) != -1); + + printf("(%zu) max = %llu\n", i, max); + tot *= max; + } + + printf("tot = %llu\n", tot); } diff --git a/day2/uppga.c b/day2/uppga.c @@ -1,25 +1,21 @@ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> -#include <string.h> +#include <stdint.h> +#include <stdio.h> #include <stdlib.h> +#include <string.h> static uint64_t nscore[256]; -static inline uint64_t line_score(char* line) -{ - char* strs[9] = {"A X\n", "A Y\n", "A Z\n", - "B X\n", "B Y\n", "B Z\n", - "C X\n", "C Y\n", "C Z\n"}; - uint64_t scores[9] = {3, 6, 0, - 0, 3, 6, - 6, 0, 3}; +static inline uint64_t line_score(char *line) { + char *strs[9] = {"A X\n", "A Y\n", "A Z\n", "B X\n", "B Y\n", + "B Z\n", "C X\n", "C Y\n", "C Z\n"}; + uint64_t scores[9] = {3, 6, 0, 0, 3, 6, 6, 0, 3}; size_t i; - for(i = 0; i < 9; i++) { - if(strncmp(strs[i], line, 3) == 0) - return nscore[(int) line[2]] + scores[i]; + for (i = 0; i < 9; i++) { + if (strncmp(strs[i], line, 3) == 0) + return nscore[(int)line[2]] + scores[i]; } // unreachable @@ -27,17 +23,18 @@ static inline uint64_t line_score(char* line) exit(1); } -int main(int argc, char** argv) -{ - char** lines; - size_t nlines = readlines(&lines, "input"); +int main(int argc, char **argv) { + char **lines; + size_t nlines = readlines(&lines, "input"); - nscore['X'] = 1; nscore['Y'] = 2; nscore['Z'] = 3; + nscore['X'] = 1; + nscore['Y'] = 2; + nscore['Z'] = 3; - uint64_t score = 0; - size_t i; - for(i = 0; i < nlines; i++) { - score += line_score(lines[i]); - } - printf("score: %llu\n", score); + uint64_t score = 0; + size_t i; + for (i = 0; i < nlines; i++) { + score += line_score(lines[i]); + } + printf("score: %llu\n", score); } diff --git a/day2/uppgb.c b/day2/uppgb.c @@ -1,13 +1,12 @@ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> #include <string.h> -static inline uint64_t choose(char opp, char ldw) -{ - int oppi = (int) (opp - 'A'); - int ldwi = (int) (ldw - 'X'); +static inline uint64_t choose(char opp, char ldw) { + int oppi = (int)(opp - 'A'); + int ldwi = (int)(ldw - 'X'); uint64_t sc[9] = {3, 1, 2, 1, 2, 3, 2, 3, 1}; assert(oppi < 3); @@ -15,23 +14,21 @@ static inline uint64_t choose(char opp, char ldw) assert(ldwi < 3); assert(0 <= ldwi); - return sc[oppi + 3*ldwi] + (3*ldwi); + return sc[oppi + 3 * ldwi] + (3 * ldwi); } -static inline uint64_t line_score(char* line) -{ +static inline uint64_t line_score(char *line) { return choose(line[0], line[2]); } -int main(int argc, char** argv) -{ - char** lines; - size_t nlines = readlines(&lines, "input"); +int main(int argc, char **argv) { + char **lines; + size_t nlines = readlines(&lines, "input"); - uint64_t score = 0; - size_t i; - for(i = 0; i < nlines; i++) { - score += line_score(lines[i]); - } - printf("score: %llu\n", score); + uint64_t score = 0; + size_t i; + for (i = 0; i < nlines; i++) { + score += line_score(lines[i]); + } + printf("score: %llu\n", score); } diff --git a/day20/common.c b/day20/common.c @@ -1,14 +1,13 @@ #include "common.h" -void parse(int64_t* d, char** lines, size_t nlines) -{ +void parse(int64_t *d, char **lines, size_t nlines) { uint64_t tmp; int64_t tmpl; size_t i; - for(i = 0; i < nlines; i++) { + for (i = 0; i < nlines; i++) { sread_next_u64(&tmp, lines[i]); - tmpl = (int64_t) tmp; - if(lines[i][0] == '-') { + tmpl = (int64_t)tmp; + if (lines[i][0] == '-') { tmpl = -tmpl; } d[i] = tmpl; diff --git a/day20/common.h b/day20/common.h @@ -1,11 +1,11 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> -void parse(int64_t* v, char** lines, size_t nlines); +void parse(int64_t *v, char **lines, size_t nlines); #endif diff --git a/day20/uppga.c b/day20/uppga.c @@ -2,94 +2,88 @@ #define NINDICES 3 -void printv(const uint16_t* v, const size_t n) -{ - size_t i; - for(i = 0; i < n; i++) { - printf("\t%u", v[i]); - } +void printv(const uint16_t *v, const size_t n) { + size_t i; + for (i = 0; i < n; i++) { + printf("\t%u", v[i]); + } } -static inline int64_t mod(int64_t a, int64_t b) -{ - int64_t r = a % b; - return r < 0 ? r + b : r; +static inline int64_t mod(int64_t a, int64_t b) { + int64_t r = a % b; + return r < 0 ? r + b : r; } -void compute_next(uint16_t* nxt, const uint16_t* prev, - const int64_t* d, const size_t i, const size_t n) -{ - assert(i < n); - - if(d[i] == 0) { - memcpy(nxt, prev, n * sizeof(*prev)); - } else { - int64_t ti = mod(((int64_t) prev[i]) + d[i], (int64_t) (n-1)); - int64_t tip = ti == 0 ? n-1 : ti; - - size_t j; - for(j = 0; j < n; j++) { - if(j == i) { - nxt[j] = tip; - } else if(tip <= prev[j] && prev[j] < prev[i]) { - nxt[j] = prev[j] + 1; - } else if(prev[i] < prev[j] && prev[j] <= tip) { - assert(prev[j] > 0); - nxt[j] = prev[j] - 1; - } else { - nxt[j] = prev[j]; - } - } - } +void compute_next(uint16_t *nxt, const uint16_t *prev, const int64_t *d, + const size_t i, const size_t n) { + assert(i < n); + + if (d[i] == 0) { + memcpy(nxt, prev, n * sizeof(*prev)); + } else { + int64_t ti = mod(((int64_t)prev[i]) + d[i], (int64_t)(n - 1)); + int64_t tip = ti == 0 ? n - 1 : ti; + + size_t j; + for (j = 0; j < n; j++) { + if (j == i) { + nxt[j] = tip; + } else if (tip <= prev[j] && prev[j] < prev[i]) { + nxt[j] = prev[j] + 1; + } else if (prev[i] < prev[j] && prev[j] <= tip) { + assert(prev[j] > 0); + nxt[j] = prev[j] - 1; + } else { + nxt[j] = prev[j]; + } + } + } } -size_t index_of(const uint16_t value, const uint16_t* v, const size_t n) -{ - size_t i; - for(i = 0; i < n; i++) { - if(v[i] == value) return i; - } - return SIZE_MAX; +size_t index_of(const uint16_t value, const uint16_t *v, const size_t n) { + size_t i; + for (i = 0; i < n; i++) { + if (v[i] == value) + return i; + } + return SIZE_MAX; } -static inline void compute_first(uint16_t* out, const int64_t* d, - const size_t n) -{ - size_t i = 0; - while(d[i] != 0) { - i++; - } - - size_t j; - for(j = 0; j < n; j++) { - out[(i + j) % n] = j; - } +static inline void compute_first(uint16_t *out, const int64_t *d, + const size_t n) { + size_t i = 0; + while (d[i] != 0) { + i++; + } + + size_t j; + for (j = 0; j < n; j++) { + out[(i + j) % n] = j; + } } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t n = readlines(&lines, "input"); printf("#lines: %lu\n", n); int64_t d[n]; parse(d, lines, n); - uint16_t v[2*n]; - compute_first(v, d, n); + uint16_t v[2 * n]; + compute_first(v, d, n); - size_t i,j; - for(i = 0; i < n; i++) { - compute_next(&v[((i+1) % 2)*n], &v[(i % 2)*n], d, i, n); - } + size_t i, j; + for (i = 0; i < n; i++) { + compute_next(&v[((i + 1) % 2) * n], &v[(i % 2) * n], d, i, n); + } - size_t ind[3] = {1000 % n, 2000 % n, 3000 % n}; + size_t ind[3] = {1000 % n, 2000 % n, 3000 % n}; - int64_t sum = 0; - for(j = 0; j < NINDICES; j++) { - sum += d[index_of(ind[j], &v[(i % 2)*n], n)]; - } - - printf("%lld\n", sum); + int64_t sum = 0; + for (j = 0; j < NINDICES; j++) { + sum += d[index_of(ind[j], &v[(i % 2) * n], n)]; + } + printf("%lld\n", sum); } diff --git a/day20/uppgb.c b/day20/uppgb.c @@ -2,99 +2,93 @@ #define NINDICES 3 -void printv(const uint16_t* v, const size_t n) -{ - size_t i; - for(i = 0; i < n; i++) { - printf("\t%u", v[i]); - } +void printv(const uint16_t *v, const size_t n) { + size_t i; + for (i = 0; i < n; i++) { + printf("\t%u", v[i]); + } } -static inline int64_t mod(int64_t a, int64_t b) -{ - int64_t r = a % b; - return r < 0 ? r + b : r; +static inline int64_t mod(int64_t a, int64_t b) { + int64_t r = a % b; + return r < 0 ? r + b : r; } -void compute_next(uint16_t* nxt, const uint16_t* prev, - const int64_t* d, const size_t i, const size_t n) -{ - assert(i < n); - - if(d[i] == 0) { - memcpy(nxt, prev, n * sizeof(*prev)); - } else { - int64_t ti = mod(((int64_t) prev[i]) + d[i], (int64_t) (n-1)); - int64_t tip = ti == 0 ? n-1 : ti; - - size_t j; - for(j = 0; j < n; j++) { - if(j == i) { - nxt[j] = tip; - } else if(tip <= prev[j] && prev[j] < prev[i]) { - nxt[j] = prev[j] + 1; - } else if(prev[i] < prev[j] && prev[j] <= tip) { - assert(prev[j] > 0); - nxt[j] = prev[j] - 1; - } else { - nxt[j] = prev[j]; - } - } - } +void compute_next(uint16_t *nxt, const uint16_t *prev, const int64_t *d, + const size_t i, const size_t n) { + assert(i < n); + + if (d[i] == 0) { + memcpy(nxt, prev, n * sizeof(*prev)); + } else { + int64_t ti = mod(((int64_t)prev[i]) + d[i], (int64_t)(n - 1)); + int64_t tip = ti == 0 ? n - 1 : ti; + + size_t j; + for (j = 0; j < n; j++) { + if (j == i) { + nxt[j] = tip; + } else if (tip <= prev[j] && prev[j] < prev[i]) { + nxt[j] = prev[j] + 1; + } else if (prev[i] < prev[j] && prev[j] <= tip) { + assert(prev[j] > 0); + nxt[j] = prev[j] - 1; + } else { + nxt[j] = prev[j]; + } + } + } } -size_t index_of(const uint16_t value, const uint16_t* v, const size_t n) -{ - size_t i; - for(i = 0; i < n; i++) { - if(v[i] == value) return i; - } - return SIZE_MAX; +size_t index_of(const uint16_t value, const uint16_t *v, const size_t n) { + size_t i; + for (i = 0; i < n; i++) { + if (v[i] == value) + return i; + } + return SIZE_MAX; } -static inline void compute_first(uint16_t* out, const int64_t* d, - const size_t n) -{ - size_t i = 0; - while(d[i] != 0) { - i++; - } - - size_t j; - for(j = 0; j < n; j++) { - out[(i + j) % n] = j; - } +static inline void compute_first(uint16_t *out, const int64_t *d, + const size_t n) { + size_t i = 0; + while (d[i] != 0) { + i++; + } + + size_t j; + for (j = 0; j < n; j++) { + out[(i + j) % n] = j; + } } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t n = readlines(&lines, "input"); printf("#lines: %lu\n", n); int64_t d[n]; parse(d, lines, n); - size_t i,j; + size_t i, j; - for(i = 0; i < n; i++) { - d[i] *= 811589153; - } + for (i = 0; i < n; i++) { + d[i] *= 811589153; + } - uint16_t v[2*n]; - compute_first(v, d, n); + uint16_t v[2 * n]; + compute_first(v, d, n); - for(i = 0; i < 10*n; i++) { - compute_next(&v[((i+1) % 2)*n], &v[(i % 2)*n], d, i % n, n); - } + for (i = 0; i < 10 * n; i++) { + compute_next(&v[((i + 1) % 2) * n], &v[(i % 2) * n], d, i % n, n); + } - size_t ind[3] = {1000 % n, 2000 % n, 3000 % n}; + size_t ind[3] = {1000 % n, 2000 % n, 3000 % n}; - int64_t sum = 0; - for(j = 0; j < NINDICES; j++) { - sum += d[index_of(ind[j], &v[(i % 2)*n], n)]; - } - - printf("%lld\n", sum); + int64_t sum = 0; + for (j = 0; j < NINDICES; j++) { + sum += d[index_of(ind[j], &v[(i % 2) * n], n)]; + } + printf("%lld\n", sum); } diff --git a/day21/bbrute.c b/day21/bbrute.c @@ -1,29 +1,29 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - monki* m = parse(lines, nlines); + monki *m = parse(lines, nlines); int64_t brute = 1; - int64_t v1 = 1, v2 = 0; - m[monkindex("humn")].op = '\0'; - do { - m[monkindex("humn")].v = brute; + int64_t v1 = 1, v2 = 0; + m[monkindex("humn")].op = '\0'; + do { + m[monkindex("humn")].v = brute; v1 = mcompute(monkindex("zhfp"), m); - v2 = mcompute(monkindex("hghd"), m); - if(brute > 0) { - if(brute % 10000000 == 0) printf("> %lld\n", brute); - brute = -brute; - } else { - brute = (-brute) + 1; - } - } while(v1 != v2); + v2 = mcompute(monkindex("hghd"), m); + if (brute > 0) { + if (brute % 10000000 == 0) + printf("> %lld\n", brute); + brute = -brute; + } else { + brute = (-brute) + 1; + } + } while (v1 != v2); - printf("%lld\n", brute); + printf("%lld\n", brute); free(m); } diff --git a/day21/common.c b/day21/common.c @@ -1,128 +1,124 @@ #include "common.h" -#define ALPHABET_SIZE ('z'-'a'+1) - -size_t monkindex(char* name) -{ - size_t r = 0; - - size_t i; - for(i = 0; i < 4; i++) { - r *= ALPHABET_SIZE; - r += (size_t) (name[i] - 'a'); - } - - return r; +#define ALPHABET_SIZE ('z' - 'a' + 1) + +size_t monkindex(char *name) { + size_t r = 0; + + size_t i; + for (i = 0; i < 4; i++) { + r *= ALPHABET_SIZE; + r += (size_t)(name[i] - 'a'); + } + + return r; } -monki* parse(char** lines, size_t nlines) -{ - monki* m = malloc(ALPHABET_SIZE * ALPHABET_SIZE * ALPHABET_SIZE * ALPHABET_SIZE * sizeof(*m)); - - size_t i; - for(i = 0; i < nlines; i++) { - char* line = lines[i]; - size_t idx = monkindex(line); - uint64_t tmp; - if(sread_next_u64(&tmp, line) != NULL) { - m[idx].v = (int64_t) tmp; - m[idx].op = '\0'; - m[idx].opl = SIZE_MAX; - m[idx].opl = SIZE_MAX; - } else { - m[idx].v = 0; - m[idx].op = line[11]; - m[idx].opl = monkindex(&line[6]); - m[idx].opr = monkindex(&line[13]); - } - } - - return m; +monki *parse(char **lines, size_t nlines) { + monki *m = malloc(ALPHABET_SIZE * ALPHABET_SIZE * ALPHABET_SIZE * + ALPHABET_SIZE * sizeof(*m)); + + size_t i; + for (i = 0; i < nlines; i++) { + char *line = lines[i]; + size_t idx = monkindex(line); + uint64_t tmp; + if (sread_next_u64(&tmp, line) != NULL) { + m[idx].v = (int64_t)tmp; + m[idx].op = '\0'; + m[idx].opl = SIZE_MAX; + m[idx].opl = SIZE_MAX; + } else { + m[idx].v = 0; + m[idx].op = line[11]; + m[idx].opl = monkindex(&line[6]); + m[idx].opr = monkindex(&line[13]); + } + } + + return m; } -size_t left_tree_root(const monki* m) -{ - size_t ri = monkindex("root"); - return m[ri].opl; +size_t left_tree_root(const monki *m) { + size_t ri = monkindex("root"); + return m[ri].opl; } -size_t right_tree_root(const monki* m) -{ - size_t ri = monkindex("root"); - return m[ri].opr; +size_t right_tree_root(const monki *m) { + size_t ri = monkindex("root"); + return m[ri].opr; } -int humn_subtree(const size_t start, const monki* m) -{ - if(start == monkindex("humn")) - return 1; - if(m[start].op == '\0') - return 0; - return humn_subtree(m[start].opl, m) || humn_subtree(m[start].opr, m); +int humn_subtree(const size_t start, const monki *m) { + if (start == monkindex("humn")) + return 1; + if (m[start].op == '\0') + return 0; + return humn_subtree(m[start].opl, m) || humn_subtree(m[start].opr, m); } -int64_t mcompute(const size_t start, const monki* m) -{ - if(m[start].op == '\0') { - return m[start].v; - } else { - int64_t opera = mcompute(m[start].opl, m); - int64_t operb = mcompute(m[start].opr, m); - switch(m[start].op) { - case '+': - return opera + operb; - case '-': - return opera - operb; - case '*': - return opera * operb; - case '/': - return opera / operb; - default: - fprintf(stderr, "ERROR! Bad operation %c\n", m[start].op); - exit(1); - } - } +int64_t mcompute(const size_t start, const monki *m) { + if (m[start].op == '\0') { + return m[start].v; + } else { + int64_t opera = mcompute(m[start].opl, m); + int64_t operb = mcompute(m[start].opr, m); + switch (m[start].op) { + case '+': + return opera + operb; + case '-': + return opera - operb; + case '*': + return opera * operb; + case '/': + return opera / operb; + default: + fprintf(stderr, "ERROR! Bad operation %c\n", m[start].op); + exit(1); + } + } } -int64_t reverse(const size_t start, const monki* m, const int64_t desired) -{ - if(start == monkindex("humn")) return desired; - - if(m[start].op == '\0') { - fprintf(stderr, "ERROR! Reverse searching in leaf %zu\n", start); - exit(1); - } - - size_t h_subtree = m[start].opl; - size_t n_subtree = m[start].opr; - int humn_left = 1; - if(!humn_subtree(h_subtree, m)) { - h_subtree = m[start].opr; - n_subtree = m[start].opl; - humn_left = 0; - } - - int64_t opv = mcompute(n_subtree, m); - - switch(m[start].op) { - case '+': - return reverse(h_subtree, m, desired - opv); - case '*': - return reverse(h_subtree, m, desired / opv); - case '/': - if(humn_left) { - return reverse(h_subtree, m, desired * opv); - } - return reverse(h_subtree, m, opv / desired); - case '-': - if(humn_left) { - return reverse(h_subtree, m, desired + opv); - } - return reverse(h_subtree, m, opv - desired); - default: - fprintf(stderr, "ERROR! Reversing with unknown operation %c\n", m[start].op); - exit(1); - } - - return INT64_MAX; +int64_t reverse(const size_t start, const monki *m, const int64_t desired) { + if (start == monkindex("humn")) + return desired; + + if (m[start].op == '\0') { + fprintf(stderr, "ERROR! Reverse searching in leaf %zu\n", start); + exit(1); + } + + size_t h_subtree = m[start].opl; + size_t n_subtree = m[start].opr; + int humn_left = 1; + if (!humn_subtree(h_subtree, m)) { + h_subtree = m[start].opr; + n_subtree = m[start].opl; + humn_left = 0; + } + + int64_t opv = mcompute(n_subtree, m); + + switch (m[start].op) { + case '+': + return reverse(h_subtree, m, desired - opv); + case '*': + return reverse(h_subtree, m, desired / opv); + case '/': + if (humn_left) { + return reverse(h_subtree, m, desired * opv); + } + return reverse(h_subtree, m, opv / desired); + case '-': + if (humn_left) { + return reverse(h_subtree, m, desired + opv); + } + return reverse(h_subtree, m, opv - desired); + default: + fprintf(stderr, "ERROR! Reversing with unknown operation %c\n", + m[start].op); + exit(1); + } + + return INT64_MAX; } diff --git a/day21/common.h b/day21/common.h @@ -1,26 +1,26 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> -#define ALPHABET_SIZE ('z'-'a'+1) +#define ALPHABET_SIZE ('z' - 'a' + 1) typedef struct { - int64_t v; - char op; - size_t opl; - size_t opr; + int64_t v; + char op; + size_t opl; + size_t opr; } monki; -size_t monkindex(char* name); -size_t left_tree_root(const monki* m); -size_t right_tree_root(const monki* m); -monki* parse(char** lines, size_t nlines); -int64_t mcompute(const size_t start, const monki* m); -int humn_subtree(const size_t start, const monki* m); -int64_t reverse(const size_t start, const monki* m, const int64_t desired); +size_t monkindex(char *name); +size_t left_tree_root(const monki *m); +size_t right_tree_root(const monki *m); +monki *parse(char **lines, size_t nlines); +int64_t mcompute(const size_t start, const monki *m); +int humn_subtree(const size_t start, const monki *m); +int64_t reverse(const size_t start, const monki *m, const int64_t desired); #endif diff --git a/day21/uppga.c b/day21/uppga.c @@ -1,15 +1,14 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - monki* m = parse(lines, nlines); + monki *m = parse(lines, nlines); int64_t v = mcompute(monkindex("root"), m); - printf("%lld\n", v); + printf("%lld\n", v); free(m); } diff --git a/day21/uppgb.c b/day21/uppgb.c @@ -1,32 +1,31 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - monki* m = parse(lines, nlines); + monki *m = parse(lines, nlines); size_t l_root = left_tree_root(m); - size_t r_root = right_tree_root(m); - printf("l_root = %zu, r_root = %zu\n", l_root, r_root); + size_t r_root = right_tree_root(m); + printf("l_root = %zu, r_root = %zu\n", l_root, r_root); - size_t h_root = l_root; - size_t n_root = r_root; - if(humn_subtree(l_root, m)) { - printf("> humn in left\n"); - } else { - printf("> humn in right\n"); - h_root = r_root; - n_root = l_root; - } + size_t h_root = l_root; + size_t n_root = r_root; + if (humn_subtree(l_root, m)) { + printf("> humn in left\n"); + } else { + printf("> humn in right\n"); + h_root = r_root; + n_root = l_root; + } - int64_t match = mcompute(n_root, m); - printf("> want to match %lld\n", match); + int64_t match = mcompute(n_root, m); + printf("> want to match %lld\n", match); int64_t revd = reverse(h_root, m, match); - printf("%lld\n", revd); + printf("%lld\n", revd); free(m); } diff --git a/day22/common.c b/day22/common.c @@ -1,433 +1,432 @@ #include "common.h" -static inline size_t get_maxwidth(const char** lines, const size_t nlines) -{ - size_t i; - size_t max = 0; - for(i = 0; i < nlines-2; i++) { - size_t ll = strlen(lines[i])-1; - if(max < ll) max = ll; - } - return max; +static inline size_t get_maxwidth(const char **lines, const size_t nlines) { + size_t i; + size_t max = 0; + for (i = 0; i < nlines - 2; i++) { + size_t ll = strlen(lines[i]) - 1; + if (max < ll) + max = ll; + } + return max; } -board new_board(const size_t h, const size_t w) -{ - board b; - b.h = h; - b.w = w; - b.data = calloc(h * w, sizeof(*b.data)); +board new_board(const size_t h, const size_t w) { + board b; + b.h = h; + b.w = w; + b.data = calloc(h * w, sizeof(*b.data)); - return b; + return b; } -static inline void set_board(board* b, const char** lines, const size_t n) -{ - size_t i,j; - for(i = 0; i < n; i++) { - size_t ll = strlen(lines[i])-1; - for(j = 0; j < ll; j++) { - if(lines[i][j] == '.') { - b->data[i * b->w + j] = FRE; - } else if(lines[i][j] == '#') { - b->data[i * b->w + j] = STP; - } - } - } +static inline void set_board(board *b, const char **lines, const size_t n) { + size_t i, j; + for (i = 0; i < n; i++) { + size_t ll = strlen(lines[i]) - 1; + for (j = 0; j < ll; j++) { + if (lines[i][j] == '.') { + b->data[i * b->w + j] = FRE; + } else if (lines[i][j] == '#') { + b->data[i * b->w + j] = STP; + } + } + } } -static inline pos initial_pos(const board* b) -{ - pos r; - r.dir = EAST; - r.y = 0; +static inline pos initial_pos(const board *b) { + pos r; + r.dir = EAST; + r.y = 0; - size_t i; - for(i = 0; i < b->w; i++) { - if(b->data[i] == FRE) { - r.x = i; - return r; - } - } + size_t i; + for (i = 0; i < b->w; i++) { + if (b->data[i] == FRE) { + r.x = i; + return r; + } + } - fprintf(stderr, "ERROR! No initial positon found\n"); - exit(1); - return r; + fprintf(stderr, "ERROR! No initial positon found\n"); + exit(1); + return r; } -static inline void print_board(const board* b) -{ - size_t i,j; - for(i = 0; i < b->h; i++) { - for(j = 0; j < b->w; j++) { - switch(b->data[i * b->w + j]) { - case NON: - printf(" "); - break; - case STP: - printf("#"); - break; - case FRE: - printf("."); - break; - default: - printf("?"); - } - } - printf("\n"); - } +static inline void print_board(const board *b) { + size_t i, j; + for (i = 0; i < b->h; i++) { + for (j = 0; j < b->w; j++) { + switch (b->data[i * b->w + j]) { + case NON: + printf(" "); + break; + case STP: + printf("#"); + break; + case FRE: + printf("."); + break; + default: + printf("?"); + } + } + printf("\n"); + } } -static inline void print_board_with_pos(const board* b, pos* p) -{ - size_t i,j; - for(i = 0; i < b->h; i++) { - for(j = 0; j < b->w; j++) { - if(p->x != j || p->y != i) { - switch(b->data[i * b->w + j]) { - case NON: - printf(" "); - break; - case STP: - printf("#"); - break; - case FRE: - printf("."); - break; - default: - printf("?"); - } - } else { - switch(p->dir) { - case NORTH: - printf("^"); - break; - case EAST: - printf(">"); - break; - case SOUTH: - printf("v"); - break; - case WEST: - printf("<"); - break; - default: - printf("?"); - } - } - } - printf("\n"); - } +static inline void print_board_with_pos(const board *b, pos *p) { + size_t i, j; + for (i = 0; i < b->h; i++) { + for (j = 0; j < b->w; j++) { + if (p->x != j || p->y != i) { + switch (b->data[i * b->w + j]) { + case NON: + printf(" "); + break; + case STP: + printf("#"); + break; + case FRE: + printf("."); + break; + default: + printf("?"); + } + } else { + switch (p->dir) { + case NORTH: + printf("^"); + break; + case EAST: + printf(">"); + break; + case SOUTH: + printf("v"); + break; + case WEST: + printf("<"); + break; + default: + printf("?"); + } + } + } + printf("\n"); + } } -move* get_moves(size_t* nmoves, const char* line) -{ - size_t ll = strlen(line); - move tmpm[ll]; +move *get_moves(size_t *nmoves, const char *line) { + size_t ll = strlen(line); + move tmpm[ll]; - //x assumes that line starts and ends with numbers + // x assumes that line starts and ends with numbers - size_t i = 0; - uint64_t tmp; - while((line = sread_next_u64(&tmp, (char*) line)) != NULL) { - tmpm[i].n = tmp; - tmpm[i].turn = '\0'; - i++; - if(line != NULL && line[0] != '\n') { - tmpm[i].turn = line[0]; - tmpm[i].n = UINT64_MAX; - line++; - i++; - } - } + size_t i = 0; + uint64_t tmp; + while ((line = sread_next_u64(&tmp, (char *)line)) != NULL) { + tmpm[i].n = tmp; + tmpm[i].turn = '\0'; + i++; + if (line != NULL && line[0] != '\n') { + tmpm[i].turn = line[0]; + tmpm[i].n = UINT64_MAX; + line++; + i++; + } + } - move* r = malloc(i * sizeof(*r)); - memcpy(r, tmpm, i * sizeof(*r)); + move *r = malloc(i * sizeof(*r)); + memcpy(r, tmpm, i * sizeof(*r)); - *nmoves = i; + *nmoves = i; - return r; + return r; } -void make_move(pos* p, const board* b, move m) -{ - if(m.n == UINT64_MAX) { - assert(m.turn == 'R' || m.turn == 'L'); - if(m.turn == 'R') { - p->dir = (p->dir + 1) % NDIR; - } else { - if(p->dir == 0) { - p->dir = NDIR-1; - } else { - p->dir = p->dir - 1; - } - } - } else { - size_t oldx = SIZE_MAX, oldy = SIZE_MAX; - size_t steps = 0; - switch(p->dir) { - case NORTH: - do { - oldy = p->y; - if(p->y > 0 && b->data[(p->y-1) * b->w + p->x] == FRE) { - p->y--; - } else if(p->y == 0 || b->data[(p->y-1) * b->w + p->x] == NON) { - size_t y = b->h-1; - while(b->data[y * b->w + p->x] == NON) { - y--; - } - if(b->data[y * b->w + p->x] == FRE) p->y = y; - } - steps++; - } while(oldy != p->y && steps < m.n); - break; - case EAST: - do { - oldx = p->x; - if(p->x < b->w-1 && b->data[p->y * b->w + p->x + 1] == FRE) { - p->x++; - } else if(p->x == b->w-1 || b->data[p->y * b->w + p->x + 1] == NON) { - size_t x = 0; - while(b->data[p->y * b->w + x] == NON) { - x++; - } - if(b->data[p->y * b->w + x] == FRE) p->x = x; - } - steps++; - } while(oldx != p->x && steps < m.n); - break; - case SOUTH: - do { - oldy = p->y; - if(p->y < b->h-1 && b->data[(p->y+1) * b->w + p->x] == FRE) { - p->y++; - } else if(p->y == b->h-1 || b->data[(p->y+1) * b->w + p->x] == NON) { - size_t y = 0; - while(b->data[y * b->w + p->x] == NON) { - y++; - } - if(b->data[y * b->w + p->x] == FRE) p->y = y; - } - steps++; - } while(oldy != p->y && steps < m.n); - break; - case WEST: - do { - oldx = p->x; - if(p->x > 0 && b->data[p->y * b->w + p->x - 1] == FRE) { - p->x--; - } else if(p->x == 0 || b->data[p->y * b->w + p->x - 1] == NON) { - size_t x = b->w-1; - while(b->data[p->y * b->w + x] == NON) { - x--; - } - if(b->data[p->y * b->w + x] == FRE) p->x = x; - } - steps++; - } while(oldx != p->x && steps < m.n); - break; - default: - fprintf(stderr, "ERROR! Unknown direction encountered\n"); - exit(1); - } - } +void make_move(pos *p, const board *b, move m) { + if (m.n == UINT64_MAX) { + assert(m.turn == 'R' || m.turn == 'L'); + if (m.turn == 'R') { + p->dir = (p->dir + 1) % NDIR; + } else { + if (p->dir == 0) { + p->dir = NDIR - 1; + } else { + p->dir = p->dir - 1; + } + } + } else { + size_t oldx = SIZE_MAX, oldy = SIZE_MAX; + size_t steps = 0; + switch (p->dir) { + case NORTH: + do { + oldy = p->y; + if (p->y > 0 && b->data[(p->y - 1) * b->w + p->x] == FRE) { + p->y--; + } else if (p->y == 0 || b->data[(p->y - 1) * b->w + p->x] == NON) { + size_t y = b->h - 1; + while (b->data[y * b->w + p->x] == NON) { + y--; + } + if (b->data[y * b->w + p->x] == FRE) + p->y = y; + } + steps++; + } while (oldy != p->y && steps < m.n); + break; + case EAST: + do { + oldx = p->x; + if (p->x < b->w - 1 && b->data[p->y * b->w + p->x + 1] == FRE) { + p->x++; + } else if (p->x == b->w - 1 || b->data[p->y * b->w + p->x + 1] == NON) { + size_t x = 0; + while (b->data[p->y * b->w + x] == NON) { + x++; + } + if (b->data[p->y * b->w + x] == FRE) + p->x = x; + } + steps++; + } while (oldx != p->x && steps < m.n); + break; + case SOUTH: + do { + oldy = p->y; + if (p->y < b->h - 1 && b->data[(p->y + 1) * b->w + p->x] == FRE) { + p->y++; + } else if (p->y == b->h - 1 || + b->data[(p->y + 1) * b->w + p->x] == NON) { + size_t y = 0; + while (b->data[y * b->w + p->x] == NON) { + y++; + } + if (b->data[y * b->w + p->x] == FRE) + p->y = y; + } + steps++; + } while (oldy != p->y && steps < m.n); + break; + case WEST: + do { + oldx = p->x; + if (p->x > 0 && b->data[p->y * b->w + p->x - 1] == FRE) { + p->x--; + } else if (p->x == 0 || b->data[p->y * b->w + p->x - 1] == NON) { + size_t x = b->w - 1; + while (b->data[p->y * b->w + x] == NON) { + x--; + } + if (b->data[p->y * b->w + x] == FRE) + p->x = x; + } + steps++; + } while (oldx != p->x && steps < m.n); + break; + default: + fprintf(stderr, "ERROR! Unknown direction encountered\n"); + exit(1); + } + } } -int64_t score_pos(pos* p) -{ - int64_t dirscore; - switch(p->dir) { - case EAST: - dirscore = 0; - break; - case SOUTH: - dirscore = 1; - break; - case WEST: - dirscore = 2; - break; - case NORTH: - dirscore = 3; - break; - default: - fprintf(stderr, "ERROR! Code 404\n"); - exit(1); - } +int64_t score_pos(pos *p) { + int64_t dirscore; + switch (p->dir) { + case EAST: + dirscore = 0; + break; + case SOUTH: + dirscore = 1; + break; + case WEST: + dirscore = 2; + break; + case NORTH: + dirscore = 3; + break; + default: + fprintf(stderr, "ERROR! Code 404\n"); + exit(1); + } - assert(dirscore < 4); - assert(dirscore >= 0); + assert(dirscore < 4); + assert(dirscore >= 0); - int64_t row = (int64_t) p->y+1; - int64_t col = (int64_t) p->x+1; + int64_t row = (int64_t)p->y + 1; + int64_t col = (int64_t)p->x + 1; - return 1000 * row + 4 * col + dirscore; + return 1000 * row + 4 * col + dirscore; } -size_t parse(board* b, pos* p, move** m, const char** lines, const size_t nlines) -{ - size_t maxw = get_maxwidth(lines, nlines); +size_t parse(board *b, pos *p, move **m, const char **lines, + const size_t nlines) { + size_t maxw = get_maxwidth(lines, nlines); - *b = new_board(nlines-2, maxw); - set_board(b, lines, nlines-2); + *b = new_board(nlines - 2, maxw); + set_board(b, lines, nlines - 2); *p = initial_pos(b); - size_t nmoves; - *m = get_moves(&nmoves, lines[nlines-1]); + size_t nmoves; + *m = get_moves(&nmoves, lines[nlines - 1]); - return nmoves; + return nmoves; } -pos map_offboard(int64_t x, int64_t y, pos* p) -{ - pos r; - if(y == -1 && 50 <= x && x <= 99) { /* a,1 */ - r.x = (size_t) 0; - r.y = (size_t) (x + 100); - assert(p->dir == NORTH); - r.dir = EAST; - } else if(x == -1 && 150 <= y && y <= 199) { /* a,2 */ - r.y = (size_t) 0; - r.x = (size_t) (y - 100); - assert(p->dir == WEST); - r.dir = SOUTH; - } else if(x == 49 && 0 <= y && y <= 49) { /* b,1 */ - r.x = (size_t) 0; - r.y = (size_t) (149-y); - assert(p->dir == WEST); - r.dir = EAST; - } else if(x == -1 && 100 <= y && y <= 149) { /* b,2 */ - r.x = (size_t) 50; - r.y = (size_t) (149 - y); - assert(p->dir == WEST); - r.dir = EAST; - } else if(x == 49 && 50 <= y && y <= 99) { /* c,1 */ - r.y = (size_t) 100; - r.x = (size_t) (y - 50); - assert(p->dir == WEST); - r.dir = SOUTH; - } else if(y == 99 && 0 <= x && x <= 49) { /* c,2 */ - r.x = (size_t) 50; - r.y = (size_t) (x + 50); - assert(p->dir == NORTH); - r.dir = EAST; - } else if(y == 150 && 50 <= x && x <= 99) { /* d,1 */ - r.x = (size_t) 49; - r.y = (size_t) (x + 100); - assert(p->dir == SOUTH); - r.dir = WEST; - } else if(x == 50 && 150 <= y && y <= 199) { /* d,2 */ - r.y = (size_t) 149; - r.x = (size_t) (y - 100); - assert(p->dir == EAST); - r.dir = NORTH; - } else if(x == 150 && 0 <= y && y <= 49) { /* e,1 */ - r.x = (size_t) 99; - r.y = (size_t) (149-y); - assert(p->dir == EAST); - r.dir = WEST; - } else if(x == 100 && 100 <= y && y <= 149) { /* e,2 */ - r.x = (size_t) 149; - r.y = (size_t) (149-y); - assert(p->dir == EAST); - r.dir = WEST; - } else if(y == 50 && 100 <= x && x <= 149) { /* f,1 */ - r.x = (size_t) 99; - r.y = (size_t) (x - 50); - assert(p->dir == SOUTH); - r.dir = WEST; - } else if(x == 100 && 50 <= y && y <= 99) { /* f,2 */ - r.y = (size_t) 49; - r.x = (size_t) (y + 50); - assert(p->dir == EAST); - r.dir = NORTH; - } else if(y == -1 && 100 <= x && x <= 149) { /* g,1 */ - r.y = (size_t) 199; - r.x = (size_t) (x - 100); - assert(p->dir == NORTH); - r.dir = NORTH; - } else if(y == 200 && 0 <= x && x <= 49) { /* g,2 */ - r.y = (size_t) 0; - r.x = (size_t) (x + 100); - assert(p->dir == SOUTH); - r.dir = SOUTH; - } else { - fprintf(stderr, "Bad position (%lld, %lld)\n", x, y); - exit(1); - } +pos map_offboard(int64_t x, int64_t y, pos *p) { + pos r; + if (y == -1 && 50 <= x && x <= 99) { /* a,1 */ + r.x = (size_t)0; + r.y = (size_t)(x + 100); + assert(p->dir == NORTH); + r.dir = EAST; + } else if (x == -1 && 150 <= y && y <= 199) { /* a,2 */ + r.y = (size_t)0; + r.x = (size_t)(y - 100); + assert(p->dir == WEST); + r.dir = SOUTH; + } else if (x == 49 && 0 <= y && y <= 49) { /* b,1 */ + r.x = (size_t)0; + r.y = (size_t)(149 - y); + assert(p->dir == WEST); + r.dir = EAST; + } else if (x == -1 && 100 <= y && y <= 149) { /* b,2 */ + r.x = (size_t)50; + r.y = (size_t)(149 - y); + assert(p->dir == WEST); + r.dir = EAST; + } else if (x == 49 && 50 <= y && y <= 99) { /* c,1 */ + r.y = (size_t)100; + r.x = (size_t)(y - 50); + assert(p->dir == WEST); + r.dir = SOUTH; + } else if (y == 99 && 0 <= x && x <= 49) { /* c,2 */ + r.x = (size_t)50; + r.y = (size_t)(x + 50); + assert(p->dir == NORTH); + r.dir = EAST; + } else if (y == 150 && 50 <= x && x <= 99) { /* d,1 */ + r.x = (size_t)49; + r.y = (size_t)(x + 100); + assert(p->dir == SOUTH); + r.dir = WEST; + } else if (x == 50 && 150 <= y && y <= 199) { /* d,2 */ + r.y = (size_t)149; + r.x = (size_t)(y - 100); + assert(p->dir == EAST); + r.dir = NORTH; + } else if (x == 150 && 0 <= y && y <= 49) { /* e,1 */ + r.x = (size_t)99; + r.y = (size_t)(149 - y); + assert(p->dir == EAST); + r.dir = WEST; + } else if (x == 100 && 100 <= y && y <= 149) { /* e,2 */ + r.x = (size_t)149; + r.y = (size_t)(149 - y); + assert(p->dir == EAST); + r.dir = WEST; + } else if (y == 50 && 100 <= x && x <= 149) { /* f,1 */ + r.x = (size_t)99; + r.y = (size_t)(x - 50); + assert(p->dir == SOUTH); + r.dir = WEST; + } else if (x == 100 && 50 <= y && y <= 99) { /* f,2 */ + r.y = (size_t)49; + r.x = (size_t)(y + 50); + assert(p->dir == EAST); + r.dir = NORTH; + } else if (y == -1 && 100 <= x && x <= 149) { /* g,1 */ + r.y = (size_t)199; + r.x = (size_t)(x - 100); + assert(p->dir == NORTH); + r.dir = NORTH; + } else if (y == 200 && 0 <= x && x <= 49) { /* g,2 */ + r.y = (size_t)0; + r.x = (size_t)(x + 100); + assert(p->dir == SOUTH); + r.dir = SOUTH; + } else { + fprintf(stderr, "Bad position (%lld, %lld)\n", x, y); + exit(1); + } - return r; + return r; } -void make_move_b(pos* p, const board* b, move m) -{ - if(m.n == UINT64_MAX) { - assert(m.turn == 'R' || m.turn == 'L'); - if(m.turn == 'R') { - p->dir = (p->dir + 1) % NDIR; - } else { - if(p->dir == 0) { - p->dir = NDIR-1; - } else { - p->dir = p->dir - 1; - } - } - } else { - size_t oldx = SIZE_MAX, oldy = SIZE_MAX; - size_t steps = 0; - do { - oldy = p->y; oldx = p->x; - switch(p->dir) { - case NORTH: - if(p->y > 0 && b->data[(p->y-1) * b->w + p->x] == FRE) { - p->y--; - } else if(p->y == 0 || b->data[(p->y-1) * b->w + p->x] == NON) { - int64_t wbx = (int64_t) p->x; - int64_t wby = ((int64_t) p->y) - 1; - pos tpos = map_offboard(wbx, wby, p); - if(b->data[tpos.y * b->w + tpos.x] == FRE) *p = tpos; - } - steps++; - break; - case EAST: - if(p->x < b->w-1 && b->data[p->y * b->w + p->x + 1] == FRE) { - p->x++; - } else if(p->x == b->w-1 || b->data[p->y * b->w + p->x + 1] == NON) { - int64_t wbx = ((int64_t) p->x) + 1; - int64_t wby = (int64_t) p->y; - pos tpos = map_offboard(wbx, wby, p); - if(b->data[tpos.y * b->w + tpos.x] == FRE) *p = tpos; - } - steps++; - break; - case SOUTH: - if(p->y < b->h-1 && b->data[(p->y+1) * b->w + p->x] == FRE) { - p->y++; - } else if(p->y == b->h-1 || b->data[(p->y+1) * b->w + p->x] == NON) { - int64_t wbx = (int64_t) p->x; - int64_t wby = ((int64_t) p->y) + 1; - pos tpos = map_offboard(wbx, wby, p); - if(b->data[tpos.y * b->w + tpos.x] == FRE) *p = tpos; - } - steps++; - break; - case WEST: - if(p->x > 0 && b->data[p->y * b->w + p->x - 1] == FRE) { - p->x--; - } else if(p->x == 0 || b->data[p->y * b->w + p->x - 1] == NON) { - int64_t wbx = ((int64_t) p->x) - 1; - int64_t wby = (int64_t) p->y; - pos tpos = map_offboard(wbx, wby, p); - if(b->data[tpos.y * b->w + tpos.x] == FRE) *p = tpos; - } - steps++; - break; - default: - fprintf(stderr, "ERROR! Unknown direction encountered\n"); - exit(1); - } - } while((oldy != p->y || oldx != p->x) && steps < m.n); - } +void make_move_b(pos *p, const board *b, move m) { + if (m.n == UINT64_MAX) { + assert(m.turn == 'R' || m.turn == 'L'); + if (m.turn == 'R') { + p->dir = (p->dir + 1) % NDIR; + } else { + if (p->dir == 0) { + p->dir = NDIR - 1; + } else { + p->dir = p->dir - 1; + } + } + } else { + size_t oldx = SIZE_MAX, oldy = SIZE_MAX; + size_t steps = 0; + do { + oldy = p->y; + oldx = p->x; + switch (p->dir) { + case NORTH: + if (p->y > 0 && b->data[(p->y - 1) * b->w + p->x] == FRE) { + p->y--; + } else if (p->y == 0 || b->data[(p->y - 1) * b->w + p->x] == NON) { + int64_t wbx = (int64_t)p->x; + int64_t wby = ((int64_t)p->y) - 1; + pos tpos = map_offboard(wbx, wby, p); + if (b->data[tpos.y * b->w + tpos.x] == FRE) + *p = tpos; + } + steps++; + break; + case EAST: + if (p->x < b->w - 1 && b->data[p->y * b->w + p->x + 1] == FRE) { + p->x++; + } else if (p->x == b->w - 1 || b->data[p->y * b->w + p->x + 1] == NON) { + int64_t wbx = ((int64_t)p->x) + 1; + int64_t wby = (int64_t)p->y; + pos tpos = map_offboard(wbx, wby, p); + if (b->data[tpos.y * b->w + tpos.x] == FRE) + *p = tpos; + } + steps++; + break; + case SOUTH: + if (p->y < b->h - 1 && b->data[(p->y + 1) * b->w + p->x] == FRE) { + p->y++; + } else if (p->y == b->h - 1 || + b->data[(p->y + 1) * b->w + p->x] == NON) { + int64_t wbx = (int64_t)p->x; + int64_t wby = ((int64_t)p->y) + 1; + pos tpos = map_offboard(wbx, wby, p); + if (b->data[tpos.y * b->w + tpos.x] == FRE) + *p = tpos; + } + steps++; + break; + case WEST: + if (p->x > 0 && b->data[p->y * b->w + p->x - 1] == FRE) { + p->x--; + } else if (p->x == 0 || b->data[p->y * b->w + p->x - 1] == NON) { + int64_t wbx = ((int64_t)p->x) - 1; + int64_t wby = (int64_t)p->y; + pos tpos = map_offboard(wbx, wby, p); + if (b->data[tpos.y * b->w + tpos.x] == FRE) + *p = tpos; + } + steps++; + break; + default: + fprintf(stderr, "ERROR! Unknown direction encountered\n"); + exit(1); + } + } while ((oldy != p->y || oldx != p->x) && steps < m.n); + } } - - diff --git a/day22/common.h b/day22/common.h @@ -1,10 +1,10 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> #define FRE 1 #define STP 2 @@ -12,29 +12,30 @@ #define NDIR 4 -enum direction {NORTH, EAST, SOUTH, WEST}; +enum direction { NORTH, EAST, SOUTH, WEST }; typedef struct { - size_t h; - size_t w; - uint8_t* data; + size_t h; + size_t w; + uint8_t *data; } board; typedef struct { - size_t x; - size_t y; + size_t x; + size_t y; - enum direction dir; + enum direction dir; } pos; typedef struct { - uint64_t n; - char turn; + uint64_t n; + char turn; } move; -size_t parse(board* b, pos* p, move** m, const char** lines, const size_t nlines); -int64_t score_pos(pos* p); -void make_move(pos* p, const board* b, move m); -void make_move_b(pos* p, const board* b, move m); +size_t parse(board *b, pos *p, move **m, const char **lines, + const size_t nlines); +int64_t score_pos(pos *p); +void make_move(pos *p, const board *b, move m); +void make_move_b(pos *p, const board *b, move m); #endif diff --git a/day22/uppga.c b/day22/uppga.c @@ -1,20 +1,19 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - board b; - pos p; - move* m; - size_t nmoves = parse(&b, &p, &m, (const char**) lines, nlines); + board b; + pos p; + move *m; + size_t nmoves = parse(&b, &p, &m, (const char **)lines, nlines); - size_t i; - for(i = 0; i < nmoves; i++) { - make_move(&p, &b, m[i]); - } + size_t i; + for (i = 0; i < nmoves; i++) { + make_move(&p, &b, m[i]); + } - printf("%lld\n", score_pos(&p)); + printf("%lld\n", score_pos(&p)); } diff --git a/day22/uppgb.c b/day22/uppgb.c @@ -1,20 +1,19 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - board b; - pos p; - move* m; - size_t nmoves = parse(&b, &p, &m, (const char**) lines, nlines); + board b; + pos p; + move *m; + size_t nmoves = parse(&b, &p, &m, (const char **)lines, nlines); - size_t i; - for(i = 0; i < nmoves; i++) { - make_move_b(&p, &b, m[i]); - } + size_t i; + for (i = 0; i < nmoves; i++) { + make_move_b(&p, &b, m[i]); + } - printf("%lld\n", score_pos(&p)); + printf("%lld\n", score_pos(&p)); } diff --git a/day23/common.c b/day23/common.c @@ -4,248 +4,242 @@ static size_t norder = 4; static char order[4] = "NSWE"; space make_space(const size_t npos) { - space r; - r.h = 2*(npos+200); - r.w = 2*(npos+200); - r.hoffs = npos+100; - r.woffs = npos+100; + space r; + r.h = 2 * (npos + 200); + r.w = 2 * (npos + 200); + r.hoffs = npos + 100; + r.woffs = npos + 100; r.data = malloc(r.h * r.w * sizeof(*r.data)); - return r; + return r; } -void set_space(space s, const int64_t x, const int64_t y, const uint8_t val) -{ - assert((y + ((int64_t) s.hoffs)) >= 0); - assert((y + ((int64_t) s.hoffs)) < s.h); - assert((x + ((int64_t) s.woffs)) >= 0); - assert((x + ((int64_t) s.woffs)) < s.w); - s.data[(y + ((int64_t) s.hoffs)) * s.w + (x + ((int64_t) s.woffs))] = val; +void set_space(space s, const int64_t x, const int64_t y, const uint8_t val) { + assert((y + ((int64_t)s.hoffs)) >= 0); + assert((y + ((int64_t)s.hoffs)) < s.h); + assert((x + ((int64_t)s.woffs)) >= 0); + assert((x + ((int64_t)s.woffs)) < s.w); + s.data[(y + ((int64_t)s.hoffs)) * s.w + (x + ((int64_t)s.woffs))] = val; } -static inline void inc_space(space s, const int64_t x, const int64_t y) -{ - assert((y + ((int64_t) s.hoffs)) >= 0); - assert((y + ((int64_t) s.hoffs)) < s.h); - assert((x + ((int64_t) s.woffs)) >= 0); - assert((x + ((int64_t) s.woffs)) < s.w); - s.data[(y + ((int64_t) s.hoffs)) * s.w + (x + ((int64_t) s.woffs))]++; +static inline void inc_space(space s, const int64_t x, const int64_t y) { + assert((y + ((int64_t)s.hoffs)) >= 0); + assert((y + ((int64_t)s.hoffs)) < s.h); + assert((x + ((int64_t)s.woffs)) >= 0); + assert((x + ((int64_t)s.woffs)) < s.w); + s.data[(y + ((int64_t)s.hoffs)) * s.w + (x + ((int64_t)s.woffs))]++; } -uint8_t get_space(space s, const int64_t x, const int64_t y) -{ - assert((y + ((int64_t) s.hoffs)) >= 0); - assert((y + ((int64_t) s.hoffs)) < s.h); - assert((x + ((int64_t) s.woffs)) >= 0); - assert((x + ((int64_t) s.woffs)) < s.w); - return s.data[(y + ((int64_t) s.hoffs)) * s.w + (x + ((int64_t) s.woffs))]; +uint8_t get_space(space s, const int64_t x, const int64_t y) { + assert((y + ((int64_t)s.hoffs)) >= 0); + assert((y + ((int64_t)s.hoffs)) < s.h); + assert((x + ((int64_t)s.woffs)) >= 0); + assert((x + ((int64_t)s.woffs)) < s.w); + return s.data[(y + ((int64_t)s.hoffs)) * s.w + (x + ((int64_t)s.woffs))]; } -static inline void clear_spaces(space s, const pos* p, const size_t npos) -{ - size_t i; - for(i = 0; i < npos; i++) { - set_space(s, p[i].x-1, p[i].y-1, 0); - set_space(s, p[i].x-1, p[i].y, 0); - set_space(s, p[i].x-1, p[i].y+1, 0); - - set_space(s, p[i].x, p[i].y-1, 0); - set_space(s, p[i].x, p[i].y, 0); - set_space(s, p[i].x, p[i].y+1, 0); - - set_space(s, p[i].x+1, p[i].y-1, 0); - set_space(s, p[i].x+1, p[i].y, 0); - set_space(s, p[i].x+1, p[i].y+1, 0); - } - - for(i = 0; i < npos; i++) { - set_space(s, p[i].x, p[i].y, 128); - } +static inline void clear_spaces(space s, const pos *p, const size_t npos) { + size_t i; + for (i = 0; i < npos; i++) { + set_space(s, p[i].x - 1, p[i].y - 1, 0); + set_space(s, p[i].x - 1, p[i].y, 0); + set_space(s, p[i].x - 1, p[i].y + 1, 0); + + set_space(s, p[i].x, p[i].y - 1, 0); + set_space(s, p[i].x, p[i].y, 0); + set_space(s, p[i].x, p[i].y + 1, 0); + + set_space(s, p[i].x + 1, p[i].y - 1, 0); + set_space(s, p[i].x + 1, p[i].y, 0); + set_space(s, p[i].x + 1, p[i].y + 1, 0); + } + + for (i = 0; i < npos; i++) { + set_space(s, p[i].x, p[i].y, 128); + } } -int none_around(space s, pos p) -{ - assert(get_space(s, p.x, p.y) == 128); - return get_space(s, p.x - 1, p.y - 1) < 128 && - get_space(s, p.x , p.y - 1) < 128 && - get_space(s, p.x + 1, p.y - 1) < 128 && - get_space(s, p.x - 1, p.y ) < 128 && - get_space(s, p.x + 1, p.y ) < 128 && - get_space(s, p.x - 1, p.y + 1) < 128 && - get_space(s, p.x , p.y + 1) < 128 && - get_space(s, p.x + 1, p.y + 1) < 128; +int none_around(space s, pos p) { + assert(get_space(s, p.x, p.y) == 128); + return get_space(s, p.x - 1, p.y - 1) < 128 && + get_space(s, p.x, p.y - 1) < 128 && + get_space(s, p.x + 1, p.y - 1) < 128 && + get_space(s, p.x - 1, p.y) < 128 && get_space(s, p.x + 1, p.y) < 128 && + get_space(s, p.x - 1, p.y + 1) < 128 && + get_space(s, p.x, p.y + 1) < 128 && + get_space(s, p.x + 1, p.y + 1) < 128; } -void propose_moves(space s, char* mvs, const pos* p, const size_t npos, const size_t round) -{ - clear_spaces(s, p, npos); - - size_t i, j; - for(i = 0; i < npos; i++) { - if(none_around(s, p[i])) { - mvs[i] = 'X'; - continue; - } - mvs[i] = 'X'; - for(j = 0; j < norder; j++) { - char dir = order[(j + round) % norder]; - if(dir == 'N') { - if(get_space(s, p[i].x - 1, p[i].y - 1) != 128 && - get_space(s, p[i].x , p[i].y - 1) != 128 && - get_space(s, p[i].x + 1, p[i].y - 1) != 128) { - inc_space(s, p[i].x, p[i].y - 1); - assert(get_space(s, p[i].x, p[i].y - 1) <= 4); - mvs[i] = dir; - break; - } - } else if(dir == 'S') { - if(get_space(s, p[i].x - 1, p[i].y + 1) != 128 && - get_space(s, p[i].x , p[i].y + 1) != 128 && - get_space(s, p[i].x + 1, p[i].y + 1) != 128) { - inc_space(s, p[i].x, p[i].y + 1); - assert(get_space(s, p[i].x, p[i].y + 1) <= 4); - mvs[i] = dir; - break; - } - } else if(dir == 'E') { - if(get_space(s, p[i].x + 1, p[i].y - 1) != 128 && - get_space(s, p[i].x + 1, p[i].y ) != 128 && - get_space(s, p[i].x + 1, p[i].y + 1) != 128) { - inc_space(s, p[i].x + 1, p[i].y); - assert(get_space(s, p[i].x + 1, p[i].y) <= 4); - mvs[i] = dir; - break; - } - } else if(dir == 'W') { - if(get_space(s, p[i].x - 1, p[i].y - 1) != 128 && - get_space(s, p[i].x - 1, p[i].y ) != 128 && - get_space(s, p[i].x - 1, p[i].y + 1) != 128) { - inc_space(s, p[i].x - 1, p[i].y); - assert(get_space(s, p[i].x - 1, p[i].y) <= 4); - mvs[i] = dir; - break; - } - } else { - fprintf(stderr, "ERROR! Unknown dir %c\n", dir); - exit(1); - } - } - assert(mvs[i] == 'N' || mvs[i] == 'S' || mvs[i] == 'E' || mvs[i] == 'W' || mvs[i] == 'X'); - } +void propose_moves(space s, char *mvs, const pos *p, const size_t npos, + const size_t round) { + clear_spaces(s, p, npos); + + size_t i, j; + for (i = 0; i < npos; i++) { + if (none_around(s, p[i])) { + mvs[i] = 'X'; + continue; + } + mvs[i] = 'X'; + for (j = 0; j < norder; j++) { + char dir = order[(j + round) % norder]; + if (dir == 'N') { + if (get_space(s, p[i].x - 1, p[i].y - 1) != 128 && + get_space(s, p[i].x, p[i].y - 1) != 128 && + get_space(s, p[i].x + 1, p[i].y - 1) != 128) { + inc_space(s, p[i].x, p[i].y - 1); + assert(get_space(s, p[i].x, p[i].y - 1) <= 4); + mvs[i] = dir; + break; + } + } else if (dir == 'S') { + if (get_space(s, p[i].x - 1, p[i].y + 1) != 128 && + get_space(s, p[i].x, p[i].y + 1) != 128 && + get_space(s, p[i].x + 1, p[i].y + 1) != 128) { + inc_space(s, p[i].x, p[i].y + 1); + assert(get_space(s, p[i].x, p[i].y + 1) <= 4); + mvs[i] = dir; + break; + } + } else if (dir == 'E') { + if (get_space(s, p[i].x + 1, p[i].y - 1) != 128 && + get_space(s, p[i].x + 1, p[i].y) != 128 && + get_space(s, p[i].x + 1, p[i].y + 1) != 128) { + inc_space(s, p[i].x + 1, p[i].y); + assert(get_space(s, p[i].x + 1, p[i].y) <= 4); + mvs[i] = dir; + break; + } + } else if (dir == 'W') { + if (get_space(s, p[i].x - 1, p[i].y - 1) != 128 && + get_space(s, p[i].x - 1, p[i].y) != 128 && + get_space(s, p[i].x - 1, p[i].y + 1) != 128) { + inc_space(s, p[i].x - 1, p[i].y); + assert(get_space(s, p[i].x - 1, p[i].y) <= 4); + mvs[i] = dir; + break; + } + } else { + fprintf(stderr, "ERROR! Unknown dir %c\n", dir); + exit(1); + } + } + assert(mvs[i] == 'N' || mvs[i] == 'S' || mvs[i] == 'E' || mvs[i] == 'W' || + mvs[i] == 'X'); + } } -int make_moves(pos* p, const space s, const char* mvs, const size_t npos) -{ - int moved = 0; - size_t i; - for(i = 0; i < npos; i++) { - if(mvs[i] != 'X') { - switch(mvs[i]) { - case 'N': - if(get_space(s, p[i].x, p[i].y - 1) == 1) { - p[i].y--; - moved = 1; - } - break; - case 'S': - if(get_space(s, p[i].x, p[i].y + 1) == 1) { - p[i].y++; - moved = 1; - } - break; - case 'E': - if(get_space(s, p[i].x + 1, p[i].y) == 1) { - p[i].x++; - moved = 1; - } - break; - case 'W': - if(get_space(s, p[i].x - 1, p[i].y) == 1) { - p[i].x--; - moved = 1; - } - break; - default: - fprintf(stderr, "ERROR! Unknown dir (in make_moves) %c\n", mvs[i]); - exit(1); - } - } - } - - return moved; +int make_moves(pos *p, const space s, const char *mvs, const size_t npos) { + int moved = 0; + size_t i; + for (i = 0; i < npos; i++) { + if (mvs[i] != 'X') { + switch (mvs[i]) { + case 'N': + if (get_space(s, p[i].x, p[i].y - 1) == 1) { + p[i].y--; + moved = 1; + } + break; + case 'S': + if (get_space(s, p[i].x, p[i].y + 1) == 1) { + p[i].y++; + moved = 1; + } + break; + case 'E': + if (get_space(s, p[i].x + 1, p[i].y) == 1) { + p[i].x++; + moved = 1; + } + break; + case 'W': + if (get_space(s, p[i].x - 1, p[i].y) == 1) { + p[i].x--; + moved = 1; + } + break; + default: + fprintf(stderr, "ERROR! Unknown dir (in make_moves) %c\n", mvs[i]); + exit(1); + } + } + } + + return moved; } -void print_board(const space s, const pos* p, const size_t npos) -{ - size_t i, j, k; - for(i = 0; i < s.h; i++) { - for(j = 0; j < s.w; j++) { - int64_t y = (int64_t) i - s.hoffs; - int64_t x = (int64_t) j - s.woffs; - int occup = 0; - for(k = 0; k < npos; k++) { - if(x == p[k].x && y == p[k].y) { - occup = k+1; - break; - } - } - - if(occup) { - printf("#"); - } else { - printf("."); - } - } - printf("\n"); - } +void print_board(const space s, const pos *p, const size_t npos) { + size_t i, j, k; + for (i = 0; i < s.h; i++) { + for (j = 0; j < s.w; j++) { + int64_t y = (int64_t)i - s.hoffs; + int64_t x = (int64_t)j - s.woffs; + int occup = 0; + for (k = 0; k < npos; k++) { + if (x == p[k].x && y == p[k].y) { + occup = k + 1; + break; + } + } + + if (occup) { + printf("#"); + } else { + printf("."); + } + } + printf("\n"); + } } -static inline void getminmax(int64_t* vals, const pos* p, const size_t npos) -{ - size_t i; - vals[0] = INT64_MAX; - vals[1] = INT64_MIN; - vals[2] = INT64_MAX; - vals[3] = INT64_MIN; - for(i = 0; i < npos; i++) { - if(p[i].x < vals[0]) vals[0] = p[i].x; - if(p[i].x > vals[1]) vals[1] = p[i].x; - if(p[i].y < vals[2]) vals[2] = p[i].y; - if(p[i].y > vals[3]) vals[3] = p[i].y; - } +static inline void getminmax(int64_t *vals, const pos *p, const size_t npos) { + size_t i; + vals[0] = INT64_MAX; + vals[1] = INT64_MIN; + vals[2] = INT64_MAX; + vals[3] = INT64_MIN; + for (i = 0; i < npos; i++) { + if (p[i].x < vals[0]) + vals[0] = p[i].x; + if (p[i].x > vals[1]) + vals[1] = p[i].x; + if (p[i].y < vals[2]) + vals[2] = p[i].y; + if (p[i].y > vals[3]) + vals[3] = p[i].y; + } } -int64_t count_empty(const pos* p, const size_t npos) -{ - int64_t bounds[4]; - getminmax(bounds, p, npos); +int64_t count_empty(const pos *p, const size_t npos) { + int64_t bounds[4]; + getminmax(bounds, p, npos); - return (bounds[1] - bounds[0] + 1) * (bounds[3] - bounds[2] + 1) - - ((int64_t) npos); + return (bounds[1] - bounds[0] + 1) * (bounds[3] - bounds[2] + 1) - + ((int64_t)npos); } -pos* parse(size_t* npos, char** lines, size_t nlines) -{ +pos *parse(size_t *npos, char **lines, size_t nlines) { size_t i, j; - size_t cols = strlen(lines[0])-1; - - size_t ntmp = 0; - pos tmp[nlines * cols]; - - for(i = 0; i < nlines; i++) { - for(j = 0; j < cols; j++) { - if(lines[i][j] == '#') { - tmp[ntmp].x = (int64_t) j; - tmp[ntmp].y = (int64_t) i; - ntmp++; - } - } - } - - pos* r = malloc(ntmp * sizeof(*r)); - memcpy(r, tmp, ntmp * sizeof(*r)); - *npos = ntmp; - - return r; + size_t cols = strlen(lines[0]) - 1; + + size_t ntmp = 0; + pos tmp[nlines * cols]; + + for (i = 0; i < nlines; i++) { + for (j = 0; j < cols; j++) { + if (lines[i][j] == '#') { + tmp[ntmp].x = (int64_t)j; + tmp[ntmp].y = (int64_t)i; + ntmp++; + } + } + } + + pos *r = malloc(ntmp * sizeof(*r)); + memcpy(r, tmp, ntmp * sizeof(*r)); + *npos = ntmp; + + return r; } diff --git a/day23/common.h b/day23/common.h @@ -1,31 +1,32 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> typedef struct { - int64_t x; - int64_t y; + int64_t x; + int64_t y; } pos; typedef struct { - size_t h; - size_t w; + size_t h; + size_t w; - int64_t hoffs; - int64_t woffs; + int64_t hoffs; + int64_t woffs; - uint8_t* data; + uint8_t *data; } space; -pos* parse(size_t* npos, char** lines, size_t nlines); -void print_board(const space s, const pos* p, const size_t npos); +pos *parse(size_t *npos, char **lines, size_t nlines); +void print_board(const space s, const pos *p, const size_t npos); space make_space(const size_t npos); -void propose_moves(space s, char* mvs, const pos* p, const size_t npos, const size_t round); -int make_moves(pos* p, const space s, const char* mvs, const size_t npos); -int64_t count_empty(const pos* p, const size_t npos); +void propose_moves(space s, char *mvs, const pos *p, const size_t npos, + const size_t round); +int make_moves(pos *p, const space s, const char *mvs, const size_t npos); +int64_t count_empty(const pos *p, const size_t npos); #endif diff --git a/day23/uppga.c b/day23/uppga.c @@ -1,24 +1,23 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - size_t npos; - pos* p = parse(&npos, lines, nlines); + size_t npos; + pos *p = parse(&npos, lines, nlines); - space s = make_space(npos); + space s = make_space(npos); - size_t round = 0; - int moved = 1; - char mvs[npos]; - while(moved && round < 10) { - propose_moves(s, mvs, p, npos, round); - moved = make_moves(p, s, mvs, npos); - round++; - } + size_t round = 0; + int moved = 1; + char mvs[npos]; + while (moved && round < 10) { + propose_moves(s, mvs, p, npos, round); + moved = make_moves(p, s, mvs, npos); + round++; + } - int64_t empty = count_empty(p, npos); + int64_t empty = count_empty(p, npos); printf("%lld\n", empty); } diff --git a/day23/uppgb.c b/day23/uppgb.c @@ -1,23 +1,22 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - size_t npos; - pos* p = parse(&npos, lines, nlines); + size_t npos; + pos *p = parse(&npos, lines, nlines); - space s = make_space(npos); + space s = make_space(npos); - size_t round = 0; - int moved = 1; - char mvs[npos]; - while(moved) { - propose_moves(s, mvs, p, npos, round); - moved = make_moves(p, s, mvs, npos); - round++; - } + size_t round = 0; + int moved = 1; + char mvs[npos]; + while (moved) { + propose_moves(s, mvs, p, npos, round); + moved = make_moves(p, s, mvs, npos); + round++; + } printf("%zu\n", round); } diff --git a/day24/common.c b/day24/common.c @@ -1,406 +1,409 @@ #include "common.h" -static inline size_t ceil_div(const size_t x, const size_t y) -{ +static inline size_t ceil_div(const size_t x, const size_t y) { assert(y > 0); - return (x + y - 1)/y; + return (x + y - 1) / y; } -static inline size_t bitvector_len(const bitvector* b) -{ +static inline size_t bitvector_len(const bitvector *b) { return ceil_div(b->n, 8 * sizeof(*b->v)); } -static inline size_t bitvector_remainder_bits(const bitvector* b) -{ - size_t remainder = b->n % (8 * sizeof(*b->v)); +static inline size_t bitvector_remainder_bits(const bitvector *b) { + size_t remainder = b->n % (8 * sizeof(*b->v)); return remainder == 0 ? (8 * sizeof(*b->v)) : remainder; } -void bitvector_init(bitvector* b, const size_t n) -{ +void bitvector_init(bitvector *b, const size_t n) { b->n = n; b->v = calloc(bitvector_len(b), sizeof(*b->v)); } -void bitvector_clear(bitvector* b) -{ +void bitvector_clear(bitvector *b) { free(b->v); b->n = 0; } -static inline void rotshift_r(bitvector* out, const bitvector* in) -{ +static inline void rotshift_r(bitvector *out, const bitvector *in) { assert(out != in); - assert(out->n = in->n); + assert(out->n = in->n); size_t i, l = bitvector_len(in); uint64_t remainder = 0; assert(l > 0); - for(i = 0; i < l-1; i++) { + for (i = 0; i < l - 1; i++) { out->v[i] = remainder ? HEADBIT_U64 | (in->v[i] >> 1) : in->v[i] >> 1; remainder = in->v[i] & 0x01; } - - const uint64_t last_remainder = in->v[l-1] & (HEADBIT_U64 >> (bitvector_remainder_bits(in) - 1)); - out->v[l-1] = remainder ? HEADBIT_U64 | (in->v[l-1] >> 1) : in->v[i] >> 1; - if(last_remainder) out->v[0] |= HEADBIT_U64; + + const uint64_t last_remainder = + in->v[l - 1] & (HEADBIT_U64 >> (bitvector_remainder_bits(in) - 1)); + out->v[l - 1] = remainder ? HEADBIT_U64 | (in->v[l - 1] >> 1) : in->v[i] >> 1; + if (last_remainder) + out->v[0] |= HEADBIT_U64; } -void rotshift_l(bitvector* out, const bitvector* in) { - size_t i, l = bitvector_len(in); - for(i = 0; i < l-1; i++) { - out->v[i] = (in->v[i] << 1); - if((HEADBIT_U64 & in->v[i+1]) != 0) { - out->v[i]++; - } - } - - out->v[l-1] = (in->v[i] << 1); - if((HEADBIT_U64 & in->v[0]) != 0) { - out->v[l-1] |= (HEADBIT_U64 >> (bitvector_remainder_bits(in) - 1)); - } +void rotshift_l(bitvector *out, const bitvector *in) { + size_t i, l = bitvector_len(in); + for (i = 0; i < l - 1; i++) { + out->v[i] = (in->v[i] << 1); + if ((HEADBIT_U64 & in->v[i + 1]) != 0) { + out->v[i]++; + } + } + + out->v[l - 1] = (in->v[i] << 1); + if ((HEADBIT_U64 & in->v[0]) != 0) { + out->v[l - 1] |= (HEADBIT_U64 >> (bitvector_remainder_bits(in) - 1)); + } } -static inline void bitvector_set(bitvector* b, const size_t n, const uint64_t v) -{ - assert(v == 0 || v == 1); - assert(n < b->n); +static inline void bitvector_set(bitvector *b, const size_t n, + const uint64_t v) { + assert(v == 0 || v == 1); + assert(n < b->n); - const size_t i = n / (8 * sizeof(*b->v)); - const size_t m = n % (8 * sizeof(*b->v)); + const size_t i = n / (8 * sizeof(*b->v)); + const size_t m = n % (8 * sizeof(*b->v)); - if(v == 0) { - b->v[i] &= (UINT64_MAX ^ (HEADBIT_U64 >> m)); - } else { - b->v[i] |= (HEADBIT_U64 >> m); - } + if (v == 0) { + b->v[i] &= (UINT64_MAX ^ (HEADBIT_U64 >> m)); + } else { + b->v[i] |= (HEADBIT_U64 >> m); + } } -static inline int bitvector_get(const bitvector* b, const size_t n) -{ - assert(n < b->n); +static inline int bitvector_get(const bitvector *b, const size_t n) { + assert(n < b->n); - const size_t i = n / (8 * sizeof(*b->v)); - const size_t m = n % (8 * sizeof(*b->v)); + const size_t i = n / (8 * sizeof(*b->v)); + const size_t m = n % (8 * sizeof(*b->v)); - if((b->v[i] & (HEADBIT_U64 >> m)) != 0) { - return 1; - } - return 0; + if ((b->v[i] & (HEADBIT_U64 >> m)) != 0) { + return 1; + } + return 0; } -static inline void bitvector_or(bitvector* out, const bitvector* a, const bitvector* b) -{ - size_t l = bitvector_len(a); - assert(l == bitvector_len(b)); - assert(l == bitvector_len(out)); +static inline void bitvector_or(bitvector *out, const bitvector *a, + const bitvector *b) { + size_t l = bitvector_len(a); + assert(l == bitvector_len(b)); + assert(l == bitvector_len(out)); - size_t i; - for(i = 0; i < l; i++) { - out->v[i] = a->v[i] | b->v[i]; - } + size_t i; + for (i = 0; i < l; i++) { + out->v[i] = a->v[i] | b->v[i]; + } } -void bitvector_print(const bitvector* b) -{ - size_t i, j, l = bitvector_len(b); - - for(i = 0; i < l-1; i++) { - for(j = 0; j < 64; j++) { - if((b->v[i] & (HEADBIT_U64 >> j)) != 0) { - printf("1"); - } else { - printf("0"); - } - } - } - - const size_t remainder = bitvector_remainder_bits(b); - for(j = 0; j < remainder; j++) { - if((b->v[l-1] & (HEADBIT_U64 >> j)) != 0) { - printf("1"); - } else { - printf("0"); - } - } +void bitvector_print(const bitvector *b) { + size_t i, j, l = bitvector_len(b); + + for (i = 0; i < l - 1; i++) { + for (j = 0; j < 64; j++) { + if ((b->v[i] & (HEADBIT_U64 >> j)) != 0) { + printf("1"); + } else { + printf("0"); + } + } + } + + const size_t remainder = bitvector_remainder_bits(b); + for (j = 0; j < remainder; j++) { + if ((b->v[l - 1] & (HEADBIT_U64 >> j)) != 0) { + printf("1"); + } else { + printf("0"); + } + } } -static inline void parse_row(board* out, const size_t row, const char** lines, const size_t nlines) -{ - assert(row < nlines - 2); - const char* line = lines[row + 1]; - - bitvector first_rs, first_ls; - bitvector_init(&first_rs, out->w); - bitvector_init(&first_ls, out->w); - - size_t i; - for(i = 1; i < out->w + 1; i++) { - if(line[i] == '>') { - bitvector_set(&first_rs, i-1, 1); - } else if(line[i] == '<') { - bitvector_set(&first_ls, i-1, 1); - } else { - assert(line[i] == '.' || line[i] == '^' || line[i] == 'v'); - } - } - - bitvector trs, tls; - bitvector_init(&trs, out->w); - bitvector_init(&tls, out->w); - - for(i = 0; i < out->w; i += 2) { - bitvector_or(&(out->row_mask[row][i]), &first_rs, &first_ls); - rotshift_r(&trs, &first_rs); - rotshift_l(&tls, &first_ls); - if(i+1 < out->w) { - bitvector_or(&(out->row_mask[row][i+1]), &trs, &tls); - rotshift_r(&first_rs, &trs); - rotshift_l(&first_ls, &tls); - } - } - - bitvector_clear(&trs); - bitvector_clear(&tls); - bitvector_clear(&first_rs); - bitvector_clear(&first_ls); +static inline void parse_row(board *out, const size_t row, const char **lines, + const size_t nlines) { + assert(row < nlines - 2); + const char *line = lines[row + 1]; + + bitvector first_rs, first_ls; + bitvector_init(&first_rs, out->w); + bitvector_init(&first_ls, out->w); + + size_t i; + for (i = 1; i < out->w + 1; i++) { + if (line[i] == '>') { + bitvector_set(&first_rs, i - 1, 1); + } else if (line[i] == '<') { + bitvector_set(&first_ls, i - 1, 1); + } else { + assert(line[i] == '.' || line[i] == '^' || line[i] == 'v'); + } + } + + bitvector trs, tls; + bitvector_init(&trs, out->w); + bitvector_init(&tls, out->w); + + for (i = 0; i < out->w; i += 2) { + bitvector_or(&(out->row_mask[row][i]), &first_rs, &first_ls); + rotshift_r(&trs, &first_rs); + rotshift_l(&tls, &first_ls); + if (i + 1 < out->w) { + bitvector_or(&(out->row_mask[row][i + 1]), &trs, &tls); + rotshift_r(&first_rs, &trs); + rotshift_l(&first_ls, &tls); + } + } + + bitvector_clear(&trs); + bitvector_clear(&tls); + bitvector_clear(&first_rs); + bitvector_clear(&first_ls); } -static inline void parse_col(board* out, const size_t col, const char** lines, const size_t nlines) -{ - assert(col < out->w); - - bitvector first_rs, first_ls; - bitvector_init(&first_rs, out->h); - bitvector_init(&first_ls, out->h); - - size_t i; - for(i = 1; i < out->h + 1; i++) { - if(lines[i][col+1] == 'v') { - bitvector_set(&first_rs, i-1, 1); - } else if(lines[i][col+1] == '^') { - bitvector_set(&first_ls, i-1, 1); - } else { - assert(lines[i][col+1] == '.' || lines[i][col+1] == '>' || lines[i][col+1] == '<'); - } - } - - bitvector trs, tls; - bitvector_init(&trs, out->h); - bitvector_init(&tls, out->h); - - for(i = 0; i < out->h; i += 2) { - bitvector_or(&(out->col_mask[col][i]), &first_rs, &first_ls); - rotshift_r(&trs, &first_rs); - rotshift_l(&tls, &first_ls); - if(i+1 < out->h) { - bitvector_or(&(out->col_mask[col][i+1]), &trs, &tls); - rotshift_r(&first_rs, &trs); - rotshift_l(&first_ls, &tls); - } - } - - bitvector_clear(&trs); - bitvector_clear(&tls); - bitvector_clear(&first_rs); - bitvector_clear(&first_ls); +static inline void parse_col(board *out, const size_t col, const char **lines, + const size_t nlines) { + assert(col < out->w); + + bitvector first_rs, first_ls; + bitvector_init(&first_rs, out->h); + bitvector_init(&first_ls, out->h); + + size_t i; + for (i = 1; i < out->h + 1; i++) { + if (lines[i][col + 1] == 'v') { + bitvector_set(&first_rs, i - 1, 1); + } else if (lines[i][col + 1] == '^') { + bitvector_set(&first_ls, i - 1, 1); + } else { + assert(lines[i][col + 1] == '.' || lines[i][col + 1] == '>' || + lines[i][col + 1] == '<'); + } + } + + bitvector trs, tls; + bitvector_init(&trs, out->h); + bitvector_init(&tls, out->h); + + for (i = 0; i < out->h; i += 2) { + bitvector_or(&(out->col_mask[col][i]), &first_rs, &first_ls); + rotshift_r(&trs, &first_rs); + rotshift_l(&tls, &first_ls); + if (i + 1 < out->h) { + bitvector_or(&(out->col_mask[col][i + 1]), &trs, &tls); + rotshift_r(&first_rs, &trs); + rotshift_l(&first_ls, &tls); + } + } + + bitvector_clear(&trs); + bitvector_clear(&tls); + bitvector_clear(&first_rs); + bitvector_clear(&first_ls); } -int intersects(const board* b, const uint64_t x, const uint64_t y, const size_t r) -{ - assert(y < b->h); - assert(x < b->w); +int intersects(const board *b, const uint64_t x, const uint64_t y, + const size_t r) { + assert(y < b->h); + assert(x < b->w); - return bitvector_get(&(b->row_mask[y][r % b->w]), x) || - bitvector_get(&(b->col_mask[x][r % b->h]), y); + return bitvector_get(&(b->row_mask[y][r % b->w]), x) || + bitvector_get(&(b->col_mask[x][r % b->h]), y); } -board parse(char** lines, size_t nlines) -{ - board b; - b.h = nlines-2; - b.w = strlen(lines[0])-3; - - b.row_mask = malloc(b.h * sizeof(*b.row_mask)); - b.col_mask = malloc(b.w * sizeof(*b.col_mask)); - - size_t i,j; - for(i = 0; i < b.h; i++) { - b.row_mask[i] = malloc(b.w * sizeof(*b.row_mask[0])); - for(j = 0; j < b.w; j++) { - bitvector_init(&b.row_mask[i][j], b.w); - } - } - - for(i = 0; i < b.w; i++) { - b.col_mask[i] = malloc(b.h * sizeof(*b.col_mask[0])); - for(j = 0; j < b.h; j++) { - bitvector_init(&b.col_mask[i][j], b.h); - } - } - - for(i = 0; i < b.h; i++) { - parse_row(&b, i, (const char**) lines, nlines); - } - - for(i = 0; i < b.w; i++) { - parse_col(&b, i, (const char**) lines, nlines); - } - - return b; +board parse(char **lines, size_t nlines) { + board b; + b.h = nlines - 2; + b.w = strlen(lines[0]) - 3; + + b.row_mask = malloc(b.h * sizeof(*b.row_mask)); + b.col_mask = malloc(b.w * sizeof(*b.col_mask)); + + size_t i, j; + for (i = 0; i < b.h; i++) { + b.row_mask[i] = malloc(b.w * sizeof(*b.row_mask[0])); + for (j = 0; j < b.w; j++) { + bitvector_init(&b.row_mask[i][j], b.w); + } + } + + for (i = 0; i < b.w; i++) { + b.col_mask[i] = malloc(b.h * sizeof(*b.col_mask[0])); + for (j = 0; j < b.h; j++) { + bitvector_init(&b.col_mask[i][j], b.h); + } + } + + for (i = 0; i < b.h; i++) { + parse_row(&b, i, (const char **)lines, nlines); + } + + for (i = 0; i < b.w; i++) { + parse_col(&b, i, (const char **)lines, nlines); + } + + return b; } -void bestfinder(board* b, uint64_t* best, uint64_t* bestend, set* s, uint64_t endx, uint64_t endy) -{ - uint64_t i, r, x, y, nr, cb; +void bestfinder(board *b, uint64_t *best, uint64_t *bestend, set *s, + uint64_t endx, uint64_t endy) { + uint64_t i, r, x, y, nr, cb; - while(s->n != 0) { - i = 0; - while(s->d[i] == 0) i++; - s->d[i] = 0; - s->n--; + while (s->n != 0) { + i = 0; + while (s->d[i] == 0) + i++; + s->d[i] = 0; + s->n--; x = i % (b->w); - y = (i / b->w) % b->h; - r = i / (b->w * b->h); - nr = (r + 1) % b->lcm; - cb = best[r * (b->w * b->h) + y * b->w + x]; - - if(!intersects(b, x, y, nr) && best[nr * (b->w * b->h) + y * b->w + x] > cb+1) { - best[nr * (b->w * b->h) + y * b->w + x] = cb+1; - if(s->d[nr * (b->w * b->h) + y * b->w + x] == 0) { - s->d[nr * (b->w * b->h) + y * b->w + x] = 1; - s->n++; - } - } - - if(x > 0 && !intersects(b, x-1, y, nr) && best[nr * (b->w * b->h) + y * b->w + (x-1)] > cb+1) { - best[nr * (b->w * b->h) + y * b->w + (x-1)] = cb+1; - if(s->d[nr * (b->w * b->h) + y * b->w + (x-1)] == 0) { - s->d[nr * (b->w * b->h) + y * b->w + (x-1)] = 1; - s->n++; - } - } - - if(x < b->w-1 && !intersects(b, x+1, y, nr) && best[nr * (b->w * b->h) + y * b->w + (x+1)] > cb+1) { - best[nr * (b->w * b->h) + y * b->w + (x+1)] = cb+1; - if(s->d[nr * (b->w * b->h) + y * b->w + (x+1)] == 0) { - s->d[nr * (b->w * b->h) + y * b->w + (x+1)] = 1; - s->n++; - } - } - - if(y > 0 && !intersects(b, x, y-1, nr) && best[nr * (b->w * b->h) + (y-1) * b->w + x] > cb+1) { - best[nr * (b->w * b->h) + (y-1) * b->w + x] = cb+1; - if(s->d[nr * (b->w * b->h) + (y-1) * b->w + x] == 0) { - s->d[nr * (b->w * b->h) + (y-1) * b->w + x] = 1; - s->n++; - } - } - - if(y < b->h-1 && !intersects(b, x, y+1, nr) && best[nr * (b->w * b->h) + (y+1) * b->w + x] > cb+1) { - best[nr * (b->w * b->h) + (y+1) * b->w + x] = cb+1; - if(s->d[nr * (b->w * b->h) + (y+1) * b->w + x] == 0) { - s->d[nr * (b->w * b->h) + (y+1) * b->w + x] = 1; - s->n++; - } - } - - if(y == endy && x == endx) { - size_t j; - for(j = 0; j < b->lcm; j++) { - bestend[(nr + j) % b->lcm] = bestend[(nr + j) % b->lcm] < cb + j ? bestend[(nr + j) % b->lcm] : cb + j; - } - } - } + y = (i / b->w) % b->h; + r = i / (b->w * b->h); + nr = (r + 1) % b->lcm; + cb = best[r * (b->w * b->h) + y * b->w + x]; + + if (!intersects(b, x, y, nr) && + best[nr * (b->w * b->h) + y * b->w + x] > cb + 1) { + best[nr * (b->w * b->h) + y * b->w + x] = cb + 1; + if (s->d[nr * (b->w * b->h) + y * b->w + x] == 0) { + s->d[nr * (b->w * b->h) + y * b->w + x] = 1; + s->n++; + } + } + + if (x > 0 && !intersects(b, x - 1, y, nr) && + best[nr * (b->w * b->h) + y * b->w + (x - 1)] > cb + 1) { + best[nr * (b->w * b->h) + y * b->w + (x - 1)] = cb + 1; + if (s->d[nr * (b->w * b->h) + y * b->w + (x - 1)] == 0) { + s->d[nr * (b->w * b->h) + y * b->w + (x - 1)] = 1; + s->n++; + } + } + + if (x < b->w - 1 && !intersects(b, x + 1, y, nr) && + best[nr * (b->w * b->h) + y * b->w + (x + 1)] > cb + 1) { + best[nr * (b->w * b->h) + y * b->w + (x + 1)] = cb + 1; + if (s->d[nr * (b->w * b->h) + y * b->w + (x + 1)] == 0) { + s->d[nr * (b->w * b->h) + y * b->w + (x + 1)] = 1; + s->n++; + } + } + + if (y > 0 && !intersects(b, x, y - 1, nr) && + best[nr * (b->w * b->h) + (y - 1) * b->w + x] > cb + 1) { + best[nr * (b->w * b->h) + (y - 1) * b->w + x] = cb + 1; + if (s->d[nr * (b->w * b->h) + (y - 1) * b->w + x] == 0) { + s->d[nr * (b->w * b->h) + (y - 1) * b->w + x] = 1; + s->n++; + } + } + + if (y < b->h - 1 && !intersects(b, x, y + 1, nr) && + best[nr * (b->w * b->h) + (y + 1) * b->w + x] > cb + 1) { + best[nr * (b->w * b->h) + (y + 1) * b->w + x] = cb + 1; + if (s->d[nr * (b->w * b->h) + (y + 1) * b->w + x] == 0) { + s->d[nr * (b->w * b->h) + (y + 1) * b->w + x] = 1; + s->n++; + } + } + + if (y == endy && x == endx) { + size_t j; + for (j = 0; j < b->lcm; j++) { + bestend[(nr + j) % b->lcm] = bestend[(nr + j) % b->lcm] < cb + j + ? bestend[(nr + j) % b->lcm] + : cb + j; + } + } + } } -void play(char** lines, size_t nlines) -{ - board b = parse(lines, nlines); +void play(char **lines, size_t nlines) { + board b = parse(lines, nlines); b.lcm = 700; - //b.lcm = 12; - - set s; - s.n = 0; - s.nalloc = b.lcm * b.h * b.w; - s.d = calloc(s.nalloc, sizeof(*s.d)); - - uint64_t* best = malloc(s.nalloc * sizeof(*best)); - memset(best, 0xff, s.nalloc * sizeof(*best)); - - uint64_t i, r, x, y, nr, cb; - uint64_t bestend[b.lcm]; - - for(i = 0; i < b.lcm; i++) { - bestend[i] = UINT64_MAX; - if(!intersects(&b, 0, 0, i+1)) { - best[i * (b.w * b.h)] = i+1; - s.d[i * (b.w * b.h)] = 1; - s.n++; - } - } - - bestfinder(&b, best, bestend, &s, b.w-1, b.h-1); - - uint64_t pb[b.lcm]; - - uint64_t min = UINT64_MAX; - for(i = 0; i < b.lcm; i++) { - printf("best[%llu, %zu, %zu] = %llu\n", i, b.w - 1, b.h - 1, best[i * (b.w * b.h) + ((b.h - 1) * b.w) + (b.w - 1)]); - printf("bestend[%llu] = %llu\n", i, bestend[i]); - if(bestend[i] < min) { - min = bestend[i]; - } - } - printf("min = %llu\n", min); - - memset(best, 0xff, s.nalloc * sizeof(*best)); - for(i = 0; i < b.lcm; i++) { - uint64_t t = bestend[i]; - bestend[i] = UINT64_MAX; - if(!intersects(&b, b.w-1, b.h-1, i+1)) { - best[i * (b.w * b.h) + ((b.h-1) * b.w) + (b.w - 1)] = t+1; - s.d[i * (b.w * b.h) + ((b.h-1) * b.w) + (b.w - 1)] = 1; - s.n++; - } - } - - bestfinder(&b, best, bestend, &s, 0, 0); - - min = UINT64_MAX; - for(i = 0; i < b.lcm; i++) { - printf("best[%llu, %zu, %zu] = %llu\n", i, 0UL, 0UL, best[i * (b.w * b.h)]); + // b.lcm = 12; + + set s; + s.n = 0; + s.nalloc = b.lcm * b.h * b.w; + s.d = calloc(s.nalloc, sizeof(*s.d)); + + uint64_t *best = malloc(s.nalloc * sizeof(*best)); + memset(best, 0xff, s.nalloc * sizeof(*best)); + + uint64_t i, r, x, y, nr, cb; + uint64_t bestend[b.lcm]; + + for (i = 0; i < b.lcm; i++) { + bestend[i] = UINT64_MAX; + if (!intersects(&b, 0, 0, i + 1)) { + best[i * (b.w * b.h)] = i + 1; + s.d[i * (b.w * b.h)] = 1; + s.n++; + } + } + + bestfinder(&b, best, bestend, &s, b.w - 1, b.h - 1); + + uint64_t pb[b.lcm]; + + uint64_t min = UINT64_MAX; + for (i = 0; i < b.lcm; i++) { + printf("best[%llu, %zu, %zu] = %llu\n", i, b.w - 1, b.h - 1, + best[i * (b.w * b.h) + ((b.h - 1) * b.w) + (b.w - 1)]); printf("bestend[%llu] = %llu\n", i, bestend[i]); - if(bestend[i] < min) { - min = bestend[i]; - } - } - printf("min = %llu\n", min); - - memset(best, 0xff, s.nalloc * sizeof(*best)); - for(i = 0; i < b.lcm; i++) { - uint64_t t = bestend[i]; - bestend[i] = UINT64_MAX; - if(!intersects(&b, 0, 0, i+1)) { - best[i * (b.w * b.h)] = t+1; - s.d[i * (b.w * b.h)] = 1; - s.n++; - } - } - - bestfinder(&b, best, bestend, &s, b.w-1, b.h-1); - - min = UINT64_MAX; - for(i = 0; i < b.lcm; i++) { - printf("best[%llu, %zu, %zu] = %llu\n", i, b.w - 1, b.h - 1, best[i * (b.w * b.h) + ((b.h - 1) * b.w) + (b.w - 1)]); - printf("bestend[%llu] = %llu\n", i, bestend[i]); - if(bestend[i] < min) { - min = bestend[i]; - } - } - printf("min = %llu\n", min); + if (bestend[i] < min) { + min = bestend[i]; + } + } + printf("min = %llu\n", min); + + memset(best, 0xff, s.nalloc * sizeof(*best)); + for (i = 0; i < b.lcm; i++) { + uint64_t t = bestend[i]; + bestend[i] = UINT64_MAX; + if (!intersects(&b, b.w - 1, b.h - 1, i + 1)) { + best[i * (b.w * b.h) + ((b.h - 1) * b.w) + (b.w - 1)] = t + 1; + s.d[i * (b.w * b.h) + ((b.h - 1) * b.w) + (b.w - 1)] = 1; + s.n++; + } + } + + bestfinder(&b, best, bestend, &s, 0, 0); + + min = UINT64_MAX; + for (i = 0; i < b.lcm; i++) { + printf("best[%llu, %zu, %zu] = %llu\n", i, 0UL, 0UL, best[i * (b.w * b.h)]); + printf("bestend[%llu] = %llu\n", i, bestend[i]); + if (bestend[i] < min) { + min = bestend[i]; + } + } + printf("min = %llu\n", min); + + memset(best, 0xff, s.nalloc * sizeof(*best)); + for (i = 0; i < b.lcm; i++) { + uint64_t t = bestend[i]; + bestend[i] = UINT64_MAX; + if (!intersects(&b, 0, 0, i + 1)) { + best[i * (b.w * b.h)] = t + 1; + s.d[i * (b.w * b.h)] = 1; + s.n++; + } + } + + bestfinder(&b, best, bestend, &s, b.w - 1, b.h - 1); + + min = UINT64_MAX; + for (i = 0; i < b.lcm; i++) { + printf("best[%llu, %zu, %zu] = %llu\n", i, b.w - 1, b.h - 1, + best[i * (b.w * b.h) + ((b.h - 1) * b.w) + (b.w - 1)]); + printf("bestend[%llu] = %llu\n", i, bestend[i]); + if (bestend[i] < min) { + min = bestend[i]; + } + } + printf("min = %llu\n", min); } diff --git a/day24/common.h b/day24/common.h @@ -1,36 +1,37 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> typedef struct { size_t n; - uint64_t* v; + uint64_t *v; } bitvector; #define HEADBIT_U64 0x8000000000000000LL typedef struct { - size_t n; - - size_t nalloc; - uint8_t* d; + size_t n; + + size_t nalloc; + uint8_t *d; } set; typedef struct { - size_t h; - size_t w; - size_t lcm; + size_t h; + size_t w; + size_t lcm; - bitvector** row_mask; - bitvector** col_mask; + bitvector **row_mask; + bitvector **col_mask; } board; -void play(char** lines, size_t nlines); -board parse(char** lines, size_t nlines); -int intersects(const board* b, const uint64_t x, const uint64_t y, const size_t r); +void play(char **lines, size_t nlines); +board parse(char **lines, size_t nlines); +int intersects(const board *b, const uint64_t x, const uint64_t y, + const size_t r); #endif diff --git a/day24/uppga.c b/day24/uppga.c @@ -1,96 +1,100 @@ #include "common.h" -void playa(char** lines, size_t nlines) -{ - board b = parse(lines, nlines); +void playa(char **lines, size_t nlines) { + board b = parse(lines, nlines); b.lcm = 700; - set s; - s.n = 0; - s.nalloc = b.lcm * b.h * b.w; - s.d = calloc(s.nalloc, sizeof(*s.d)); + set s; + s.n = 0; + s.nalloc = b.lcm * b.h * b.w; + s.d = calloc(s.nalloc, sizeof(*s.d)); - uint64_t* best = malloc(s.nalloc * sizeof(*best)); - memset(best, 0xff, s.nalloc * sizeof(*best)); + uint64_t *best = malloc(s.nalloc * sizeof(*best)); + memset(best, 0xff, s.nalloc * sizeof(*best)); - uint64_t i, r, x, y, nr, cb; + uint64_t i, r, x, y, nr, cb; - for(i = 0; i < b.lcm; i++) { - best[i * (b.w * b.h)] = i+1; - s.d[i * (b.w * b.h)] = 1; - s.n++; - } - - while(s.n != 0) { - i = 0; - while(s.d[i] == 0) i++; - s.d[i] = 0; - s.n--; + for (i = 0; i < b.lcm; i++) { + best[i * (b.w * b.h)] = i + 1; + s.d[i * (b.w * b.h)] = 1; + s.n++; + } + + while (s.n != 0) { + i = 0; + while (s.d[i] == 0) + i++; + s.d[i] = 0; + s.n--; x = i % (b.w); - y = (i / b.w) % b.h; - r = i / (b.w * b.h); - nr = (r + 1) % b.lcm; - cb = best[r * (b.w * b.h) + y * b.w + x]; - - if(!intersects(&b, x, y, nr) && best[nr * (b.w * b.h) + y * b.w + x] > cb+1) { - best[nr * (b.w * b.h) + y * b.w + x] = cb+1; - if(s.d[nr * (b.w * b.h) + y * b.w + x] == 0) { - s.d[nr * (b.w * b.h) + y * b.w + x] = 1; - s.n++; - } - } - - if(x > 0 && !intersects(&b, x-1, y, nr) && best[nr * (b.w * b.h) + y * b.w + (x-1)] > cb+1) { - best[nr * (b.w * b.h) + y * b.w + (x-1)] = cb+1; - if(s.d[nr * (b.w * b.h) + y * b.w + (x-1)] == 0) { - s.d[nr * (b.w * b.h) + y * b.w + (x-1)] = 1; - s.n++; - } - } - - if(x < b.w-1 && !intersects(&b, x+1, y, nr) && best[nr * (b.w * b.h) + y * b.w + (x+1)] > cb+1) { - best[nr * (b.w * b.h) + y * b.w + (x+1)] = cb+1; - if(s.d[nr * (b.w * b.h) + y * b.w + (x+1)] == 0) { - s.d[nr * (b.w * b.h) + y * b.w + (x+1)] = 1; - s.n++; - } - } - - if(y > 0 && !intersects(&b, x, y-1, nr) && best[nr * (b.w * b.h) + (y-1) * b.w + x] > cb+1) { - best[nr * (b.w * b.h) + (y-1) * b.w + x] = cb+1; - if(s.d[nr * (b.w * b.h) + (y-1) * b.w + x] == 0) { - s.d[nr * (b.w * b.h) + (y-1) * b.w + x] = 1; - s.n++; - } - } - - if(y < b.h-1 && !intersects(&b, x, y+1, nr) && best[nr * (b.w * b.h) + (y+1) * b.w + x] > cb+1) { - best[nr * (b.w * b.h) + (y+1) * b.w + x] = cb+1; - if(s.d[nr * (b.w * b.h) + (y+1) * b.w + x] == 0) { - s.d[nr * (b.w * b.h) + (y+1) * b.w + x] = 1; - s.n++; - } - } - } - - uint64_t min = UINT64_MAX; - for(i = 0; i < b.lcm; i++) { - if(best[i * (b.w * b.h) + ((b.h - 1) * b.w) + (b.w - 1)] < min) { - min = best[i * (b.w * b.h) + ((b.h - 1) * b.w) + (b.w - 1)]; - } - } - - printf("min = %llu\n", min); + y = (i / b.w) % b.h; + r = i / (b.w * b.h); + nr = (r + 1) % b.lcm; + cb = best[r * (b.w * b.h) + y * b.w + x]; + + if (!intersects(&b, x, y, nr) && + best[nr * (b.w * b.h) + y * b.w + x] > cb + 1) { + best[nr * (b.w * b.h) + y * b.w + x] = cb + 1; + if (s.d[nr * (b.w * b.h) + y * b.w + x] == 0) { + s.d[nr * (b.w * b.h) + y * b.w + x] = 1; + s.n++; + } + } + + if (x > 0 && !intersects(&b, x - 1, y, nr) && + best[nr * (b.w * b.h) + y * b.w + (x - 1)] > cb + 1) { + best[nr * (b.w * b.h) + y * b.w + (x - 1)] = cb + 1; + if (s.d[nr * (b.w * b.h) + y * b.w + (x - 1)] == 0) { + s.d[nr * (b.w * b.h) + y * b.w + (x - 1)] = 1; + s.n++; + } + } + + if (x < b.w - 1 && !intersects(&b, x + 1, y, nr) && + best[nr * (b.w * b.h) + y * b.w + (x + 1)] > cb + 1) { + best[nr * (b.w * b.h) + y * b.w + (x + 1)] = cb + 1; + if (s.d[nr * (b.w * b.h) + y * b.w + (x + 1)] == 0) { + s.d[nr * (b.w * b.h) + y * b.w + (x + 1)] = 1; + s.n++; + } + } + + if (y > 0 && !intersects(&b, x, y - 1, nr) && + best[nr * (b.w * b.h) + (y - 1) * b.w + x] > cb + 1) { + best[nr * (b.w * b.h) + (y - 1) * b.w + x] = cb + 1; + if (s.d[nr * (b.w * b.h) + (y - 1) * b.w + x] == 0) { + s.d[nr * (b.w * b.h) + (y - 1) * b.w + x] = 1; + s.n++; + } + } + + if (y < b.h - 1 && !intersects(&b, x, y + 1, nr) && + best[nr * (b.w * b.h) + (y + 1) * b.w + x] > cb + 1) { + best[nr * (b.w * b.h) + (y + 1) * b.w + x] = cb + 1; + if (s.d[nr * (b.w * b.h) + (y + 1) * b.w + x] == 0) { + s.d[nr * (b.w * b.h) + (y + 1) * b.w + x] = 1; + s.n++; + } + } + } + + uint64_t min = UINT64_MAX; + for (i = 0; i < b.lcm; i++) { + if (best[i * (b.w * b.h) + ((b.h - 1) * b.w) + (b.w - 1)] < min) { + min = best[i * (b.w * b.h) + ((b.h - 1) * b.w) + (b.w - 1)]; + } + } + + printf("min = %llu\n", min); } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: \t%lu\n", nlines); - printf("#cols : \t%lu\n", strlen(lines[0])-1); + printf("#cols : \t%lu\n", strlen(lines[0]) - 1); - playa(lines, nlines); + playa(lines, nlines); } diff --git a/day24/uppgb.c b/day24/uppgb.c @@ -1,12 +1,11 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: \t%lu\n", nlines); - printf("#cols : \t%lu\n", strlen(lines[0])-1); + printf("#cols : \t%lu\n", strlen(lines[0]) - 1); - play(lines, nlines); + play(lines, nlines); } diff --git a/day25/common.c b/day25/common.c @@ -1,74 +1,71 @@ #include "common.h" -int64_t snafu2i64(const char* str, const size_t len) -{ - assert(len < 27); +int64_t snafu2i64(const char *str, const size_t len) { + assert(len < 27); - int64_t r = 0; + int64_t r = 0; - size_t i; - for(i = 0; i < len; i++) { - r *= 5; - if(str[i] == '1') { - r += 1; - } else if(str[i] == '2') { - r += 2; - } else if(str[i] == '0') { - r += 0; // hah - } else if(str[i] == '-') { - r -= 1; - } else if(str[i] == '=') { - r -= 2; - } else { - fprintf(stderr, "Error! Bad SNAFU char %c\n", str[i]); - exit(1); - } - } + size_t i; + for (i = 0; i < len; i++) { + r *= 5; + if (str[i] == '1') { + r += 1; + } else if (str[i] == '2') { + r += 2; + } else if (str[i] == '0') { + r += 0; // hah + } else if (str[i] == '-') { + r -= 1; + } else if (str[i] == '=') { + r -= 2; + } else { + fprintf(stderr, "Error! Bad SNAFU char %c\n", str[i]); + exit(1); + } + } - return r; + return r; } -static inline void strrev(char* buf, const size_t len) -{ - size_t i = 0; - while(i < len-i-1) { - buf[i] ^= buf[len-i-1] ^= buf[i] ^= buf[len-i-1]; - i++; - } +static inline void strrev(char *buf, const size_t len) { + size_t i = 0; + while (i < len - i - 1) { + buf[i] ^= buf[len - i - 1] ^= buf[i] ^= buf[len - i - 1]; + i++; + } } -void i64_2snafu(char* buffer, const size_t buffer_len, const int64_t x) -{ - int64_t t = x; - size_t i = 0; +void i64_2snafu(char *buffer, const size_t buffer_len, const int64_t x) { + int64_t t = x; + size_t i = 0; - while(t > 0) { - assert(i < buffer_len-1); - switch(t % 5) { - case 0: - buffer[i] = '0'; - t /= 5; - break; - case 1: - buffer[i] = '1'; - t = t/5; - break; - case 2: - buffer[i] = '2'; - t = t/5; - break; - case 3: - buffer[i] = '='; - t = (t+2)/5; - break; - case 4: - buffer[i] = '-'; - t = (t+1)/5; - break; - } - i++; - } - - buffer[i] = '\0'; - strrev(buffer, i); + while (t > 0) { + assert(i < buffer_len - 1); + switch (t % 5) { + case 0: + buffer[i] = '0'; + t /= 5; + break; + case 1: + buffer[i] = '1'; + t = t / 5; + break; + case 2: + buffer[i] = '2'; + t = t / 5; + break; + case 3: + buffer[i] = '='; + t = (t + 2) / 5; + break; + case 4: + buffer[i] = '-'; + t = (t + 1) / 5; + break; + } + i++; + } + + buffer[i] = '\0'; + strrev(buffer, i); } diff --git a/day25/common.h b/day25/common.h @@ -1,12 +1,12 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> -int64_t snafu2i64(const char* str, const size_t len); -void i64_2snafu(char* buffer, const size_t buffer_len, int64_t x); +int64_t snafu2i64(const char *str, const size_t len); +void i64_2snafu(char *buffer, const size_t buffer_len, int64_t x); #endif diff --git a/day25/uppga.c b/day25/uppga.c @@ -1,19 +1,18 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - int64_t sum = 0; + int64_t sum = 0; - size_t i; - for(i = 0; i < nlines; i++) { - sum += snafu2i64(lines[i], strlen(lines[i])-1); - } + size_t i; + for (i = 0; i < nlines; i++) { + sum += snafu2i64(lines[i], strlen(lines[i]) - 1); + } - char buf[256]; + char buf[256]; i64_2snafu(buf, 256, sum); - printf("%s\n", buf); + printf("%s\n", buf); } diff --git a/day3/common.c b/day3/common.c @@ -1,14 +1,11 @@ #include "common.h" -size_t map(const char c) -{ - assert(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')); - if(c <= 'Z') { - return (size_t) (c - 'A') + 26; - } - return (size_t) (c - 'a'); +size_t map(const char c) { + assert(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z')); + if (c <= 'Z') { + return (size_t)(c - 'A') + 26; + } + return (size_t)(c - 'a'); } -size_t prio(const uint64_t x) { - return x+1; -} +size_t prio(const uint64_t x) { return x + 1; } diff --git a/day3/common.h b/day3/common.h @@ -1,15 +1,15 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> #include <smallset.h> +#include <stdint.h> +#include <stdio.h> -#define ASIZE (2*26) +#define ASIZE (2 * 26) size_t map(const char c); size_t prio(const uint64_t x); - + #endif diff --git a/day3/uppga.c b/day3/uppga.c @@ -1,26 +1,34 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; - size_t nlines = readlines(&lines, "input"); +int main(int argc, char **argv) { + char **lines; + size_t nlines = readlines(&lines, "input"); - size_t i,j,sum=0; - smallset a,b,c; - smallset_init(&a, ASIZE); smallset_init(&b, ASIZE); smallset_init(&c, ASIZE); - for(i = 0; i < nlines; i++) { - size_t slen = strlen(lines[i]); - if(lines[i][slen-1] == '\n') slen--; + size_t i, j, sum = 0; + smallset a, b, c; + smallset_init(&a, ASIZE); + smallset_init(&b, ASIZE); + smallset_init(&c, ASIZE); + for (i = 0; i < nlines; i++) { + size_t slen = strlen(lines[i]); + if (lines[i][slen - 1] == '\n') + slen--; - for(j = 0; j < slen/2; j++) smallset_insert(&a, map(lines[i][j])); - for(j = slen/2; j < slen; j++) smallset_insert(&b, map(lines[i][j])); - smallset_intersection(&c, &a, &b); + for (j = 0; j < slen / 2; j++) + smallset_insert(&a, map(lines[i][j])); + for (j = slen / 2; j < slen; j++) + smallset_insert(&b, map(lines[i][j])); + smallset_intersection(&c, &a, &b); - sum += prio(smallset_getone(&c)); + sum += prio(smallset_getone(&c)); - smallset_empty(&c); smallset_empty(&b); smallset_empty(&a); - } - smallset_clear(&a); smallset_clear(&b); smallset_clear(&c); + smallset_empty(&c); + smallset_empty(&b); + smallset_empty(&a); + } + smallset_clear(&a); + smallset_clear(&b); + smallset_clear(&c); - printf("sum: %zu\n", sum); + printf("sum: %zu\n", sum); } diff --git a/day3/uppgb.c b/day3/uppgb.c @@ -1,28 +1,33 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; - size_t nlines = readlines(&lines, "input"); +int main(int argc, char **argv) { + char **lines; + size_t nlines = readlines(&lines, "input"); - size_t i,j,k,sum=0,slen; - smallset a[3]; - smallset_init(&a[0], ASIZE); smallset_init(&a[1], ASIZE); smallset_init(&a[2], ASIZE); + size_t i, j, k, sum = 0, slen; + smallset a[3]; + smallset_init(&a[0], ASIZE); + smallset_init(&a[1], ASIZE); + smallset_init(&a[2], ASIZE); - for(i = 0; i < nlines; i += 3) { - for(j = 0; j < 3; j++) { - slen = strlen(lines[i+j]); - if(lines[i+j][slen-1] == '\n') slen--; - for(k = 0; k < slen; k++) smallset_insert(&a[j], map(lines[i+j][k])); - } + for (i = 0; i < nlines; i += 3) { + for (j = 0; j < 3; j++) { + slen = strlen(lines[i + j]); + if (lines[i + j][slen - 1] == '\n') + slen--; + for (k = 0; k < slen; k++) + smallset_insert(&a[j], map(lines[i + j][k])); + } - smallset_intersection(&a[0], &a[0], &a[1]); - smallset_intersection(&a[0], &a[0], &a[2]); - sum += prio(smallset_getone(&a[0])); + smallset_intersection(&a[0], &a[0], &a[1]); + smallset_intersection(&a[0], &a[0], &a[2]); + sum += prio(smallset_getone(&a[0])); - for(j = 0; j < 3; j++) smallset_empty(&a[j]); - } - for(j = 0; j < 3; j++) smallset_clear(&a[j]); + for (j = 0; j < 3; j++) + smallset_empty(&a[j]); + } + for (j = 0; j < 3; j++) + smallset_clear(&a[j]); - printf("sum: %zu\n", sum); + printf("sum: %zu\n", sum); } diff --git a/day4/common.c b/day4/common.c @@ -1,10 +1,13 @@ #include "common.h" -int readfour(uint64_t* v, FILE* fp) -{ - if(read_next_u64(&v[0], fp) == EOF) return EOF; - if(read_next_u64(&v[1], fp) == EOF) return EOF; - if(read_next_u64(&v[2], fp) == EOF) return EOF; - if(read_next_u64(&v[3], fp) == EOF) return EOF; - return 0; +int readfour(uint64_t *v, FILE *fp) { + if (read_next_u64(&v[0], fp) == EOF) + return EOF; + if (read_next_u64(&v[1], fp) == EOF) + return EOF; + if (read_next_u64(&v[2], fp) == EOF) + return EOF; + if (read_next_u64(&v[3], fp) == EOF) + return EOF; + return 0; } diff --git a/day4/common.h b/day4/common.h @@ -1,9 +1,9 @@ #ifndef COMMON_H #define COMMON_H -#include <stdio.h> #include <reading.h> +#include <stdio.h> -int readfour(uint64_t* v, FILE* fp); +int readfour(uint64_t *v, FILE *fp); #endif diff --git a/day4/uppga.c b/day4/uppga.c @@ -1,21 +1,19 @@ #include "common.h" -static inline int iscontain(uint64_t *v) -{ - return ((v[0] <= v[2] && v[3] <= v[1]) || - (v[2] <= v[0] && v[1] <= v[3])); +static inline int iscontain(uint64_t *v) { + return ((v[0] <= v[2] && v[3] <= v[1]) || (v[2] <= v[0] && v[1] <= v[3])); } -int main(int argc, char** argv) -{ - uint64_t v[4]; - uint64_t c = 0; - FILE* fp = fopen("input", "r"); +int main(int argc, char **argv) { + uint64_t v[4]; + uint64_t c = 0; + FILE *fp = fopen("input", "r"); - while(readfour(v, fp) != EOF) { - if(iscontain(v)) c++; - } - printf("%llu\n", c); + while (readfour(v, fp) != EOF) { + if (iscontain(v)) + c++; + } + printf("%llu\n", c); - return 0; + return 0; } diff --git a/day4/uppgb.c b/day4/uppgb.c @@ -1,23 +1,20 @@ #include "common.h" -static inline int iscontain(uint64_t *v) -{ - return ((v[0] <= v[2] && v[3] <= v[1]) || - (v[2] <= v[0] && v[1] <= v[3]) || - (v[0] <= v[2] && v[2] <= v[1]) || - (v[2] <= v[0] && v[0] <= v[3])); +static inline int iscontain(uint64_t *v) { + return ((v[0] <= v[2] && v[3] <= v[1]) || (v[2] <= v[0] && v[1] <= v[3]) || + (v[0] <= v[2] && v[2] <= v[1]) || (v[2] <= v[0] && v[0] <= v[3])); } -int main(int argc, char** argv) -{ - uint64_t v[4]; - uint64_t c = 0; - FILE* fp = fopen("input", "r"); +int main(int argc, char **argv) { + uint64_t v[4]; + uint64_t c = 0; + FILE *fp = fopen("input", "r"); - while(readfour(v, fp) != EOF) { - if(iscontain(v)) c++; - } - printf("%llu\n", c); + while (readfour(v, fp) != EOF) { + if (iscontain(v)) + c++; + } + printf("%llu\n", c); - return 0; + return 0; } diff --git a/day5/common.c b/day5/common.c @@ -1,80 +1,81 @@ #include "common.h" -#define swap(x,y) (x^=y^=x^=y) +#define swap(x, y) (x ^= y ^= x ^= y) -static inline void invertstacks(stack_u64** stacks, size_t nstacks) -{ - size_t i,k,l; - stack_u64* ss = *stacks; - for(i = 0; i < nstacks; i++) { - k = 0; - l = ss[i].nmemb-1; - while(k < l) { - swap(ss[i].data[k], ss[i].data[l]); - k++; - l--; - } - } +static inline void invertstacks(stack_u64 **stacks, size_t nstacks) { + size_t i, k, l; + stack_u64 *ss = *stacks; + for (i = 0; i < nstacks; i++) { + k = 0; + l = ss[i].nmemb - 1; + while (k < l) { + swap(ss[i].data[k], ss[i].data[l]); + k++; + l--; + } + } } -void makeamove(stack_u64* stacks, size_t nstacks, uint64_t* move) { - uint64_t f=move[1], t=move[2], n=move[0]; - assert(f < nstacks); - assert(t < nstacks); - assert(n <= stacks[f].nmemb); - size_t i; - for(i = stacks[f].nmemb-n; i < stacks[f].nmemb; i++) - stack_u64_push(&stacks[t], stacks[f].data[i]); - stacks[f].nmemb -= n; +void makeamove(stack_u64 *stacks, size_t nstacks, uint64_t *move) { + uint64_t f = move[1], t = move[2], n = move[0]; + assert(f < nstacks); + assert(t < nstacks); + assert(n <= stacks[f].nmemb); + size_t i; + for (i = stacks[f].nmemb - n; i < stacks[f].nmemb; i++) + stack_u64_push(&stacks[t], stacks[f].data[i]); + stacks[f].nmemb -= n; } -void printtops(stack_u64* stacks, size_t nstacks) { - size_t i; - for(i = 0; i < nstacks; i++) printf("%c", (char) stack_u64_getlast(&stacks[i])); +void printtops(stack_u64 *stacks, size_t nstacks) { + size_t i; + for (i = 0; i < nstacks; i++) + printf("%c", (char)stack_u64_getlast(&stacks[i])); } -void parse(stack_u64* moves, stack_u64** stacks, size_t* nstacks, char** lines, size_t nlines) -{ +void parse(stack_u64 *moves, stack_u64 **stacks, size_t *nstacks, char **lines, + size_t nlines) { int hasstacks = 0; - size_t nalloc_stacks = 128; - size_t ninitd_stacks = 0; - *stacks = calloc(nalloc_stacks, sizeof(**stacks)); - stack_u64_init(moves); + size_t nalloc_stacks = 128; + size_t ninitd_stacks = 0; + *stacks = calloc(nalloc_stacks, sizeof(**stacks)); + stack_u64_init(moves); - size_t r,i; - for(r = 0; r < nlines; r++) { - char* line = lines[r]; - if(strlen(line) > 0 && line[1] == '1') { - hasstacks = 1; - *nstacks = ninitd_stacks; - invertstacks(stacks, *nstacks); - } + size_t r, i; + for (r = 0; r < nlines; r++) { + char *line = lines[r]; + if (strlen(line) > 0 && line[1] == '1') { + hasstacks = 1; + *nstacks = ninitd_stacks; + invertstacks(stacks, *nstacks); + } - if(!hasstacks) { - for(i = 1; i < strlen(line); i += 4) { - size_t j = i >> 2; - while(j+1 > ninitd_stacks) { - if(ninitd_stacks >= nalloc_stacks) { - nalloc_stacks <<= 1; - *stacks = realloc(*stacks, nalloc_stacks * sizeof(**stacks)); - } - stack_u64_init(&(*stacks)[ninitd_stacks]); - ninitd_stacks++; - } + if (!hasstacks) { + for (i = 1; i < strlen(line); i += 4) { + size_t j = i >> 2; + while (j + 1 > ninitd_stacks) { + if (ninitd_stacks >= nalloc_stacks) { + nalloc_stacks <<= 1; + *stacks = realloc(*stacks, nalloc_stacks * sizeof(**stacks)); + } + stack_u64_init(&(*stacks)[ninitd_stacks]); + ninitd_stacks++; + } - if(line[i] != ' ') stack_u64_push(&(*stacks)[j], (uint64_t) line[i]); - } - } else { - if(strlen(line) > 0 && line[0] == 'm') { - char* p; - uint64_t rn; - p = sread_next_u64(&rn, line); - stack_u64_push(moves, rn); - p = sread_next_u64(&rn, p); - stack_u64_push(moves, rn-1); - p = sread_next_u64(&rn, p); - stack_u64_push(moves, rn-1); - } - } - } + if (line[i] != ' ') + stack_u64_push(&(*stacks)[j], (uint64_t)line[i]); + } + } else { + if (strlen(line) > 0 && line[0] == 'm') { + char *p; + uint64_t rn; + p = sread_next_u64(&rn, line); + stack_u64_push(moves, rn); + p = sread_next_u64(&rn, p); + stack_u64_push(moves, rn - 1); + p = sread_next_u64(&rn, p); + stack_u64_push(moves, rn - 1); + } + } + } } diff --git a/day5/common.h b/day5/common.h @@ -1,15 +1,16 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> -#include <string.h> -#include <stack_u64.h> #include <reading.h> +#include <stack_u64.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> -void parse(stack_u64* moves, stack_u64** stacks, size_t* nstacks, char** lines, size_t nlines); -void makeamove(stack_u64* stacks, size_t nstacks, uint64_t* move); -void printtops(stack_u64* stacks, size_t nstacks); +void parse(stack_u64 *moves, stack_u64 **stacks, size_t *nstacks, char **lines, + size_t nlines); +void makeamove(stack_u64 *stacks, size_t nstacks, uint64_t *move); +void printtops(stack_u64 *stacks, size_t nstacks); #endif diff --git a/day5/uppga.c b/day5/uppga.c @@ -1,28 +1,27 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; - size_t nlines = readlines(&lines, "input"); +int main(int argc, char **argv) { + char **lines; + size_t nlines = readlines(&lines, "input"); - stack_u64* stacks; - size_t nstacks; - stack_u64 moves; + stack_u64 *stacks; + size_t nstacks; + stack_u64 moves; - parse(&moves, &stacks, &nstacks, lines, nlines); + parse(&moves, &stacks, &nstacks, lines, nlines); - size_t i,j; - for(i = 0; i < moves.nmemb; i+=3) { - uint64_t tmp = moves.data[i]; - moves.data[i] = 1; - for(j = 0; j < tmp; j++) - makeamove(stacks, nstacks, &(moves.data[i])); - } - printtops(stacks, nstacks); - printf("\n"); + size_t i, j; + for (i = 0; i < moves.nmemb; i += 3) { + uint64_t tmp = moves.data[i]; + moves.data[i] = 1; + for (j = 0; j < tmp; j++) + makeamove(stacks, nstacks, &(moves.data[i])); + } + printtops(stacks, nstacks); + printf("\n"); - for(i = 0; i < nstacks; i++) { - stack_u64_clear(&stacks[i]); - } - stack_u64_clear(&moves); + for (i = 0; i < nstacks; i++) { + stack_u64_clear(&stacks[i]); + } + stack_u64_clear(&moves); } diff --git a/day5/uppgb.c b/day5/uppgb.c @@ -1,26 +1,25 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; - size_t nlines = readlines(&lines, "input"); +int main(int argc, char **argv) { + char **lines; + size_t nlines = readlines(&lines, "input"); - stack_u64* stacks; - size_t nstacks; - stack_u64 moves; + stack_u64 *stacks; + size_t nstacks; + stack_u64 moves; - parse(&moves, &stacks, &nstacks, lines, nlines); + parse(&moves, &stacks, &nstacks, lines, nlines); - size_t i; + size_t i; - for(i = 0; i < moves.nmemb; i+=3) { - makeamove(stacks, nstacks, &(moves.data[i])); - } - printtops(stacks, nstacks); - printf("\n"); + for (i = 0; i < moves.nmemb; i += 3) { + makeamove(stacks, nstacks, &(moves.data[i])); + } + printtops(stacks, nstacks); + printf("\n"); - for(i = 0; i < nstacks; i++) { - stack_u64_clear(&stacks[i]); - } - stack_u64_clear(&moves); + for (i = 0; i < nstacks; i++) { + stack_u64_clear(&stacks[i]); + } + stack_u64_clear(&moves); } diff --git a/day6/common.c b/day6/common.c @@ -1,12 +1,12 @@ #include "common.h" -int pairwise_diff(char* s, size_t n) -{ - int i,j; +int pairwise_diff(char *s, size_t n) { + int i, j; - for(i = n-2; i >= 0; i--) - for(j = i+1; j < n; j++) - if(s[i] == s[j]) return i+1; + for (i = n - 2; i >= 0; i--) + for (j = i + 1; j < n; j++) + if (s[i] == s[j]) + return i + 1; return 0; } diff --git a/day6/common.h b/day6/common.h @@ -1,11 +1,11 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> -int pairwise_diff(char* s, size_t n); +int pairwise_diff(char *s, size_t n); #endif diff --git a/day6/uppga.c b/day6/uppga.c @@ -1,20 +1,19 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; - size_t nlines = readlines(&lines, "input"); +int main(int argc, char **argv) { + char **lines; + size_t nlines = readlines(&lines, "input"); - assert(nlines == 1); + assert(nlines == 1); - char* s = lines[0]; - size_t len = strlen(s); + char *s = lines[0]; + size_t len = strlen(s); - size_t i,step=0; - for(i = 0; i < len; i+=step, s+= step) { - if((step = pairwise_diff(s, 4)) == 0) { - printf("%zu\n", i+4); - break; - } + size_t i, step = 0; + for (i = 0; i < len; i += step, s += step) { + if ((step = pairwise_diff(s, 4)) == 0) { + printf("%zu\n", i + 4); + break; } + } } diff --git a/day6/uppgb.c b/day6/uppgb.c @@ -1,20 +1,19 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; - size_t nlines = readlines(&lines, "input"); +int main(int argc, char **argv) { + char **lines; + size_t nlines = readlines(&lines, "input"); - assert(nlines == 1); + assert(nlines == 1); - char* s = lines[0]; - size_t len = strlen(s); + char *s = lines[0]; + size_t len = strlen(s); - size_t i,step=0; - for(i = 0; i < len; i+=step, s += step) { - if((step = pairwise_diff(s, 14)) == 0) { - printf("%zu\n", i+14); - break; - } + size_t i, step = 0; + for (i = 0; i < len; i += step, s += step) { + if ((step = pairwise_diff(s, 14)) == 0) { + printf("%zu\n", i + 14); + break; } + } } diff --git a/day7/common.c b/day7/common.c @@ -1,122 +1,115 @@ #include "common.h" -void tn_init(tn* t, char* name, const size_t size) { - t->name = name; - t->size = size; +void tn_init(tn *t, char *name, const size_t size) { + t->name = name; + t->size = size; t->children_alloc = TN_DEFAULT_ALLOC; t->children_n = 0; - t->children = malloc(t->children_alloc * sizeof(*(t->children))); + t->children = malloc(t->children_alloc * sizeof(*(t->children))); - t->parent = NULL; + t->parent = NULL; } -void tn_set_parent(tn* t, tn* pere) -{ - t->parent = pere; -} +void tn_set_parent(tn *t, tn *pere) { t->parent = pere; } + +void tn_add_child(tn *t, tn *fils) { + if (t->children_n >= t->children_alloc) { + t->children_alloc <<= 1; + t->children = + realloc(t->children, t->children_alloc * sizeof(*(t->children))); + } -void tn_add_child(tn* t, tn* fils) -{ - if(t->children_n >= t->children_alloc) { - t->children_alloc <<= 1; - t->children = realloc(t->children, - t->children_alloc * sizeof(*(t->children))); - } - - t->children[t->children_n] = fils; - t->children_n++; - fils->parent = t; + t->children[t->children_n] = fils; + t->children_n++; + fils->parent = t; } -void print_tree(tn* t, size_t level) -{ - size_t i; - for(i = 0; i < level; i++) printf(" "); - printf("%s (%zu)\n", t->name, t->size); - for(i = 0; i < t->children_n; i++) - print_tree(t->children[i], level+2); +void print_tree(tn *t, size_t level) { + size_t i; + for (i = 0; i < level; i++) + printf(" "); + printf("%s (%zu)\n", t->name, t->size); + for (i = 0; i < t->children_n; i++) + print_tree(t->children[i], level + 2); } -void tn_cleartree(tn* t) -{ - size_t i; - for(i = 0; i < t->children_n; i++) tn_cleartree(t->children[i]); +void tn_cleartree(tn *t) { + size_t i; + for (i = 0; i < t->children_n; i++) + tn_cleartree(t->children[i]); free(t->children); - free(t); + free(t); } -tn* find_root(tn* t) -{ - if(t->parent == NULL) return t; - return find_root(t->parent); +tn *find_root(tn *t) { + if (t->parent == NULL) + return t; + return find_root(t->parent); } -tn* find_child(tn* t, char* s) -{ - size_t i; - for(i = 0; i < t->children_n; i++) { - if(strcmp(t->children[i]->name, s) == 0) - return t->children[i]; - } - return NULL; +tn *find_child(tn *t, char *s) { + size_t i; + for (i = 0; i < t->children_n; i++) { + if (strcmp(t->children[i]->name, s) == 0) + return t->children[i]; + } + return NULL; } -tn* parse_input(char** lines, size_t nlines) -{ - size_t i; - - size_t ralloc = 1; - size_t rn = 0; - tn** r = calloc(ralloc, sizeof(*r)); - tn* cp = NULL; - - for(i = 0; i < nlines; i++) { - char* line = lines[i]; - if(strncmp(line, "$ cd /", 6) == 0) { - if(rn == 0) { - ralloc = 1; - r[0] = malloc(sizeof(**r)); - tn_init(r[0], "", 0); - rn = 1; - } - cp = r[0]; - } else if(strncmp(line, "$ cd ..", 7) == 0) { - assert(cp->parent != NULL); - cp = cp->parent; - } else if(strncmp(line, "$ cd ", 5) == 0) { - char* ndir = &line[5]; - tn* childp; - if((childp = find_child(cp, ndir)) == NULL) { - if(rn >= ralloc) { - ralloc <<= 1; - r = realloc(r, ralloc * sizeof(*r)); - } - - r[rn] = malloc(sizeof(**r)); - tn_init(r[rn], ndir, 0); - childp = r[rn]; - rn++; - tn_add_child(cp, childp); - } - cp = childp; - } else if(line[0] != '$' && line[0] != 'd') { - uint64_t tmp; - sread_next_u64(&tmp, line); - cp->size += tmp; - } - } - - tn* root = r[0]; - free(r); - return root; +tn *parse_input(char **lines, size_t nlines) { + size_t i; + + size_t ralloc = 1; + size_t rn = 0; + tn **r = calloc(ralloc, sizeof(*r)); + tn *cp = NULL; + + for (i = 0; i < nlines; i++) { + char *line = lines[i]; + if (strncmp(line, "$ cd /", 6) == 0) { + if (rn == 0) { + ralloc = 1; + r[0] = malloc(sizeof(**r)); + tn_init(r[0], "", 0); + rn = 1; + } + cp = r[0]; + } else if (strncmp(line, "$ cd ..", 7) == 0) { + assert(cp->parent != NULL); + cp = cp->parent; + } else if (strncmp(line, "$ cd ", 5) == 0) { + char *ndir = &line[5]; + tn *childp; + if ((childp = find_child(cp, ndir)) == NULL) { + if (rn >= ralloc) { + ralloc <<= 1; + r = realloc(r, ralloc * sizeof(*r)); + } + + r[rn] = malloc(sizeof(**r)); + tn_init(r[rn], ndir, 0); + childp = r[rn]; + rn++; + tn_add_child(cp, childp); + } + cp = childp; + } else if (line[0] != '$' && line[0] != 'd') { + uint64_t tmp; + sread_next_u64(&tmp, line); + cp->size += tmp; + } + } + + tn *root = r[0]; + free(r); + return root; } -void sum_sizes(tn* t) -{ - size_t i; - for(i = 0; i < t->children_n; i++) { - sum_sizes(t->children[i]); - t->size += t->children[i]->size; - } +void sum_sizes(tn *t) { + size_t i; + for (i = 0; i < t->children_n; i++) { + sum_sizes(t->children[i]); + t->size += t->children[i]->size; + } } diff --git a/day7/common.h b/day7/common.h @@ -1,28 +1,28 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> #define TN_DEFAULT_ALLOC 1 #define NO_SIZE (SIZE_MAX) typedef struct _struct_tn { - char* name; - size_t size; + char *name; + size_t size; - size_t children_alloc; - size_t children_n; - struct _struct_tn** children; + size_t children_alloc; + size_t children_n; + struct _struct_tn **children; - struct _struct_tn* parent; + struct _struct_tn *parent; } tn; -tn* parse_input(char** lines, size_t nlines); -void sum_sizes(tn* t); -void print_tree(tn* t, size_t level); -void tn_cleartree(tn* t); +tn *parse_input(char **lines, size_t nlines); +void sum_sizes(tn *t); +void print_tree(tn *t, size_t level); +void tn_cleartree(tn *t); #endif diff --git a/day7/uppga.c b/day7/uppga.c @@ -1,28 +1,27 @@ #include "common.h" -uint64_t sum_sizes_leq(tn* cp, uint64_t leq) -{ - uint64_t r = 0; - size_t i; - for(i = 0; i < cp->children_n; i++) - r += sum_sizes_leq(cp->children[i], leq); - if(cp->size <= leq) r += cp->size; - return r; +uint64_t sum_sizes_leq(tn *cp, uint64_t leq) { + uint64_t r = 0; + size_t i; + for (i = 0; i < cp->children_n; i++) + r += sum_sizes_leq(cp->children[i], leq); + if (cp->size <= leq) + r += cp->size; + return r; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - size_t i; - for(i = 0; i < nlines; i++) { - if(lines[i][strlen(lines[i])-1] == '\n') - lines[i][strlen(lines[i])-1] = '\0'; - } + size_t i; + for (i = 0; i < nlines; i++) { + if (lines[i][strlen(lines[i]) - 1] == '\n') + lines[i][strlen(lines[i]) - 1] = '\0'; + } - tn* root = parse_input(lines, nlines); - sum_sizes(root); - printf("%llu\n", sum_sizes_leq(root, 100000)); - tn_cleartree(root); + tn *root = parse_input(lines, nlines); + sum_sizes(root); + printf("%llu\n", sum_sizes_leq(root, 100000)); + tn_cleartree(root); } diff --git a/day7/uppgb.c b/day7/uppgb.c @@ -1,30 +1,29 @@ #include "common.h" -uint64_t minok(tn* cp, uint64_t limit) -{ - uint64_t r = limit <= cp->size ? cp->size : UINT64_MAX; - size_t i; - for(i = 0; i < cp->children_n; i++) { - uint64_t tmp = minok(cp->children[i], limit); - if(tmp < r) r = tmp; - } - return r; +uint64_t minok(tn *cp, uint64_t limit) { + uint64_t r = limit <= cp->size ? cp->size : UINT64_MAX; + size_t i; + for (i = 0; i < cp->children_n; i++) { + uint64_t tmp = minok(cp->children[i], limit); + if (tmp < r) + r = tmp; + } + return r; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - size_t i; - for(i = 0; i < nlines; i++) { - if(lines[i][strlen(lines[i])-1] == '\n') - lines[i][strlen(lines[i])-1] = '\0'; - } + size_t i; + for (i = 0; i < nlines; i++) { + if (lines[i][strlen(lines[i]) - 1] == '\n') + lines[i][strlen(lines[i]) - 1] = '\0'; + } - tn* root = parse_input(lines, nlines); - sum_sizes(root); - assert(root->size + 30000000 >= 70000000); - printf("%llu\n", minok(root, (root->size + 30000000) - 70000000)); - tn_cleartree(root); + tn *root = parse_input(lines, nlines); + sum_sizes(root); + assert(root->size + 30000000 >= 70000000); + printf("%llu\n", minok(root, (root->size + 30000000) - 70000000)); + tn_cleartree(root); } diff --git a/day8/common.c b/day8/common.c @@ -1,15 +1,15 @@ #include "common.h" -uint8_t** grid(char** lines, size_t nlines, size_t* w) -{ +uint8_t **grid(char **lines, size_t nlines, size_t *w) { size_t i, j; - uint8_t** r = calloc(nlines, sizeof(*r)); + uint8_t **r = calloc(nlines, sizeof(*r)); - *w = strlen(lines[0])-1; + *w = strlen(lines[0]) - 1; - for(i = 0; i < nlines; i++) { + for (i = 0; i < nlines; i++) { r[i] = calloc(*w, sizeof(**r)); - for(j = 0; j < *w; j++) r[i][j] = lines[i][j]-'0'; + for (j = 0; j < *w; j++) + r[i][j] = lines[i][j] - '0'; } return r; diff --git a/day8/common.h b/day8/common.h @@ -1,11 +1,11 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> -uint8_t** grid(char** lines, size_t nlines, size_t* w); +uint8_t **grid(char **lines, size_t nlines, size_t *w); #endif diff --git a/day8/uppga.c b/day8/uppga.c @@ -1,74 +1,78 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); size_t w; - int i,j; - uint8_t** g = grid(lines, nlines, &w); + int i, j; + uint8_t **g = grid(lines, nlines, &w); uint8_t f[nlines][w]; - bzero(f, nlines*w); + bzero(f, nlines * w); // left size_t m; - for(i = 0; i < nlines; i++) { + for (i = 0; i < nlines; i++) { f[i][0] = 1; m = g[i][0]; - for(j = 1; j < w; j++) { - if(g[i][j] > m) { + for (j = 1; j < w; j++) { + if (g[i][j] > m) { m = g[i][j]; f[i][j] = 1; } - if(m == 9) break; + if (m == 9) + break; } } // right - for(i = 0; i < nlines; i++) { - f[i][w-1] = 1; - m = g[i][w-1]; - for(j = w-2; j > 0; j--) { - if(g[i][j] > m) { + for (i = 0; i < nlines; i++) { + f[i][w - 1] = 1; + m = g[i][w - 1]; + for (j = w - 2; j > 0; j--) { + if (g[i][j] > m) { m = g[i][j]; f[i][j] = 1; } - if(m == 9) break; + if (m == 9) + break; } } // top - for(i = 0; i < w; i++) { + for (i = 0; i < w; i++) { f[0][i] = 1; m = g[0][i]; - for(j = 1; j < nlines; j++) { - if(g[j][i] > m) { + for (j = 1; j < nlines; j++) { + if (g[j][i] > m) { m = g[j][i]; f[j][i] = 1; } - if(m == 9) break; + if (m == 9) + break; } } // bottom - for(i = 0; i < w; i++) { - f[nlines-1][i] = 1; - m = g[nlines-1][i]; - for(j = nlines-2; j > 0; j--) { - if(g[j][i] > m) { + for (i = 0; i < w; i++) { + f[nlines - 1][i] = 1; + m = g[nlines - 1][i]; + for (j = nlines - 2; j > 0; j--) { + if (g[j][i] > m) { m = g[j][i]; f[j][i] = 1; } - if(m == 9) break; + if (m == 9) + break; } } uint64_t c = 0; - for(i = 0; i < nlines; i++) { - for(j = 0; j < w; j++) { - if(f[i][j]) c++; + for (i = 0; i < nlines; i++) { + for (j = 0; j < w; j++) { + if (f[i][j]) + c++; } } printf("%llu\n", c); diff --git a/day8/uppgb.c b/day8/uppgb.c @@ -1,60 +1,63 @@ #include "common.h" -uint64_t sscore(uint8_t** g, int i, int j, size_t nlines, size_t w) -{ - uint64_t u=0,d=0,r=0,l=0; +uint64_t sscore(uint8_t **g, int i, int j, size_t nlines, size_t w) { + uint64_t u = 0, d = 0, r = 0, l = 0; int k; uint8_t m = g[i][j]; - for(k = i-1; k >= 0; k--) { + for (k = i - 1; k >= 0; k--) { u++; - if(g[k][j] >= m) break; + if (g[k][j] >= m) + break; } m = g[i][j]; - for(k = i+1; k < nlines; k++) { + for (k = i + 1; k < nlines; k++) { d++; - if(g[k][j] >= m) break; + if (g[k][j] >= m) + break; } m = g[i][j]; - for(k = j+1; k < w; k++) { + for (k = j + 1; k < w; k++) { r++; - if(g[i][k] >= m) break; + if (g[i][k] >= m) + break; } m = g[i][j]; - for(k = j-1; k >= 0; k--) { + for (k = j - 1; k >= 0; k--) { l++; - if(g[i][k] >= m) break; + if (g[i][k] >= m) + break; } - return u*d*r*l; + return u * d * r * l; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); size_t w; - uint8_t** g = grid(lines, nlines, &w); + uint8_t **g = grid(lines, nlines, &w); uint64_t f[nlines][w]; - int i,j; - for(i = 0; i < nlines; i++) - for(j = 0; j < w; j++) + int i, j; + for (i = 0; i < nlines; i++) + for (j = 0; j < w; j++) f[i][j] = 0; - for(i = 0; i < nlines; i++) - for(j = 0; j < w; j++) + for (i = 0; i < nlines; i++) + for (j = 0; j < w; j++) f[i][j] = sscore(g, i, j, nlines, w); uint64_t m = 0; - for(i = 0; i < nlines; i++) - for(j = 0; j < w; j++) - if(f[i][j] > m) m = f[i][j]; + for (i = 0; i < nlines; i++) + for (j = 0; j < w; j++) + if (f[i][j] > m) + m = f[i][j]; - printf("%llu\n", m); + printf("%llu\n", m); } diff --git a/day9/common.c b/day9/common.c @@ -1,37 +1,43 @@ #include "common.h" -uint64_t parse(int* boundaries, char* dirs, uint64_t* times, - char** lines, size_t nlines) -{ - uint64_t tot = 0; - int cp[2] = {0,0}; - bzero(boundaries, sizeof(*boundaries) * 4); +uint64_t parse(int *boundaries, char *dirs, uint64_t *times, char **lines, + size_t nlines) { + uint64_t tot = 0; + int cp[2] = {0, 0}; + bzero(boundaries, sizeof(*boundaries) * 4); - size_t i; - for(i = 0; i < nlines; i++) { - uint64_t tmp; - sread_next_u64(&tmp, lines[i]); - dirs[i] = lines[i][0]; - times[i] = tmp; - tot += tmp; + size_t i; + for (i = 0; i < nlines; i++) { + uint64_t tmp; + sread_next_u64(&tmp, lines[i]); + dirs[i] = lines[i][0]; + times[i] = tmp; + tot += tmp; - if(lines[i][0] == 'R') cp[0] += tmp; - else if(lines[i][0] == 'L') cp[0] -= tmp; - else if(lines[i][0] == 'U') cp[1] += tmp; - else if(lines[i][0] == 'D') cp[1] -= tmp; - if(cp[0] < boundaries[0]) boundaries[0] = cp[0]; - if(cp[0] > boundaries[1]) boundaries[1] = cp[0]; - if(cp[1] < boundaries[2]) boundaries[2] = cp[1]; - if(cp[1] > boundaries[3]) boundaries[3] = cp[1]; - } + if (lines[i][0] == 'R') + cp[0] += tmp; + else if (lines[i][0] == 'L') + cp[0] -= tmp; + else if (lines[i][0] == 'U') + cp[1] += tmp; + else if (lines[i][0] == 'D') + cp[1] -= tmp; + if (cp[0] < boundaries[0]) + boundaries[0] = cp[0]; + if (cp[0] > boundaries[1]) + boundaries[1] = cp[0]; + if (cp[1] < boundaries[2]) + boundaries[2] = cp[1]; + if (cp[1] > boundaries[3]) + boundaries[3] = cp[1]; + } return tot; } -uint64_t diagon(int* pos, int* boundaries) -{ - uint64_t xpos = (uint64_t) (pos[0] - boundaries[0]); - uint64_t ypos = (uint64_t) (pos[1] - boundaries[2]); - uint64_t xdim = (uint64_t) (1 + boundaries[1] - boundaries[0]); - return ypos*xdim + xpos; +uint64_t diagon(int *pos, int *boundaries) { + uint64_t xpos = (uint64_t)(pos[0] - boundaries[0]); + uint64_t ypos = (uint64_t)(pos[1] - boundaries[2]); + uint64_t xdim = (uint64_t)(1 + boundaries[1] - boundaries[0]); + return ypos * xdim + xpos; } diff --git a/day9/common.h b/day9/common.h @@ -1,15 +1,15 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> #include <smallset.h> #include <stack_u64.h> +#include <stdint.h> +#include <stdio.h> -uint64_t parse(int* boundaries, char* dirs, uint64_t* times, - char** lines, size_t nlines); -uint64_t diagon(int* pos, int* boundaries); +uint64_t parse(int *boundaries, char *dirs, uint64_t *times, char **lines, + size_t nlines); +uint64_t diagon(int *pos, int *boundaries); #endif diff --git a/day9/uppga.c b/day9/uppga.c @@ -1,86 +1,85 @@ #include "common.h" -static inline int is_touching(int* hpos, int* tpos) { - if(abs(hpos[0]-tpos[0]) > 1 || abs(hpos[1]-tpos[1]) > 1) { +static inline int is_touching(int *hpos, int *tpos) { + if (abs(hpos[0] - tpos[0]) > 1 || abs(hpos[1] - tpos[1]) > 1) { return 0; } return 1; } -int make_move(int* hpos, int* tpos, char move, smallset* s, int* boundaries) -{ - switch(move) { - case 'U': - hpos[1]++; - break; - case 'D': - hpos[1]--; - break; - case 'L': - hpos[0]--; - break; - default: - hpos[0]++; - break; +int make_move(int *hpos, int *tpos, char move, smallset *s, int *boundaries) { + switch (move) { + case 'U': + hpos[1]++; + break; + case 'D': + hpos[1]--; + break; + case 'L': + hpos[0]--; + break; + default: + hpos[0]++; + break; } - if(!is_touching(hpos, tpos)) { - if(hpos[0] == tpos[0]) { - if(hpos[1] > tpos[1]) { + if (!is_touching(hpos, tpos)) { + if (hpos[0] == tpos[0]) { + if (hpos[1] > tpos[1]) { tpos[1]++; } else { tpos[1]--; } - } else if(hpos[1] == tpos[1]) { - if(hpos[0] > tpos[0]) { + } else if (hpos[1] == tpos[1]) { + if (hpos[0] > tpos[0]) { tpos[0]++; } else { tpos[0]--; } } else { - if(hpos[0] > tpos[0]) { + if (hpos[0] > tpos[0]) { tpos[0]++; } else { tpos[0]--; } - if(hpos[1] > tpos[1]) { + if (hpos[1] > tpos[1]) { tpos[1]++; } else { tpos[1]--; } - } + } } - smallset_insert(s, diagon(tpos, boundaries)); + smallset_insert(s, diagon(tpos, boundaries)); return 0; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - char* dirs = malloc(nlines * sizeof(*dirs)); - uint64_t* times = malloc(nlines * sizeof(*times)); - int boundaries[4]; + char *dirs = malloc(nlines * sizeof(*dirs)); + uint64_t *times = malloc(nlines * sizeof(*times)); + int boundaries[4]; parse(boundaries, dirs, times, lines, nlines); - smallset s; - smallset_init(&s, (1 + boundaries[1] - boundaries[0]) * (1 + boundaries[3] - boundaries[2])); + smallset s; + smallset_init(&s, (1 + boundaries[1] - boundaries[0]) * + (1 + boundaries[3] - boundaries[2])); - int hpos[2] = {0,0}; - int tpos[2] = {0,0}; + int hpos[2] = {0, 0}; + int tpos[2] = {0, 0}; - size_t i,j; + size_t i, j; uint64_t jl; - for(i = 0; i < nlines; i++) { + for (i = 0; i < nlines; i++) { sread_next_u64(&jl, lines[i]); - for(j = 0; j < jl; j++) { + for (j = 0; j < jl; j++) { make_move(hpos, tpos, lines[i][0], &s, boundaries); } } printf("%llu\n", smallset_cardinality(&s)); - smallset_clear(&s); + smallset_clear(&s); } diff --git a/day9/uppgb.c b/day9/uppgb.c @@ -1,92 +1,91 @@ #include "common.h" -static inline int is_touching(int* hpos, int* tpos) { - if(abs(hpos[0]-tpos[0]) > 1 || abs(hpos[1]-tpos[1]) > 1) { +static inline int is_touching(int *hpos, int *tpos) { + if (abs(hpos[0] - tpos[0]) > 1 || abs(hpos[1] - tpos[1]) > 1) { return 0; } return 1; } -int make_move(int* pos, size_t npos, char move, smallset* s, int* boundaries) -{ - switch(move) { - case 'U': - pos[1]++; - break; - case 'D': - pos[1]--; - break; - case 'L': - pos[0]--; - break; - default: - pos[0]++; - break; +int make_move(int *pos, size_t npos, char move, smallset *s, int *boundaries) { + switch (move) { + case 'U': + pos[1]++; + break; + case 'D': + pos[1]--; + break; + case 'L': + pos[0]--; + break; + default: + pos[0]++; + break; } size_t i; - for(i = 0; i < 2*npos-2; i += 2) { - int* hpos = &pos[i]; - int* tpos = &pos[i+2]; - if(!is_touching(hpos, tpos)) { - if(hpos[0] == tpos[0]) { - if(hpos[1] > tpos[1]) { + for (i = 0; i < 2 * npos - 2; i += 2) { + int *hpos = &pos[i]; + int *tpos = &pos[i + 2]; + if (!is_touching(hpos, tpos)) { + if (hpos[0] == tpos[0]) { + if (hpos[1] > tpos[1]) { tpos[1]++; } else { tpos[1]--; } - } else if(hpos[1] == tpos[1]) { - if(hpos[0] > tpos[0]) { + } else if (hpos[1] == tpos[1]) { + if (hpos[0] > tpos[0]) { tpos[0]++; } else { tpos[0]--; } } else { - if(hpos[0] > tpos[0]) { + if (hpos[0] > tpos[0]) { tpos[0]++; } else { tpos[0]--; } - if(hpos[1] > tpos[1]) { + if (hpos[1] > tpos[1]) { tpos[1]++; } else { tpos[1]--; } - } + } } } - smallset_insert(s, diagon(&pos[2*npos-2], boundaries)); + smallset_insert(s, diagon(&pos[2 * npos - 2], boundaries)); return 0; } -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); - char* dirs = malloc(nlines * sizeof(*dirs)); - uint64_t* times = malloc(nlines * sizeof(*times)); - int boundaries[4]; + char *dirs = malloc(nlines * sizeof(*dirs)); + uint64_t *times = malloc(nlines * sizeof(*times)); + int boundaries[4]; parse(boundaries, dirs, times, lines, nlines); - smallset s; - smallset_init(&s, (1 + boundaries[1] - boundaries[0]) * (1 + boundaries[3] - boundaries[2])); + smallset s; + smallset_init(&s, (1 + boundaries[1] - boundaries[0]) * + (1 + boundaries[3] - boundaries[2])); size_t npos = 10; - int pos[20] = {0}; + int pos[20] = {0}; - size_t i,j; + size_t i, j; uint64_t jl; - for(i = 0; i < nlines; i++) { + for (i = 0; i < nlines; i++) { sread_next_u64(&jl, lines[i]); - for(j = 0; j < jl; j++) { + for (j = 0; j < jl; j++) { make_move(pos, npos, lines[i][0], &s, boundaries); } } printf("%llu\n", smallset_cardinality(&s)); - smallset_clear(&s); + smallset_clear(&s); } diff --git a/template/common.c b/template/common.c @@ -1,6 +1,3 @@ #include "common.h" -size_t fn(char** lines, size_t nlines) -{ - return 0; -} +size_t fn(char **lines, size_t nlines) { return 0; } diff --git a/template/common.h b/template/common.h @@ -1,11 +1,11 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> #include <reading.h> +#include <stdint.h> +#include <stdio.h> -size_t fn(char** lines, size_t nlines); +size_t fn(char **lines, size_t nlines); #endif diff --git a/template/uppga.c b/template/uppga.c @@ -1,10 +1,7 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - - } diff --git a/template/uppgb.c b/template/uppgb.c @@ -1,9 +1,7 @@ #include "common.h" -int main(int argc, char** argv) -{ - char** lines; +int main(int argc, char **argv) { + char **lines; size_t nlines = readlines(&lines, "input"); printf("#lines: %lu\n", nlines); - } diff --git a/utils/ht.c b/utils/ht.c @@ -1,25 +1,20 @@ #include "ht.h" -void ht_init_size(ht* t, const size_t alloc) -{ +void ht_init_size(ht *t, const size_t alloc) { t->nalloc = alloc; t->nelts = 0; t->entries = calloc(t->nalloc, sizeof(*t->entries)); size_t i; - for(i = 0; i < t->nalloc; i++) + for (i = 0; i < t->nalloc; i++) stack_sd_init_size(&t->entries[i], HT_DEFAULT_STACK_SIZE); } -void ht_init(ht* t) -{ - ht_init_size(t, HT_DEFAULT_SIZE); -} +void ht_init(ht *t) { ht_init_size(t, HT_DEFAULT_SIZE); } -void ht_clear(ht* t) -{ +void ht_clear(ht *t) { size_t i; - for(i = 0; i < t->nalloc; i++) + for (i = 0; i < t->nalloc; i++) stack_sd_clear(&t->entries[i]); t->nalloc = 0; @@ -28,16 +23,15 @@ void ht_clear(ht* t) t->entries = NULL; } -int ht_lookup(ht* t, sd d) -{ +int ht_lookup(ht *t, sd d) { uint64_t h = sd_hash(&d); size_t i = h % t->nalloc; return stack_sd_lookup(&t->entries[i], d); } -void ht_insert(ht* t, sd d) -{ - if(ht_lookup(t, d) != STACK_SD_LOOKUP_NOT_FOUND) return; +void ht_insert(ht *t, sd d) { + if (ht_lookup(t, d) != STACK_SD_LOOKUP_NOT_FOUND) + return; size_t i = sd_hash(&d) % t->nalloc; stack_sd_push(&t->entries[i], d); diff --git a/utils/ht.h b/utils/ht.h @@ -8,10 +8,10 @@ #define HT_LOOKUP_NOT_FOUND (STACK_SD_LOOKUP_NOT_FOUND) // == -1 typedef struct { - size_t nelts; - size_t nalloc; + size_t nelts; + size_t nalloc; - stack_sd* entries; + stack_sd *entries; } ht; /** @@ -23,7 +23,7 @@ typedef struct { * @param t pointer to the ht to be initialized * @param alloc the number of buckets to allocate for */ -void ht_init_size(ht* t, const size_t alloc); +void ht_init_size(ht *t, const size_t alloc); /** * Initialize a hash table. @@ -34,14 +34,14 @@ void ht_init_size(ht* t, const size_t alloc); * * @param t pointer to the th to initialized */ -void ht_init(ht* t); +void ht_init(ht *t); /** * Clear a stack. * * @param t the stack to clear */ -void ht_clear(ht* t); +void ht_clear(ht *t); /** * Lookup a sd in the hashtable. @@ -56,7 +56,7 @@ void ht_clear(ht* t); * @param t pointer to the hash table too look up in * @param d the sd to look up */ -int ht_lookup(ht* t, sd d); +int ht_lookup(ht *t, sd d); /** * Insert a as into the hashtable. @@ -68,6 +68,6 @@ int ht_lookup(ht* t, sd d); * @param t pointer to the hash table to insert into * @param d the sd to insert */ -void ht_insert(ht* t, sd d); +void ht_insert(ht *t, sd d); #endif diff --git a/utils/reading.c b/utils/reading.c @@ -1,112 +1,112 @@ #include "reading.h" -size_t readall(char** output, char* filename) -{ - FILE* f = NULL; - - f = fopen(filename, "r"); - - if(f == NULL) { - perror("Error when opening file"); - exit(EXIT_FAILURE); - } +size_t readall(char **output, char *filename) { + FILE *f = NULL; - char buffer[MAXIMUM_FILESIZE_BYTES]; - size_t read_size = fread(buffer, 1, MAXIMUM_FILESIZE_BYTES, f); + f = fopen(filename, "r"); - if(read_size == MAXIMUM_FILESIZE_BYTES) { - perror("File is to big?!"); - exit(EXIT_FAILURE); - } + if (f == NULL) { + perror("Error when opening file"); + exit(EXIT_FAILURE); + } - *output = (char*) malloc(read_size + 1); - memcpy(*output, buffer, read_size); - (*output)[read_size] = '\0'; + char buffer[MAXIMUM_FILESIZE_BYTES]; + size_t read_size = fread(buffer, 1, MAXIMUM_FILESIZE_BYTES, f); - return read_size; -} + if (read_size == MAXIMUM_FILESIZE_BYTES) { + perror("File is to big?!"); + exit(EXIT_FAILURE); + } -size_t readlines(char*** lines, char* filename) -{ - FILE* f = fopen(filename, "r"); + *output = (char *)malloc(read_size + 1); + memcpy(*output, buffer, read_size); + (*output)[read_size] = '\0'; - if(f == NULL) { - perror("Error when opening file"); - exit(EXIT_FAILURE); - } + return read_size; +} - size_t tl_nalloc = LINES_ALLOC_MIN; - size_t ntl = 0; - char** tl = calloc(tl_nalloc, sizeof(*tl)); +size_t readlines(char ***lines, char *filename) { + FILE *f = fopen(filename, "r"); - char line_buffer[MAXIMUM_LINE_BYTES]; - while(fgets(line_buffer, MAXIMUM_LINE_BYTES, f)) { - if(ntl == tl_nalloc) { - tl_nalloc <<= 1; - tl = realloc(tl, tl_nalloc * sizeof(*tl)); - } + if (f == NULL) { + perror("Error when opening file"); + exit(EXIT_FAILURE); + } - tl[ntl] = strdup(line_buffer); + size_t tl_nalloc = LINES_ALLOC_MIN; + size_t ntl = 0; + char **tl = calloc(tl_nalloc, sizeof(*tl)); - ntl++; + char line_buffer[MAXIMUM_LINE_BYTES]; + while (fgets(line_buffer, MAXIMUM_LINE_BYTES, f)) { + if (ntl == tl_nalloc) { + tl_nalloc <<= 1; + tl = realloc(tl, tl_nalloc * sizeof(*tl)); } - tl = realloc(tl, ntl * sizeof(*tl)); - *lines = tl; + tl[ntl] = strdup(line_buffer); + + ntl++; + } - return(ntl); + tl = realloc(tl, ntl * sizeof(*tl)); + *lines = tl; + + return (ntl); } -int read_next_u64(uint64_t* n, FILE* fp) -{ - int r,c=0; - - // skip to next numerical char - while((r = fgetc(fp)) != EOF) { - if((int)'0' <= r && r <= (int)'9') break; - } - if(r == EOF) return EOF; - - // read an unsigned int - *n = (uint64_t)(r - (int)'0'); - c++; - while((r = fgetc(fp)) != EOF) { - if((int)'0' <= r && r <= (int)'9') { - *n = (*n) * 10UL + (uint64_t)(r - (int)'0'); - c++; - } else { - break; - } - } - - return c; +int read_next_u64(uint64_t *n, FILE *fp) { + int r, c = 0; + + // skip to next numerical char + while ((r = fgetc(fp)) != EOF) { + if ((int)'0' <= r && r <= (int)'9') + break; + } + if (r == EOF) + return EOF; + + // read an unsigned int + *n = (uint64_t)(r - (int)'0'); + c++; + while ((r = fgetc(fp)) != EOF) { + if ((int)'0' <= r && r <= (int)'9') { + *n = (*n) * 10UL + (uint64_t)(r - (int)'0'); + c++; + } else { + break; + } + } + + return c; } +char *sread_next_u64(uint64_t *n, char *s) { + char *fc = s; + + // skip to next numerical char + while (*fc != '\0') { + if ('0' <= *fc && *fc <= '9') + break; + fc++; + } + if (*fc == '\0') + return NULL; + + // read an unsigned int + *n = (uint64_t)(*fc - '0'); + fc++; + while (*fc != EOF) { + if ('0' <= *fc && *fc <= '9') { + *n = (*n) * 10UL + (uint64_t)(*fc - '0'); + } else { + break; + } + fc++; + } + + if (*fc == '\0') + return NULL; -char* sread_next_u64(uint64_t* n, char* s) -{ - char* fc = s; - - // skip to next numerical char - while(*fc != '\0') { - if('0' <= *fc && *fc <= '9') break; - fc++; - } - if(*fc == '\0') return NULL; - - // read an unsigned int - *n = (uint64_t)(*fc - '0'); - fc++; - while(*fc != EOF) { - if('0' <= *fc && *fc <= '9') { - *n = (*n) * 10UL + (uint64_t)(*fc - '0'); - } else { - break; - } - fc++; - } - - if(*fc == '\0') return NULL; - - return fc; + return fc; } diff --git a/utils/reading.h b/utils/reading.h @@ -1,10 +1,10 @@ #ifndef READING_H #define READING_H +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <stdint.h> #define MAXIMUM_FILESIZE_BYTES 256000 #define MAXIMUM_LINE_BYTES 256000 @@ -12,30 +12,30 @@ /** * Read all chars from a file. - * + * * Reads an entire file (assuming its size is < MAXIMUM_FILESIZE_BYTES). * This allocates memory that needs to be free'd. - * + * * @param output pointer to the address where the allocated string * should be written * @param filename the name of the file * @return the number of bytes read (excluding final null byte) */ -size_t readall(char** output, char* filename); +size_t readall(char **output, char *filename); /** * Read all lines from a file. - * + * * Reads a file, line-by-line (assuming no line is longer than * MAXIMUM_LINE_BYTES). * Allocates memory for each line that needs to be free'd. - * + * * @param lines pointer to the address where the list of pointers * should be written * @param filename the name of the file * @return the number of lines read */ -size_t readlines(char*** lines, char* filename); +size_t readlines(char ***lines, char *filename); /** * Read the next uint64_t from FILE ptr. @@ -48,7 +48,7 @@ size_t readlines(char*** lines, char* filename); * @param fp file pointer to read from * @returns the number of characters read as part of u64 */ -int read_next_u64(uint64_t* n, FILE* fp); +int read_next_u64(uint64_t *n, FILE *fp); /** * Read the next uin64_t from string. @@ -62,7 +62,6 @@ int read_next_u64(uint64_t* n, FILE* fp); * @param s the string * @returns pointer to next char in string, or NULL */ -char* sread_next_u64(uint64_t* n, char* s); - +char *sread_next_u64(uint64_t *n, char *s); #endif diff --git a/utils/sd.c b/utils/sd.c @@ -1,15 +1,13 @@ #include "sd.h" -void sd_init(sd* s, const size_t size) -{ - s->size = size; - s->data = malloc(s->size); +void sd_init(sd *s, const size_t size) { + s->size = size; + s->data = malloc(s->size); } -void sd_clear(sd* s) -{ - s->size = 0; - free(s->data); +void sd_clear(sd *s) { + s->size = 0; + free(s->data); } static inline uint64_t murmur_u64(uint64_t h) { @@ -21,50 +19,42 @@ static inline uint64_t murmur_u64(uint64_t h) { return h; } -uint64_t sd_hash(sd* s) -{ - uint64_t tmp = s->size; - - size_t i; - for(i = 0; i < s->size; i++) { - tmp ^= ((uint64_t) s->data[i]) << (i % 4); - } +uint64_t sd_hash(sd *s) { + uint64_t tmp = s->size; - return murmur_u64(tmp); + size_t i; + for (i = 0; i < s->size; i++) { + tmp ^= ((uint64_t)s->data[i]) << (i % 4); + } + + return murmur_u64(tmp); } -int sd_cmp(sd* x, sd* y) -{ - if(x->size < y->size) { - return -1; - } else if(x->size > y->size) { - return 1; - } +int sd_cmp(sd *x, sd *y) { + if (x->size < y->size) { + return -1; + } else if (x->size > y->size) { + return 1; + } - return memcmp(x->data, y->data, x->size); + return memcmp(x->data, y->data, x->size); } -void sd_init_u64(sd* s, const uint64_t x) -{ - sd_init(s, sizeof(x)); - memcpy(s->data, &x, sizeof(x)); +void sd_init_u64(sd *s, const uint64_t x) { + sd_init(s, sizeof(x)); + memcpy(s->data, &x, sizeof(x)); } -uint64_t sd_conv_u64(sd* s) -{ - uint64_t r; - memcpy(&r, s->data, sizeof(r)); - return r; +uint64_t sd_conv_u64(sd *s) { + uint64_t r; + memcpy(&r, s->data, sizeof(r)); + return r; } -void sd_init_str(sd* s, const char* str) -{ - size_t len = strlen(str)+1; - sd_init(s, sizeof(*str) * len); - memcpy(s->data, str, len); +void sd_init_str(sd *s, const char *str) { + size_t len = strlen(str) + 1; + sd_init(s, sizeof(*str) * len); + memcpy(s->data, str, len); } -char* sd_get_str(sd* s) -{ - return (char*) s->data; -} -\ No newline at end of file +char *sd_get_str(sd *s) { return (char *)s->data; } +\ No newline at end of file diff --git a/utils/sd.h b/utils/sd.h @@ -1,103 +1,103 @@ #ifndef SD_H #define SD_H -#include <stdlib.h> #include <stdint.h> +#include <stdlib.h> #include <string.h> typedef struct { - size_t size; - uint8_t* data; + size_t size; + uint8_t *data; } sd; /** * Initialise a sized data. - * + * * Allocates memory that needs to be sd_clear'd. - * + * * @param s pointer where to write the sized data obj * @param size number of bytes in the sd */ -void sd_init(sd* s, const size_t size); +void sd_init(sd *s, const size_t size); /** * Clear a sd. - * + * * @param s pointer to the sd */ -void sd_clear(sd* s); +void sd_clear(sd *s); /** * Computes a hash of a sd. - * + * * This hash is a simple combination of xoring * in all the bytes to a uint64_t in different * positions, then computing the mix64 function * known as murmur64. - * + * * @param s the sd to get hash of * @return hash of s */ -uint64_t sd_hash(sd* s); +uint64_t sd_hash(sd *s); /** * Compare two sds. - * + * * Compares the sd's pointed to by x and y. In the * following sense: a < b if either a.size < b.size * or a.data < b.data (in the memcmp order). If * the sd are equal then 0 is returned. - * + * * @param x pointer to first sd * @param y pointer to second sd * @return comparison number (lex order) */ -int sd_cmp(sd* x, sd* y); +int sd_cmp(sd *x, sd *y); /** * Initialises a sd from a uint64_t. - * + * * The size will be 8 bytes, and the order of the * bytes is raw (host order). Allocates memory that * needs to be sd_clear'd. - * + * * @param s ptr to the sd to initialise * @param x the uint64_t to initialize from */ -void sd_init_u64(sd* s, const uint64_t x); +void sd_init_u64(sd *s, const uint64_t x); /** * Converts a sd to a uint64_t. - * + * * Gets a uint64_t from a sd that has been created * by sd_init_u64. - * + * * @param s ptr to the sd to convert * @return the uint64_t */ -uint64_t sd_conv_u64(sd* s); +uint64_t sd_conv_u64(sd *s); /** * Initializes a sd from a string. - * + * * Copies a string into a sd, including the * terminating null byte. Allocates memory that * needs to be sd_clear'd. - * + * * @param s ptr to the sd to be init'd * @param str the string to copy into new sd */ -void sd_init_str(sd* s, const char* str); +void sd_init_str(sd *s, const char *str); /** * Get string stored in sd. - * + * * This returns a char* to the data stored in the * sd. This assumes that the data is in fact a * null terminated string. - * + * * @param s ptr to the sd to get string from */ -char* sd_get_str(sd* s); +char *sd_get_str(sd *s); #endif \ No newline at end of file diff --git a/utils/smallset.c b/utils/smallset.c @@ -1,97 +1,90 @@ #include "smallset.h" -#define BIDX(x) (x/8) +#define BIDX(x) (x / 8) #define BMSK(x) (0x80 >> (x % 8)) #define SIZE(b) ((b + 7) >> 3) -void smallset_init(smallset* s, const size_t bits) -{ - s->bits = bits; - s->data = calloc(SIZE(bits), sizeof(*s->data)); - assert(sizeof(*s->data) == 1); +void smallset_init(smallset *s, const size_t bits) { + s->bits = bits; + s->data = calloc(SIZE(bits), sizeof(*s->data)); + assert(sizeof(*s->data) == 1); } -void smallset_clear(smallset* s) -{ - s->bits = 0; - free(s->data); +void smallset_clear(smallset *s) { + s->bits = 0; + free(s->data); } -void smallset_insert(smallset* s, const uint64_t x) -{ - assert(x < s->bits); - s->data[BIDX(x)] |= BMSK(x); +void smallset_insert(smallset *s, const uint64_t x) { + assert(x < s->bits); + s->data[BIDX(x)] |= BMSK(x); } -int smallset_lookup(smallset* s, const uint64_t x) -{ - assert(x < s->bits); - return s->data[BIDX(x)] & BMSK(x); +int smallset_lookup(smallset *s, const uint64_t x) { + assert(x < s->bits); + return s->data[BIDX(x)] & BMSK(x); } -void smallset_intersection(smallset* out, smallset* a, smallset* b) -{ - assert(a->bits == b->bits); - assert(out->bits == a->bits); +void smallset_intersection(smallset *out, smallset *a, smallset *b) { + assert(a->bits == b->bits); + assert(out->bits == a->bits); - size_t i; - for(i = 0; i < SIZE(out->bits); i++) { - out->data[i] = a->data[i] & b->data[i]; - } + size_t i; + for (i = 0; i < SIZE(out->bits); i++) { + out->data[i] = a->data[i] & b->data[i]; + } } -void smallset_tonstr(char* str, size_t n, smallset* s) -{ - int first = 1; - int u = snprintf(str, n, "{"); - - size_t i; - for(i = 0; i < s->bits; i++) { - if(smallset_lookup(s, i)) { - if(!first) { - u += snprintf(&str[u], n-u, ", %zu", i); - } else { - u += snprintf(&str[u], n-u, "%zu", i); - first = !first; - } - } - } - - snprintf(&str[u], n-u, "}"); +void smallset_tonstr(char *str, size_t n, smallset *s) { + int first = 1; + int u = snprintf(str, n, "{"); + + size_t i; + for (i = 0; i < s->bits; i++) { + if (smallset_lookup(s, i)) { + if (!first) { + u += snprintf(&str[u], n - u, ", %zu", i); + } else { + u += snprintf(&str[u], n - u, "%zu", i); + first = !first; + } + } + } + + snprintf(&str[u], n - u, "}"); } -uint64_t smallset_getone(smallset* s) -{ +uint64_t smallset_getone(smallset *s) { uint64_t i; - for(i = 0; i < s->bits; i++) { - if(smallset_lookup(s, i)) return i; - } + for (i = 0; i < s->bits; i++) { + if (smallset_lookup(s, i)) + return i; + } - return SMALLSET_IS_EMPTY; + return SMALLSET_IS_EMPTY; } -void smallset_empty(smallset* s) -{ - memset(s->data, 0, SIZE(s->bits)); -} +void smallset_empty(smallset *s) { memset(s->data, 0, SIZE(s->bits)); } -uint64_t smallset_cardinality(smallset* s) -{ +uint64_t smallset_cardinality(smallset *s) { uint64_t a; uint64_t r = 0; size_t i; - for(i = 0; i < SIZE(s->bits); i += 8) { - __asm__ volatile ("POPCNT %1, %0" :"=r"(a) :"r"(*((uint64_t*) &s->data[i])) :); + for (i = 0; i < SIZE(s->bits); i += 8) { + __asm__ volatile("POPCNT %1, %0" + : "=r"(a) + : "r"(*((uint64_t *)&s->data[i])) + :); r += a; } - uint64_t tmp = 0; - while(i < SIZE(s->bits)) { - tmp = (tmp << 8) ^ ((uint64_t) s->data[i]); - i++; - } - __asm__ volatile ("POPCNT %1, %0" :"=r"(a) :"r"(tmp) :); - r += a; + uint64_t tmp = 0; + while (i < SIZE(s->bits)) { + tmp = (tmp << 8) ^ ((uint64_t)s->data[i]); + i++; + } + __asm__ volatile("POPCNT %1, %0" : "=r"(a) : "r"(tmp) :); + r += a; return r; } diff --git a/utils/smallset.h b/utils/smallset.h @@ -4,17 +4,17 @@ #ifndef SMALLSET_H_ #define SMALLSET_H_ -#include <stdio.h> -#include <stdint.h> #include <assert.h> +#include <stdint.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #define SMALLSET_IS_EMPTY UINT64_MAX typedef struct { - size_t bits; - uint8_t* data; + size_t bits; + uint8_t *data; } smallset; /** @@ -26,14 +26,14 @@ typedef struct { * @param s pointer to the smallset to be inited * @param bits specifies that the set is a subset of [bits] */ -void smallset_init(smallset* s, const size_t bits); +void smallset_init(smallset *s, const size_t bits); /** * Clears a set. * * @param s pointer to the smallset to clear */ -void smallset_clear(smallset* s); +void smallset_clear(smallset *s); /** * Insert an element into a set. @@ -45,7 +45,7 @@ void smallset_clear(smallset* s); * @param s pointer to the set to insert into * @param x element to insert into the set */ -void smallset_insert(smallset* s, const uint64_t x); +void smallset_insert(smallset *s, const uint64_t x); /** * Check if an element is in a set. @@ -57,7 +57,7 @@ void smallset_insert(smallset* s, const uint64_t x); * @param x the element to check for * @returns a nonzero value iff x is in the set */ -int smallset_lookup(smallset* s, const uint64_t x); +int smallset_lookup(smallset *s, const uint64_t x); /** * Compute the intersection of two sets. @@ -70,7 +70,7 @@ int smallset_lookup(smallset* s, const uint64_t x); * @param a pointer to one of the intersect sets * @param b pointer to the other intersect set */ -void smallset_intersection(smallset* out, smallset* a, smallset* b); +void smallset_intersection(smallset *out, smallset *a, smallset *b); /** * Writes the smallset to a string. @@ -86,7 +86,7 @@ void smallset_intersection(smallset* out, smallset* a, smallset* b); * @param n limit on the number of bytes to be written * @param s pointer to the smallset */ -void smallset_tonstr(char* str, size_t n, smallset* s); +void smallset_tonstr(char *str, size_t n, smallset *s); /** * Get an element that is in the set. @@ -97,7 +97,7 @@ void smallset_tonstr(char* str, size_t n, smallset* s); * * @param s pointer to the smallset to get elem from */ -uint64_t smallset_getone(smallset* s); +uint64_t smallset_getone(smallset *s); /** * Empty a set. @@ -106,7 +106,7 @@ uint64_t smallset_getone(smallset* s); * * @param s pointer to the smallset to empty */ -void smallset_empty(smallset* s); +void smallset_empty(smallset *s); /** * Get the cardinality of a set. @@ -114,6 +114,6 @@ void smallset_empty(smallset* s); * @param s the set to get the cardinality of * @return the cardinality */ -uint64_t smallset_cardinality(smallset* s); +uint64_t smallset_cardinality(smallset *s); #endif diff --git a/utils/stack_sd.c b/utils/stack_sd.c @@ -1,63 +1,53 @@ #include "stack_sd.h" -void stack_sd_init(stack_sd* d) -{ - d->nmemb = 0; - d->alloc = BASE_STACK_SD_SIZE; +void stack_sd_init(stack_sd *d) { + d->nmemb = 0; + d->alloc = BASE_STACK_SD_SIZE; - d->data = calloc(d->alloc, sizeof(*(d->data))); + d->data = calloc(d->alloc, sizeof(*(d->data))); } -void stack_sd_init_size(stack_sd* d, const size_t size) -{ - d->nmemb = 0; - d->alloc = size; +void stack_sd_init_size(stack_sd *d, const size_t size) { + d->nmemb = 0; + d->alloc = size; - d->data = calloc(d->alloc, sizeof(*(d->data))); + d->data = calloc(d->alloc, sizeof(*(d->data))); } -void stack_sd_clear(stack_sd* d) -{ - d->nmemb = 0; - free(d->data); - d->data = NULL; +void stack_sd_clear(stack_sd *d) { + d->nmemb = 0; + free(d->data); + d->data = NULL; } -void stack_sd_push(stack_sd* d, sd x) -{ - if(d->alloc <= d->nmemb) { - d->alloc <<= 1; - d->data = realloc(d->data, d->alloc * sizeof(*(d->data))); - } +void stack_sd_push(stack_sd *d, sd x) { + if (d->alloc <= d->nmemb) { + d->alloc <<= 1; + d->data = realloc(d->data, d->alloc * sizeof(*(d->data))); + } - d->data[d->nmemb] = x; - d->nmemb++; + d->data[d->nmemb] = x; + d->nmemb++; } -sd stack_sd_get(const stack_sd* d, const size_t i) -{ - return d->data[i]; -} +sd stack_sd_get(const stack_sd *d, const size_t i) { return d->data[i]; } -sd stack_sd_getlast(const stack_sd* d) -{ - return stack_sd_get(d, d->nmemb-1); -} +sd stack_sd_getlast(const stack_sd *d) { return stack_sd_get(d, d->nmemb - 1); } -sd stack_sd_pop(stack_sd* d) -{ - if(d->nmemb == 0) return STACK_SD_EMPTY_POP; +sd stack_sd_pop(stack_sd *d) { + if (d->nmemb == 0) + return STACK_SD_EMPTY_POP; - sd r = stack_sd_getlast(d); - d->nmemb--; - return r; + sd r = stack_sd_getlast(d); + d->nmemb--; + return r; } -int stack_sd_lookup(stack_sd* d, sd s) -{ - int i; - for(i = 0; i < d->nmemb; i++) { - if(sd_cmp(&d->data[i], &s) == 0) return i; - } - return STACK_SD_LOOKUP_NOT_FOUND; +int stack_sd_lookup(stack_sd *d, sd s) { + int i; + for (i = 0; i < d->nmemb; i++) { + if (sd_cmp(&d->data[i], &s) == 0) + return i; + } + return STACK_SD_LOOKUP_NOT_FOUND; } diff --git a/utils/stack_sd.h b/utils/stack_sd.h @@ -1,86 +1,86 @@ #ifndef STACK_SD_H #define STACK_SD_H -#include <stdlib.h> -#include <stdint.h> #include "sd.h" +#include <stdint.h> +#include <stdlib.h> #define BASE_STACK_SD_SIZE 256 #define STACK_SD_LOOKUP_NOT_FOUND (-1) -#define STACK_SD_EMPTY_POP ((sd) {.size = 0, .data = NULL}) +#define STACK_SD_EMPTY_POP ((sd){.size = 0, .data = NULL}) typedef struct { - size_t nmemb; - size_t alloc; + size_t nmemb; + size_t alloc; - sd* data; + sd *data; } stack_sd; /** * Initialization of stack. - * + * * Initialises memory for the stack. Use clear. - * + * * @param d pointer to the address where to write the new stack */ -void stack_sd_init(stack_sd* d); +void stack_sd_init(stack_sd *d); /** * Initialization of stack, with size. - * + * * Initialises memory for the stack. Use clear. - * + * * @param d pointer to the address where to write the new stack * @param size initial size of stack alloc */ -void stack_sd_init_size(stack_sd* d, const size_t size); +void stack_sd_init_size(stack_sd *d, const size_t size); /** * Clear a stack - * + * * @param d pointer to the stack to clear */ -void stack_sd_clear(stack_sd* d); +void stack_sd_clear(stack_sd *d); /** * Pushes a sd pointer onto the stack. - * + * * Note that this does not copy the sd data, it just pushes the sd * struct including the pointer to the stack. - * + * * @param d pointer to the stack * @param x the sd to push */ -void stack_sd_push(stack_sd* d, sd x); +void stack_sd_push(stack_sd *d, sd x); /** * Gets the element at index i in the stack. - * + * * @param d pointer to the stack * @param i the index to get element at * @return the value of the sd at index i */ -sd stack_sd_get(const stack_sd* d, const size_t i); +sd stack_sd_get(const stack_sd *d, const size_t i); /** * Get the last (top) element of a stack. - * + * * @param d pointer to the stack * @return the value of the sd at the top */ -sd stack_sd_getlast(const stack_sd* d); +sd stack_sd_getlast(const stack_sd *d); /** * Pop an element of the stack. - * + * * Note that popping never decreases the amount of memory allocated. * If memory is an issue the stacks have to be destroyed and replaced. * Returns STACK_SD_POP_EMPY if stack is empty. - * + * * @param d pointer to the stack * @return the value of the popped sd */ -sd stack_sd_pop(stack_sd* d); +sd stack_sd_pop(stack_sd *d); /** * Lookup a sd on a stack. @@ -92,6 +92,6 @@ sd stack_sd_pop(stack_sd* d); * @param d the stack to lookup in * @param s the sd to look for in the stack */ -int stack_sd_lookup(stack_sd* d, sd s); +int stack_sd_lookup(stack_sd *d, sd s); #endif diff --git a/utils/stack_str.c b/utils/stack_str.c @@ -1,46 +1,38 @@ #include "stack_str.h" -void stack_str_init(stack_str* d) -{ - d->nmemb = 0; - d->alloc = BASE_STACK_STR_SIZE; +void stack_str_init(stack_str *d) { + d->nmemb = 0; + d->alloc = BASE_STACK_STR_SIZE; - d->data = calloc(d->alloc, sizeof(*(d->data))); + d->data = calloc(d->alloc, sizeof(*(d->data))); } -void stack_str_clear(stack_str* d) -{ - d->nmemb = 0; - free(d->data); - d->data = NULL; +void stack_str_clear(stack_str *d) { + d->nmemb = 0; + free(d->data); + d->data = NULL; } -void stack_str_push(stack_str* d, char* x) -{ - if(d->alloc <= d->nmemb) { - d->alloc <<= 1; - d->data = realloc(d->data, d->alloc * sizeof(*(d->data))); - } +void stack_str_push(stack_str *d, char *x) { + if (d->alloc <= d->nmemb) { + d->alloc <<= 1; + d->data = realloc(d->data, d->alloc * sizeof(*(d->data))); + } - d->data[d->nmemb] = x; - d->nmemb++; + d->data[d->nmemb] = x; + d->nmemb++; } -char* stack_str_get(const stack_str* d, const size_t i) -{ - return d->data[i]; -} +char *stack_str_get(const stack_str *d, const size_t i) { return d->data[i]; } -char* stack_str_getlast(const stack_str* d) -{ - return stack_str_get(d, d->nmemb-1); +char *stack_str_getlast(const stack_str *d) { + return stack_str_get(d, d->nmemb - 1); } -char* stack_str_pop(stack_str* d) -{ - if(d->nmemb == 0) - return STACK_STR_EMPTY_POP; - char* r = stack_str_getlast(d); - d->nmemb--; - return r; +char *stack_str_pop(stack_str *d) { + if (d->nmemb == 0) + return STACK_STR_EMPTY_POP; + char *r = stack_str_getlast(d); + d->nmemb--; + return r; } \ No newline at end of file diff --git a/utils/stack_str.h b/utils/stack_str.h @@ -1,69 +1,70 @@ #ifndef STACK_STR_H #define STACK_STR_H -#include <stdlib.h> #include <stdint.h> +#include <stdlib.h> #define BASE_STACK_STR_SIZE 256 #define STACK_STR_EMPTY_POP (NULL) typedef struct { - size_t nmemb; - size_t alloc; + size_t nmemb; + size_t alloc; - char** data; + char **data; } stack_str; /** * Initialization of stack. - * + * * Initialises memory for the stack. Use clear. - * + * * @param d pointer to the address where to write the new stack */ -void stack_str_init(stack_str* d); +void stack_str_init(stack_str *d); /** * Clear a stack - * + * * @param d pointer to the stack to clear */ -void stack_str_clear(stack_str* d); +void stack_str_clear(stack_str *d); /** * Pushes a string (char pointer) onto the stack. - * - * Note that this does not copy the string, it just pushes the pointer to the stack. - * + * + * Note that this does not copy the string, it just pushes the pointer to the + * stack. + * * @param d pointer to the stack * @param x the string (char pointer) to push */ -void stack_str_push(stack_str* d, char* x); +void stack_str_push(stack_str *d, char *x); /** * Gets the element at index i in the stack. - * + * * @param d pointer to the stack * @param i the index to get element at * @return the value (the char pointer) of the element at index i */ -char* stack_str_get(const stack_str* d, const size_t i); +char *stack_str_get(const stack_str *d, const size_t i); /** * Get the last (top) element of a stack. - * + * * @param d pointer to the stack */ -char* stack_str_getlast(const stack_str* d); +char *stack_str_getlast(const stack_str *d); /** * Pop an element of the stack. - * + * * Note that popping never decreases the amount of memory allocated. * If memory is an issue the stacks have to be destroyed and replaced. - * + * * @param d pointer to the stack */ -char* stack_str_pop(stack_str* d); +char *stack_str_pop(stack_str *d); #endif \ No newline at end of file diff --git a/utils/stack_u64.c b/utils/stack_u64.c @@ -1,46 +1,40 @@ #include "stack_u64.h" -void stack_u64_init(stack_u64* d) -{ - d->nmemb = 0; - d->alloc = BASE_STACK_SIZE; +void stack_u64_init(stack_u64 *d) { + d->nmemb = 0; + d->alloc = BASE_STACK_SIZE; - d->data = calloc(d->alloc, sizeof(*(d->data))); + d->data = calloc(d->alloc, sizeof(*(d->data))); } -void stack_u64_clear(stack_u64* d) -{ - d->nmemb = 0; - free(d->data); - d->data = NULL; +void stack_u64_clear(stack_u64 *d) { + d->nmemb = 0; + free(d->data); + d->data = NULL; } -void stack_u64_push(stack_u64* d, const uint64_t x) -{ - if(d->alloc <= d->nmemb) { - d->alloc <<= 1; - d->data = realloc(d->data, d->alloc * sizeof(*(d->data))); - } +void stack_u64_push(stack_u64 *d, const uint64_t x) { + if (d->alloc <= d->nmemb) { + d->alloc <<= 1; + d->data = realloc(d->data, d->alloc * sizeof(*(d->data))); + } - d->data[d->nmemb] = x; - d->nmemb++; + d->data[d->nmemb] = x; + d->nmemb++; } -uint64_t stack_u64_get(const stack_u64* d, const size_t i) -{ - return d->data[i]; +uint64_t stack_u64_get(const stack_u64 *d, const size_t i) { + return d->data[i]; } -uint64_t stack_u64_getlast(const stack_u64* d) -{ - return stack_u64_get(d, d->nmemb-1); +uint64_t stack_u64_getlast(const stack_u64 *d) { + return stack_u64_get(d, d->nmemb - 1); } -uint64_t stack_u64_pop(stack_u64* d) -{ - if(d->nmemb == 0) - return STACK_U64_EMPTY_POP; - uint64_t r = stack_u64_getlast(d); - d->nmemb--; - return r; +uint64_t stack_u64_pop(stack_u64 *d) { + if (d->nmemb == 0) + return STACK_U64_EMPTY_POP; + uint64_t r = stack_u64_getlast(d); + d->nmemb--; + return r; } \ No newline at end of file diff --git a/utils/stack_u64.h b/utils/stack_u64.h @@ -1,67 +1,67 @@ #ifndef STACK_U64_H #define STACK_U64_H -#include <stdlib.h> #include <stdint.h> +#include <stdlib.h> #define BASE_STACK_SIZE 256 #define STACK_U64_EMPTY_POP UINT64_MAX typedef struct { - size_t nmemb; - size_t alloc; + size_t nmemb; + size_t alloc; - uint64_t* data; + uint64_t *data; } stack_u64; /** * Initialization of stack. - * + * * Initialises memory for the stack. Use clear. - * + * * @param d pointer to the address where to write the new stack */ -void stack_u64_init(stack_u64* d); +void stack_u64_init(stack_u64 *d); /** * Clear a stack - * + * * @param d pointer to the stack to clear */ -void stack_u64_clear(stack_u64* d); +void stack_u64_clear(stack_u64 *d); /** * Pushes a uint64_t onto the stack. - * + * * @param d pointer to the stack * @param x the uint64_t to push */ -void stack_u64_push(stack_u64* d, const uint64_t x); +void stack_u64_push(stack_u64 *d, const uint64_t x); /** * Gets the element at index i in the stack. - * + * * @param d pointer to the stack * @param i the index to get element at * @return the value of the element at index i */ -uint64_t stack_u64_get(const stack_u64* d, const size_t i); +uint64_t stack_u64_get(const stack_u64 *d, const size_t i); /** * Get the last (top) element of a stack. - * + * * @param d pointer to the stack */ -uint64_t stack_u64_getlast(const stack_u64* d); +uint64_t stack_u64_getlast(const stack_u64 *d); /** * Pop an element of the stack. - * + * * Note that popping never decreases the amount of memory allocated. * If memory is an issue the stacks have to be destroyed and replaced. - * + * * @param d pointer to the stack */ -uint64_t stack_u64_pop(stack_u64* d); +uint64_t stack_u64_pop(stack_u64 *d); #endif \ No newline at end of file