From 24c28577f55e83e17a7f488c2dc7d4454a6d8348 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 5 Dec 2020 13:37:21 +0000 Subject: [PATCH] day5 --- Makefile | 12 +++-- day5/Makefile | 41 ++++++++++++++++ day5/day5.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+), 5 deletions(-) create mode 100644 day5/Makefile create mode 100644 day5/day5.c diff --git a/Makefile b/Makefile index afd1163..f2852bf 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -COMMON_OPT_FLAGS?= -O3 -march=native -pipe -flto \ +COMMON_OPT_FLAGS?= -DFROM_MAIN -O3 -march=native -pipe -flto \ -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ -fno-stack-check -fno-strict-aliasing @@ -9,18 +9,20 @@ LD_OPT_FLAGS?=-O3 -flto COMMON_FLAGS=-Wall -pedantic $(COMMON_OPT_FLAGS) -CFLAGS?=$(COMMMON_FLAGS) --std=gnu11 $(C_OPT_FLAGS) -CXXFLAGS?=$(COMMON_FLAGS) --std=gnu++20 $(CXX_OPT_FLAGS) -LDFLAGS?=$(LD_OPT_FLAGS) +CFLAGS+=$(COMMON_FLAGS) --std=gnu11 $(C_OPT_FLAGS) +CXXFLAGS+=$(COMMON_FLAGS) --std=gnu++20 $(CXX_OPT_FLAGS) +LDFLAGS+=$(LD_OPT_FLAGS) DAYS= $(wildcard day*) +ENV= CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" + .PHONY: all all: $(addsuffix /part2,$(DAYS)) day%/part2: day% - cd $< && $(MAKE) + cd $< && $(ENV) $(MAKE) clean: for d in $(DAYS); do pushd $$d && $(MAKE) clean && popd; done diff --git a/day5/Makefile b/day5/Makefile new file mode 100644 index 0000000..32a9fdf --- /dev/null +++ b/day5/Makefile @@ -0,0 +1,41 @@ +SRC:= day5.c + +OPT_FLAGS?= -O3 -march=native -pipe -flto \ + -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ + -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ + -fno-stack-check -fno-strict-aliasing + +CFLAGS?=$(OPT_FLAGS) +LDFLAGS?=-O3 -flto + +CFLAGS+= -Wall -pedantic --std=gnu11 + +.PHONY: all +all: part1 part2 + +.PHONY: test +test: part1-test part2-test + +inpu%.h: inpu% + @rm -f $@ + while read line; do \ + echo "\"$$line\"," >> $@; \ + done < $< + +part1-test: $(SRC) | input-test.h + $(CC) $^ -DTEST -DDEBUG $(CFLAGS) -o $@ $(LDFLAGS) + +part2-test: $(SRC) | input-test.h + $(CC) $^ -DTEST -DDEBUG -DPART2 $(CFLAGS) -o $@ $(LDFLAGS) + +part1: $(SRC) | input.h + $(CC) $^ $(CFLAGS) -o $@ $(LDFLAGS) + strip $@ + +part2: $(SRC) | input.h + $(CC) $^ -DPART2 $(CFLAGS) -o $@ $(LDFLAGS) + strip $@ + +clean: + rm -f part{1,2}{,-test} + rm -f input{,-test}.h diff --git a/day5/day5.c b/day5/day5.c new file mode 100644 index 0000000..1090845 --- /dev/null +++ b/day5/day5.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include + +#define _cold __attribute__((cold, noinline)) +#define noreturn __attribute__((noreturn)) void + +#ifdef DEBUG +#define dlog(...) fprintf(stderr, "[debug]" __VA_ARGS__) +#else +inline static void do_nothing(int _n, ...) {} +#define dlog(...) do { if(0) { do_nothing(0 __VA_OPT__(,) __VA_ARGS__); } } while(0) +#endif + +noreturn static _cold panic(const char* msg) +{ + fputs(msg, stderr); + fputc('\n', stderr); + abort(); +} + +typedef struct sloc { + int row; + int column; + + int id; +} seat_t; + +static const char* input[] = { +#ifdef TEST +#include "input-test.h" +#else +#include "input.h" +#endif +}; + +#define input_sz (sizeof(input) / sizeof(char*)) + + +#define ROW_MAX 128 +#define COL_MAX 8 + +inline static void seat_calc_id(seat_t* restrict s) +{ + s->id = (s->row * 8) + s->column; +} + + +static int sbsearch(const char* direct, size_t len, int max) +{ + int min = 0; +#define diff (1 + (max - min)) + for(size_t i=0;iid < s2->id ? -1 + : s1->id > s2->id ? 1 + : 0; +} +#endif + + +int main() +{ + int max=0; + seat_t seats[input_sz]; + for(size_t i=0;i { .row = %d, .column = %d, .id = %d }\n", seat.row, seat.column, seat.id); + if(id>max) max = id; + } +#ifdef PART2 + qsort(seats, input_sz, sizeof(seat_t), &_seat_cmp); + for(size_t i=1; i