master
Avril 3 years ago
parent 9a5f80174f
commit 24c28577f5
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -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

@ -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

@ -0,0 +1,132 @@
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#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;i<len;i++)
{
dlog("%c: min = %d, max = %d\n", direct[i], min, max);
switch(direct[i])
{
case 0:
panic("string overflow");
// lower half
case 'F':
case 'L':
max -= diff / 2;
if(diff==1) return min;
break;
// upper half
case 'R':
case 'B':
min += diff / 2;
if(diff==1) return max;
break;
}
}
panic("bsearch failed");
#undef diff
}
static seat_t pbsearch(const char* str)
{
seat_t seat = {
.row = (int)sbsearch(str, 7, ROW_MAX-1),
.column = (int)sbsearch(str+7, 3, COL_MAX-1),
};
seat_calc_id(&seat);
return seat;
}
#ifdef PART2
static int _seat_cmp(const void* ptr, const void* ptr2)
{
const seat_t *s1 = ptr;
const seat_t *s2 = ptr2;
return s1->id < 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<input_sz;i++)
{
dlog("Testing %s\n", input[i]);
seat_t seat = seats[i] = pbsearch(input[i]);
int id = seat.id;
dlog(" -> { .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<input_sz;i++)
{
seat_t prev = seats[i-1];
seat_t this = seats[i];
#define diff (this.id - prev.id)
if(diff == 2)
{
printf("%d\n", prev.id+1);
return 0;
}
}
fprintf(stderr, "Failed to find id\n");
return 1;
#else
printf("%d\n", max);
#endif
return 0;
}
Loading…
Cancel
Save