From 778583267377f25081a64bd8b16304a8e06fd57a Mon Sep 17 00:00:00 2001 From: Avril Date: Wed, 9 Dec 2020 12:35:30 +0000 Subject: [PATCH] day9: part1 --- day9/Makefile | 41 +++++++++++++++++++++++ day9/day9.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ day9/map.c | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++ day9/map.h | 28 ++++++++++++++++ 4 files changed, 254 insertions(+) create mode 100644 day9/Makefile create mode 100644 day9/day9.c create mode 100644 day9/map.c create mode 100644 day9/map.h diff --git a/day9/Makefile b/day9/Makefile new file mode 100644 index 0000000..d21ba00 --- /dev/null +++ b/day9/Makefile @@ -0,0 +1,41 @@ +SRC:= day9.c map.c + +OPT_FLAGS?= -march=native -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 + +COMMON_FLAGS?=-pipe -O3 -Wall -pedantic -Wextra -Wstrict-aliasing + +C_OPT_FLAGS=$(OPT_FLAGS) +CXX_OPT_FLAGS=$(OPT_FLAGS) -felide-constructors -fno-exceptions +LD_OPT_FLAGS?=-O3 -flto + +INCLUDE=../common/include + +CFLAGS?=$(COMMON_FLAGS) $(C_OPT_FLAGS) --std=gnu11 +CXXFLAGS?=$(COMMON_FLAGS) $(CXX_OPT_FLAGS) --std=gnu++20 +LDFLAGS?=$(LD_OPT_FLAGS) + +CFLAGS+=-I$(INCLUDE) + +.PHONY: all +all: part1 part2 + +inpu%.h: inpu% + @rm -f $@ + while read line; do \ + echo "$$line," >> $@; \ + done < $< + +part1: $(SRC) | input.h + $(CC) $(SRC) $(CFLAGS) -o $@ $(LDFLAGS) + strip $@ + +part1-test: $(SRC) | input-test.h + $(CC) $(SRC) -DTEST $(CFLAGS) -o $@ $(LDFLAGS) + strip $@ + +clean: + rm -f part{1,2}{,-test} + rm -f input{,-test}.h diff --git a/day9/day9.c b/day9/day9.c new file mode 100644 index 0000000..0d7c837 --- /dev/null +++ b/day9/day9.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#include + +#include "map.h" + +typedef uint64_t u64; + +#ifdef TEST +#define PREAMBLE 5 +#else +#define PREAMBLE 25 +#endif + +#define input_sz (sizeof(input) / sizeof(u64)) +const u64 input[] = { +#ifdef TEST +#include "input-test.h" +#else +#include "input.h" +#endif +}; + +#define box(t) (calloc(sizeof(t), 1)) + +struct pair { + bool valid; + u64 num; + u64 sum[2]; +}; + +inline static void insert_page(map_t* restrict where, u64 key, size_t i) +{ + //if(key > PAGE_SIZE) panic("Data too large, increase page size to fit %lu in a single page", key); + map_insert(where, key, i); +} + +static void calculate_deficits(const u64 pre[static PREAMBLE], map_t * restrict output, u64 target) +{ + for(size_t i=0;i pre[i]) + insert_page(output, target - pre[i], i); +} + +#ifdef PART2 +static u64 find_set(u64 target) +{ + +} +#endif + +int main() +{ + map_t def = map_new(); + struct pair result[input_sz] = {0}; + + for(register size_t i= PREAMBLE;i +#include +#include + +#include "map.h" + +#define box(t) calloc(sizeof(t), 1) + +struct page { + struct entry { + bool set; + map_key_t key; + map_value_t value; + } p[PAGE_SIZE]; + + struct page* next; +}; + +map_t map_new() +{ + register map_t rval; + + rval.page0 = box(struct page); + return rval; +} + +static inline size_t page_index(map_key_t k) +{ + return (size_t)(k % PAGE_SIZE); +} + +void map_insert(map_t * restrict into, map_key_t k, map_value_t v) +{ + struct page** p = &into->page0; + size_t i = page_index(k); + + while(*p) { + register struct page* page = *p; + if (!page->p[i].set || page->p[i].key == k) + { + page->p[i] = (struct entry){.set = true, .key = k, .value = v}; + return; + } + p = &page->next; + } + // p == &.next == &NULL + (*p = box(struct page))->p[i] = (struct entry){.set = true, .key = k, .value = v}; +} + +bool map_get(const map_t* from, map_key_t k, map_value_t* restrict v) +{ + const struct page* p = from->page0; + size_t i = page_index(k); + + while(p) { + if (p->p[i].set && p->p[i].key ==k) { + *v = p->p[i].value; + return true; + } + p = p->next; + } + return false; +} + +void map_free(map_t map) +{ + while(map.page0) + { + struct page* next = map.page0->next; + free(map.page0); + map.page0 = next; + } +} + +static void _map_walk(map_t* restrict map, void (*cb)(struct page* page)) +{ + struct page* page= map->page0; + while(page) + { + cb(page); + page = page->next; + } +} + +static void _map_page_clear(struct page* page) +{ + memset(page, 0, sizeof(struct page)); +} + +void map_clear(map_t* restrict map) +{ + _map_walk(map, &_map_page_clear); +} diff --git a/day9/map.h b/day9/map.h new file mode 100644 index 0000000..7b14c36 --- /dev/null +++ b/day9/map.h @@ -0,0 +1,28 @@ +#ifndef _MAP_H +#define _MAP_H + +#ifndef KELEMENT +#include +# define KELEMENT uint64_t +#endif +#ifndef VELEMENT +#include +# define VELEMENT size_t +#endif + +typedef KELEMENT map_key_t; +typedef VELEMENT map_value_t; + +#define PAGE_SIZE UINT16_MAX + +typedef struct _map { + struct page* page0; +} map_t; + +map_t map_new(); +void map_insert(map_t * restrict into, map_key_t k, map_value_t v); +bool map_get(const map_t* from, map_key_t k, map_value_t* restrict v); +void map_free(map_t map); +void map_clear(map_t* restrict map); + +#endif /* _MAP_H */