From 50e1f31093b4750c45ceee74cda2903a23d21b3f Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 2 Dec 2021 15:03:35 +0000 Subject: [PATCH] day2: part1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for day2's current commit: Small blessing − 小吉 --- day2/Makefile | 100 ++++ day2/include/input.h | 17 + day2/include/result.h | 40 ++ day2/input | 1000 ++++++++++++++++++++++++++++++++++++++++ day2/mkinput | 15 + day2/src/input.c | 1004 +++++++++++++++++++++++++++++++++++++++++ day2/src/main.c | 34 ++ day2/src/p1.c | 99 ++++ 8 files changed, 2309 insertions(+) create mode 100644 day2/Makefile create mode 100644 day2/include/input.h create mode 100644 day2/include/result.h create mode 100644 day2/input create mode 100755 day2/mkinput create mode 100644 day2/src/input.c create mode 100644 day2/src/main.c create mode 100644 day2/src/p1.c diff --git a/day2/Makefile b/day2/Makefile new file mode 100644 index 0000000..fa5f0cf --- /dev/null +++ b/day2/Makefile @@ -0,0 +1,100 @@ +# aoc2021-day2 + +PROJECT=day2 +AUTHOR=Avril (Flanchan) + +SRC_C = $(wildcard src/*.c) +SRC_CXX = $(wildcard src/*.cpp) + +INCLUDE=include + +INPUT_SOURCE=input +INPUT_DEST=src/input.c + +COMMON_FLAGS+= -W -Wall -fno-strict-aliasing $(addprefix -I,$(INCLUDE)) + +COMMON_FLAGS+=-msse -msse2 -msse3 +COMMON_FLAGS+=-D_PART1 +#-D_PART2 + +OPT_FLAGS?= -march=native -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \ + -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ + -fno-stack-check + +CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors + +CFLAGS += $(COMMON_FLAGS) --std=gnu17 +CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 +LDFLAGS += + +STRIP=strip + +RELEASE_COMMON_FLAGS+= -Werror +DEBUG_COMMON_FLAGS+= -fanalyzer + +ifneq ($(TARGET_SPEC_FLAGS),no) + RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) + RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS) + RELEASE_LDFLAGS?= -Wl,-O3 -Wl,-flto + + DEBUG_CFLAGS?= -Og -g + DEBUG_CXXFLAGS?=-Og -g + DEBUG_LDFLAGS?= +endif + +DEBUG_CFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS) +DEBUG_CXXFLAGS+=-DDEBUG $(DEBUG_COMMON_FLAGS) + +RELEASE_CFLAGS+=-DRELEASE $(RELEASE_COMMON_FLAGS) +RELEASE_CXXFLAGS+=-DRELEASE $(RELEASE_COMMON_FLAGS) + +# Objects + +OBJ_C = $(addprefix obj/c/,$(SRC_C:.c=.o)) +OBJ_CXX = $(addprefix obj/cxx/,$(SRC_CXX:.cpp=.o)) +OBJ = $(OBJ_C) $(OBJ_CXX) + +# Phonies + +.PHONY: release +release: | dirs $(INPUT_DEST) + $(MAKE) $(PROJECT)-release + +.PHONY: debug +debug: | dirs $(INPUT_DEST) + $(MAKE) $(PROJECT)-debug + +# Targets + +dirs: + @mkdir -p obj/c{,xx}/src + +$(INPUT_DEST): $(INPUT_SOURCE) + ./mkinput $< > $@ + +obj/c/%.o: %.c + $(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS) + +obj/cxx/%.o: %.cpp + $(CXX) -c $< $(CXXFLAGS) -o $@ $(LDFLAGS) + +$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS) +$(PROJECT)-release: CXXFLAGS += $(RELEASE_CXXFLAGS) +$(PROJECT)-release: LDFLAGS += $(RELEASE_LDFLAGS) +$(PROJECT)-release: $(OBJ) + $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) + $(STRIP) $@ + +$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS) +$(PROJECT)-debug: CXXFLAGS += $(DEBUG_CXXFLAGS) +$(PROJECT)-debug: LDFLAGS += $(DEBUG_LDFLAGS) +$(PROJECT)-debug: $(OBJ) + $(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS) + +clean-rebuild: + rm -rf obj + +clean: clean-rebuild + rm -f $(PROJECT)-{release,debug,pgo} + rm -f $(INPUT_DEST) + diff --git a/day2/include/input.h b/day2/include/input.h new file mode 100644 index 0000000..4addc83 --- /dev/null +++ b/day2/include/input.h @@ -0,0 +1,17 @@ +#ifndef _INPUT_H +#define _INPUT_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +extern const char INPUT[]; +extern const size_t INPUT_SIZE; + +#ifdef __cplusplus +extern "C" { +#endif + +#endif diff --git a/day2/include/result.h b/day2/include/result.h new file mode 100644 index 0000000..5c0a41b --- /dev/null +++ b/day2/include/result.h @@ -0,0 +1,40 @@ +#ifndef _RESULT_H +#define _RESULT_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef int64_t result_t; + +struct psum { + union { + struct { result_t forward, up, down; }; + result_t _fields[3]; + }; +}; + +typedef struct nsum { + result_t hoz, depth; +} position_t; + +#ifdef DEBUG +static +#else +extern +#endif +inline __attribute__((gnu_inline, const)) position_t normalise_position(struct psum p) +{ + return (struct nsum) { + .hoz = p.forward, + .depth = p.down - p.up, + }; +} + +#ifdef __cplusplus +} +#endif + +#endif /* _RESULT_H */ diff --git a/day2/input b/day2/input new file mode 100644 index 0000000..aef25e3 --- /dev/null +++ b/day2/input @@ -0,0 +1,1000 @@ +forward 8 +forward 3 +down 8 +forward 4 +down 2 +down 4 +down 8 +down 4 +down 7 +up 4 +down 6 +down 2 +forward 7 +down 5 +down 7 +down 5 +forward 8 +forward 4 +forward 3 +down 4 +forward 2 +up 3 +up 6 +forward 7 +down 4 +down 2 +forward 7 +up 3 +forward 8 +down 4 +down 6 +forward 3 +forward 9 +down 9 +down 6 +forward 8 +up 8 +forward 3 +down 8 +down 4 +forward 7 +forward 3 +up 9 +down 8 +forward 7 +forward 5 +forward 3 +down 1 +down 4 +forward 1 +down 6 +forward 9 +up 2 +forward 3 +down 2 +down 1 +up 5 +down 8 +down 7 +down 5 +down 9 +up 7 +forward 4 +down 9 +down 8 +down 4 +forward 3 +down 1 +up 8 +down 1 +down 8 +forward 4 +up 1 +forward 7 +down 6 +down 2 +forward 3 +down 9 +forward 9 +forward 4 +down 1 +up 6 +down 1 +up 2 +down 4 +down 9 +down 7 +forward 5 +down 3 +up 9 +forward 4 +up 5 +down 9 +forward 1 +forward 7 +forward 1 +down 8 +forward 5 +down 2 +down 4 +down 3 +forward 4 +down 9 +up 7 +up 1 +forward 4 +up 2 +forward 4 +forward 4 +down 1 +down 5 +forward 6 +forward 1 +down 6 +forward 8 +forward 8 +forward 6 +down 3 +forward 3 +forward 1 +forward 5 +forward 7 +down 5 +forward 8 +down 7 +down 8 +forward 7 +forward 8 +down 7 +forward 7 +up 2 +up 9 +down 1 +forward 1 +forward 6 +up 3 +up 6 +down 2 +down 4 +forward 2 +up 8 +forward 5 +up 9 +up 5 +down 2 +forward 5 +forward 9 +up 3 +forward 7 +down 4 +down 3 +down 2 +forward 9 +up 4 +down 7 +down 1 +forward 9 +forward 3 +up 4 +forward 1 +up 6 +forward 5 +forward 2 +forward 2 +down 9 +forward 1 +up 7 +forward 3 +down 1 +down 5 +up 8 +down 5 +forward 5 +forward 5 +down 9 +up 5 +forward 8 +up 9 +down 1 +forward 1 +down 7 +up 3 +down 9 +down 9 +down 5 +up 6 +forward 7 +down 5 +down 2 +down 5 +forward 3 +forward 2 +forward 9 +forward 5 +forward 6 +up 7 +up 7 +forward 4 +forward 2 +up 4 +up 6 +down 8 +up 9 +up 5 +down 8 +forward 4 +up 9 +forward 8 +forward 3 +down 6 +down 6 +down 7 +down 6 +forward 7 +up 5 +down 2 +down 5 +forward 2 +up 5 +forward 7 +down 5 +up 4 +up 2 +forward 2 +forward 9 +forward 5 +forward 8 +up 4 +forward 6 +up 7 +forward 5 +down 2 +down 3 +down 8 +up 2 +down 2 +up 1 +up 7 +down 1 +down 6 +down 9 +down 7 +forward 8 +up 8 +up 9 +down 9 +forward 6 +forward 6 +down 6 +forward 3 +forward 4 +forward 6 +forward 7 +down 5 +down 8 +forward 7 +forward 9 +down 4 +up 9 +forward 8 +down 6 +down 1 +down 2 +forward 4 +down 5 +up 1 +forward 1 +down 6 +forward 4 +forward 2 +forward 6 +down 8 +up 2 +up 8 +forward 3 +down 6 +forward 7 +down 1 +forward 1 +forward 8 +down 8 +down 8 +down 2 +forward 8 +down 4 +up 8 +down 6 +forward 2 +down 5 +up 3 +up 1 +down 2 +forward 4 +up 7 +forward 2 +up 9 +forward 1 +down 5 +forward 3 +up 9 +up 4 +down 4 +up 6 +down 1 +forward 9 +up 5 +forward 5 +up 4 +down 6 +forward 2 +up 6 +forward 5 +forward 5 +down 4 +up 7 +forward 2 +down 9 +down 8 +down 7 +forward 4 +forward 7 +down 4 +forward 9 +up 2 +forward 1 +up 4 +down 8 +forward 9 +up 2 +up 5 +down 7 +up 6 +forward 7 +up 1 +forward 3 +down 3 +down 2 +down 6 +down 6 +forward 3 +down 2 +down 2 +down 5 +down 4 +down 6 +down 2 +forward 7 +down 6 +forward 4 +down 5 +down 7 +down 3 +forward 2 +forward 8 +forward 1 +up 6 +down 8 +down 4 +up 5 +forward 5 +down 7 +forward 5 +forward 8 +forward 7 +down 8 +down 5 +down 3 +up 2 +forward 8 +forward 7 +down 6 +up 1 +down 9 +down 3 +down 7 +down 2 +forward 9 +forward 2 +forward 4 +forward 8 +forward 3 +forward 2 +down 3 +down 8 +up 7 +down 7 +down 1 +forward 8 +down 2 +up 4 +forward 9 +down 6 +forward 5 +forward 8 +forward 1 +forward 4 +down 6 +down 1 +down 6 +forward 1 +forward 7 +down 3 +down 3 +down 1 +up 2 +down 6 +down 5 +down 6 +forward 5 +forward 2 +forward 1 +forward 8 +up 5 +down 2 +down 9 +down 9 +down 5 +down 3 +up 6 +forward 2 +down 7 +down 7 +down 2 +down 2 +up 6 +forward 8 +forward 6 +forward 4 +down 1 +forward 7 +down 5 +up 9 +forward 5 +down 4 +down 9 +up 7 +forward 1 +up 3 +up 7 +forward 6 +forward 8 +up 6 +up 3 +forward 4 +up 3 +down 1 +forward 8 +forward 1 +up 3 +forward 7 +up 2 +forward 6 +forward 2 +down 2 +down 9 +down 9 +forward 1 +forward 8 +down 5 +forward 4 +forward 5 +forward 5 +down 9 +forward 8 +up 8 +down 6 +down 2 +down 5 +down 6 +up 9 +up 8 +down 4 +down 9 +up 4 +forward 6 +forward 1 +forward 3 +up 4 +up 3 +forward 3 +forward 8 +down 1 +down 2 +down 2 +forward 9 +forward 4 +up 2 +forward 7 +down 4 +up 1 +forward 2 +forward 9 +forward 8 +down 4 +down 7 +up 7 +down 1 +forward 3 +down 7 +down 8 +up 2 +forward 2 +forward 8 +forward 4 +forward 4 +down 5 +forward 8 +down 2 +up 7 +down 1 +up 9 +up 5 +down 3 +forward 6 +forward 2 +forward 6 +down 5 +down 6 +forward 9 +up 5 +up 6 +down 4 +down 5 +up 4 +down 6 +down 2 +down 5 +down 4 +forward 7 +forward 6 +down 8 +forward 2 +down 5 +down 7 +down 2 +forward 8 +forward 6 +down 3 +forward 4 +up 6 +down 9 +down 3 +forward 3 +forward 3 +down 9 +up 1 +up 3 +forward 3 +forward 6 +forward 1 +forward 4 +forward 3 +forward 3 +forward 3 +down 6 +down 8 +forward 3 +down 5 +forward 8 +forward 3 +down 4 +up 3 +up 1 +down 7 +forward 7 +up 6 +forward 7 +down 4 +down 7 +up 9 +down 9 +forward 8 +down 5 +down 2 +forward 9 +down 3 +forward 4 +forward 4 +forward 4 +forward 6 +down 1 +up 5 +forward 8 +down 6 +forward 5 +up 5 +up 5 +down 2 +down 9 +down 7 +up 3 +up 7 +up 6 +forward 2 +forward 6 +up 9 +forward 5 +forward 2 +up 4 +down 4 +down 5 +forward 7 +down 5 +down 7 +forward 3 +down 6 +down 1 +forward 6 +up 5 +up 6 +up 3 +down 9 +up 7 +forward 9 +down 3 +forward 4 +up 2 +forward 9 +down 2 +up 2 +up 5 +forward 1 +down 8 +down 1 +down 8 +up 2 +forward 9 +forward 1 +up 2 +down 1 +up 1 +forward 2 +down 7 +forward 9 +up 1 +forward 8 +down 5 +down 5 +down 9 +forward 5 +down 7 +down 1 +forward 9 +down 5 +forward 9 +forward 9 +down 6 +down 5 +down 6 +forward 3 +down 4 +up 8 +down 6 +up 4 +down 9 +up 1 +up 2 +up 8 +forward 6 +down 7 +down 4 +down 9 +down 3 +forward 9 +down 5 +forward 6 +down 6 +forward 7 +down 9 +forward 2 +forward 2 +down 8 +down 7 +forward 5 +down 8 +forward 6 +down 1 +forward 6 +forward 7 +forward 3 +forward 2 +forward 1 +forward 1 +down 9 +forward 7 +up 9 +down 5 +forward 6 +down 4 +down 2 +forward 4 +forward 3 +forward 9 +down 8 +down 2 +forward 4 +down 8 +down 6 +forward 9 +down 7 +forward 1 +up 1 +forward 3 +down 5 +down 8 +up 6 +forward 9 +forward 4 +down 2 +forward 4 +up 2 +forward 9 +down 5 +down 1 +down 9 +forward 5 +down 9 +forward 1 +down 9 +forward 8 +down 5 +forward 9 +forward 8 +up 8 +down 7 +up 9 +down 5 +up 9 +forward 7 +forward 7 +forward 1 +up 2 +up 2 +forward 6 +up 5 +up 5 +down 7 +forward 2 +up 8 +forward 8 +down 8 +forward 7 +forward 8 +down 7 +down 8 +forward 4 +forward 2 +down 8 +up 4 +down 1 +up 7 +forward 4 +forward 3 +down 6 +up 6 +forward 6 +down 1 +forward 3 +down 6 +forward 6 +up 3 +up 1 +up 5 +down 1 +up 9 +down 8 +forward 7 +forward 9 +up 2 +forward 2 +forward 6 +up 2 +up 3 +forward 1 +forward 2 +forward 8 +down 6 +forward 5 +down 8 +forward 2 +up 1 +down 1 +down 3 +forward 9 +down 6 +down 1 +down 6 +down 6 +down 6 +down 1 +up 6 +down 6 +forward 6 +down 5 +down 2 +up 5 +forward 7 +down 3 +forward 9 +forward 2 +down 2 +down 1 +down 9 +up 3 +down 2 +forward 2 +forward 6 +down 4 +up 7 +up 3 +up 9 +forward 3 +forward 8 +forward 9 +forward 4 +forward 2 +up 7 +up 5 +down 9 +forward 1 +up 1 +down 7 +forward 5 +forward 5 +down 3 +forward 1 +forward 6 +up 8 +down 4 +down 2 +up 2 +forward 2 +forward 1 +down 4 +up 9 +down 1 +down 4 +down 6 +forward 3 +forward 7 +down 4 +up 8 +down 6 +forward 7 +forward 4 +up 2 +down 8 +forward 5 +down 1 +forward 7 +down 1 +up 6 +down 9 +forward 3 +forward 1 +up 2 +up 4 +forward 8 +forward 1 +up 8 +forward 3 +forward 7 +up 9 +up 6 +up 9 +down 9 +down 5 +forward 8 +forward 7 +down 5 +forward 7 +forward 6 +forward 8 +up 4 +forward 6 +down 6 +up 8 +down 7 +up 8 +forward 6 +forward 7 +down 4 +forward 4 +up 2 +forward 9 +down 6 +down 2 +down 8 +forward 6 +up 2 +up 8 +forward 2 +down 3 +forward 7 +forward 6 +down 9 +up 1 +forward 1 +down 8 +down 8 +forward 4 +forward 4 +up 6 +down 8 +up 8 +forward 1 +forward 1 +down 6 +up 3 +up 7 +forward 1 +forward 1 +up 2 +forward 2 +down 4 +up 8 +forward 1 +up 7 +down 2 +forward 2 +down 1 +forward 1 +down 4 +forward 4 +forward 8 +up 5 +down 6 +up 8 +forward 4 +down 3 +up 6 +forward 3 +forward 4 +down 5 +forward 1 +forward 6 +forward 4 +forward 1 +down 7 +down 4 +forward 2 +down 6 +forward 3 +down 5 +forward 3 +forward 4 +forward 4 +forward 9 +down 5 +forward 7 +forward 7 +forward 9 +down 1 +up 6 +forward 1 +down 9 +forward 3 +down 7 +up 8 +up 6 +down 7 +forward 2 +down 9 +forward 9 +forward 6 +down 9 +forward 6 +down 8 +forward 1 +up 6 +down 1 +forward 6 +down 9 +forward 6 +forward 7 +forward 3 +forward 8 +forward 5 +forward 8 +down 9 +down 2 +forward 3 +down 3 +up 4 +down 4 +down 3 +forward 3 +forward 3 +down 5 +forward 5 +forward 1 +down 9 +down 3 +up 7 +forward 9 +up 1 +down 1 +down 6 +up 1 +forward 2 diff --git a/day2/mkinput b/day2/mkinput new file mode 100755 index 0000000..6da95ac --- /dev/null +++ b/day2/mkinput @@ -0,0 +1,15 @@ +#!/bin/bash + +INPUT=${1:-input} + +echo "#include " +echo "" +printf "const char INPUT[] = " + +while IFS= read -r line; do + echo " \"$line\"" +done < $INPUT +unset line + +echo ";" +echo "const size_t INPUT_SIZE=sizeof(INPUT)/sizeof(char);" diff --git a/day2/src/input.c b/day2/src/input.c new file mode 100644 index 0000000..51a5865 --- /dev/null +++ b/day2/src/input.c @@ -0,0 +1,1004 @@ +#include + +const char INPUT[] = "forward 8" + "forward 3" + "down 8" + "forward 4" + "down 2" + "down 4" + "down 8" + "down 4" + "down 7" + "up 4" + "down 6" + "down 2" + "forward 7" + "down 5" + "down 7" + "down 5" + "forward 8" + "forward 4" + "forward 3" + "down 4" + "forward 2" + "up 3" + "up 6" + "forward 7" + "down 4" + "down 2" + "forward 7" + "up 3" + "forward 8" + "down 4" + "down 6" + "forward 3" + "forward 9" + "down 9" + "down 6" + "forward 8" + "up 8" + "forward 3" + "down 8" + "down 4" + "forward 7" + "forward 3" + "up 9" + "down 8" + "forward 7" + "forward 5" + "forward 3" + "down 1" + "down 4" + "forward 1" + "down 6" + "forward 9" + "up 2" + "forward 3" + "down 2" + "down 1" + "up 5" + "down 8" + "down 7" + "down 5" + "down 9" + "up 7" + "forward 4" + "down 9" + "down 8" + "down 4" + "forward 3" + "down 1" + "up 8" + "down 1" + "down 8" + "forward 4" + "up 1" + "forward 7" + "down 6" + "down 2" + "forward 3" + "down 9" + "forward 9" + "forward 4" + "down 1" + "up 6" + "down 1" + "up 2" + "down 4" + "down 9" + "down 7" + "forward 5" + "down 3" + "up 9" + "forward 4" + "up 5" + "down 9" + "forward 1" + "forward 7" + "forward 1" + "down 8" + "forward 5" + "down 2" + "down 4" + "down 3" + "forward 4" + "down 9" + "up 7" + "up 1" + "forward 4" + "up 2" + "forward 4" + "forward 4" + "down 1" + "down 5" + "forward 6" + "forward 1" + "down 6" + "forward 8" + "forward 8" + "forward 6" + "down 3" + "forward 3" + "forward 1" + "forward 5" + "forward 7" + "down 5" + "forward 8" + "down 7" + "down 8" + "forward 7" + "forward 8" + "down 7" + "forward 7" + "up 2" + "up 9" + "down 1" + "forward 1" + "forward 6" + "up 3" + "up 6" + "down 2" + "down 4" + "forward 2" + "up 8" + "forward 5" + "up 9" + "up 5" + "down 2" + "forward 5" + "forward 9" + "up 3" + "forward 7" + "down 4" + "down 3" + "down 2" + "forward 9" + "up 4" + "down 7" + "down 1" + "forward 9" + "forward 3" + "up 4" + "forward 1" + "up 6" + "forward 5" + "forward 2" + "forward 2" + "down 9" + "forward 1" + "up 7" + "forward 3" + "down 1" + "down 5" + "up 8" + "down 5" + "forward 5" + "forward 5" + "down 9" + "up 5" + "forward 8" + "up 9" + "down 1" + "forward 1" + "down 7" + "up 3" + "down 9" + "down 9" + "down 5" + "up 6" + "forward 7" + "down 5" + "down 2" + "down 5" + "forward 3" + "forward 2" + "forward 9" + "forward 5" + "forward 6" + "up 7" + "up 7" + "forward 4" + "forward 2" + "up 4" + "up 6" + "down 8" + "up 9" + "up 5" + "down 8" + "forward 4" + "up 9" + "forward 8" + "forward 3" + "down 6" + "down 6" + "down 7" + "down 6" + "forward 7" + "up 5" + "down 2" + "down 5" + "forward 2" + "up 5" + "forward 7" + "down 5" + "up 4" + "up 2" + "forward 2" + "forward 9" + "forward 5" + "forward 8" + "up 4" + "forward 6" + "up 7" + "forward 5" + "down 2" + "down 3" + "down 8" + "up 2" + "down 2" + "up 1" + "up 7" + "down 1" + "down 6" + "down 9" + "down 7" + "forward 8" + "up 8" + "up 9" + "down 9" + "forward 6" + "forward 6" + "down 6" + "forward 3" + "forward 4" + "forward 6" + "forward 7" + "down 5" + "down 8" + "forward 7" + "forward 9" + "down 4" + "up 9" + "forward 8" + "down 6" + "down 1" + "down 2" + "forward 4" + "down 5" + "up 1" + "forward 1" + "down 6" + "forward 4" + "forward 2" + "forward 6" + "down 8" + "up 2" + "up 8" + "forward 3" + "down 6" + "forward 7" + "down 1" + "forward 1" + "forward 8" + "down 8" + "down 8" + "down 2" + "forward 8" + "down 4" + "up 8" + "down 6" + "forward 2" + "down 5" + "up 3" + "up 1" + "down 2" + "forward 4" + "up 7" + "forward 2" + "up 9" + "forward 1" + "down 5" + "forward 3" + "up 9" + "up 4" + "down 4" + "up 6" + "down 1" + "forward 9" + "up 5" + "forward 5" + "up 4" + "down 6" + "forward 2" + "up 6" + "forward 5" + "forward 5" + "down 4" + "up 7" + "forward 2" + "down 9" + "down 8" + "down 7" + "forward 4" + "forward 7" + "down 4" + "forward 9" + "up 2" + "forward 1" + "up 4" + "down 8" + "forward 9" + "up 2" + "up 5" + "down 7" + "up 6" + "forward 7" + "up 1" + "forward 3" + "down 3" + "down 2" + "down 6" + "down 6" + "forward 3" + "down 2" + "down 2" + "down 5" + "down 4" + "down 6" + "down 2" + "forward 7" + "down 6" + "forward 4" + "down 5" + "down 7" + "down 3" + "forward 2" + "forward 8" + "forward 1" + "up 6" + "down 8" + "down 4" + "up 5" + "forward 5" + "down 7" + "forward 5" + "forward 8" + "forward 7" + "down 8" + "down 5" + "down 3" + "up 2" + "forward 8" + "forward 7" + "down 6" + "up 1" + "down 9" + "down 3" + "down 7" + "down 2" + "forward 9" + "forward 2" + "forward 4" + "forward 8" + "forward 3" + "forward 2" + "down 3" + "down 8" + "up 7" + "down 7" + "down 1" + "forward 8" + "down 2" + "up 4" + "forward 9" + "down 6" + "forward 5" + "forward 8" + "forward 1" + "forward 4" + "down 6" + "down 1" + "down 6" + "forward 1" + "forward 7" + "down 3" + "down 3" + "down 1" + "up 2" + "down 6" + "down 5" + "down 6" + "forward 5" + "forward 2" + "forward 1" + "forward 8" + "up 5" + "down 2" + "down 9" + "down 9" + "down 5" + "down 3" + "up 6" + "forward 2" + "down 7" + "down 7" + "down 2" + "down 2" + "up 6" + "forward 8" + "forward 6" + "forward 4" + "down 1" + "forward 7" + "down 5" + "up 9" + "forward 5" + "down 4" + "down 9" + "up 7" + "forward 1" + "up 3" + "up 7" + "forward 6" + "forward 8" + "up 6" + "up 3" + "forward 4" + "up 3" + "down 1" + "forward 8" + "forward 1" + "up 3" + "forward 7" + "up 2" + "forward 6" + "forward 2" + "down 2" + "down 9" + "down 9" + "forward 1" + "forward 8" + "down 5" + "forward 4" + "forward 5" + "forward 5" + "down 9" + "forward 8" + "up 8" + "down 6" + "down 2" + "down 5" + "down 6" + "up 9" + "up 8" + "down 4" + "down 9" + "up 4" + "forward 6" + "forward 1" + "forward 3" + "up 4" + "up 3" + "forward 3" + "forward 8" + "down 1" + "down 2" + "down 2" + "forward 9" + "forward 4" + "up 2" + "forward 7" + "down 4" + "up 1" + "forward 2" + "forward 9" + "forward 8" + "down 4" + "down 7" + "up 7" + "down 1" + "forward 3" + "down 7" + "down 8" + "up 2" + "forward 2" + "forward 8" + "forward 4" + "forward 4" + "down 5" + "forward 8" + "down 2" + "up 7" + "down 1" + "up 9" + "up 5" + "down 3" + "forward 6" + "forward 2" + "forward 6" + "down 5" + "down 6" + "forward 9" + "up 5" + "up 6" + "down 4" + "down 5" + "up 4" + "down 6" + "down 2" + "down 5" + "down 4" + "forward 7" + "forward 6" + "down 8" + "forward 2" + "down 5" + "down 7" + "down 2" + "forward 8" + "forward 6" + "down 3" + "forward 4" + "up 6" + "down 9" + "down 3" + "forward 3" + "forward 3" + "down 9" + "up 1" + "up 3" + "forward 3" + "forward 6" + "forward 1" + "forward 4" + "forward 3" + "forward 3" + "forward 3" + "down 6" + "down 8" + "forward 3" + "down 5" + "forward 8" + "forward 3" + "down 4" + "up 3" + "up 1" + "down 7" + "forward 7" + "up 6" + "forward 7" + "down 4" + "down 7" + "up 9" + "down 9" + "forward 8" + "down 5" + "down 2" + "forward 9" + "down 3" + "forward 4" + "forward 4" + "forward 4" + "forward 6" + "down 1" + "up 5" + "forward 8" + "down 6" + "forward 5" + "up 5" + "up 5" + "down 2" + "down 9" + "down 7" + "up 3" + "up 7" + "up 6" + "forward 2" + "forward 6" + "up 9" + "forward 5" + "forward 2" + "up 4" + "down 4" + "down 5" + "forward 7" + "down 5" + "down 7" + "forward 3" + "down 6" + "down 1" + "forward 6" + "up 5" + "up 6" + "up 3" + "down 9" + "up 7" + "forward 9" + "down 3" + "forward 4" + "up 2" + "forward 9" + "down 2" + "up 2" + "up 5" + "forward 1" + "down 8" + "down 1" + "down 8" + "up 2" + "forward 9" + "forward 1" + "up 2" + "down 1" + "up 1" + "forward 2" + "down 7" + "forward 9" + "up 1" + "forward 8" + "down 5" + "down 5" + "down 9" + "forward 5" + "down 7" + "down 1" + "forward 9" + "down 5" + "forward 9" + "forward 9" + "down 6" + "down 5" + "down 6" + "forward 3" + "down 4" + "up 8" + "down 6" + "up 4" + "down 9" + "up 1" + "up 2" + "up 8" + "forward 6" + "down 7" + "down 4" + "down 9" + "down 3" + "forward 9" + "down 5" + "forward 6" + "down 6" + "forward 7" + "down 9" + "forward 2" + "forward 2" + "down 8" + "down 7" + "forward 5" + "down 8" + "forward 6" + "down 1" + "forward 6" + "forward 7" + "forward 3" + "forward 2" + "forward 1" + "forward 1" + "down 9" + "forward 7" + "up 9" + "down 5" + "forward 6" + "down 4" + "down 2" + "forward 4" + "forward 3" + "forward 9" + "down 8" + "down 2" + "forward 4" + "down 8" + "down 6" + "forward 9" + "down 7" + "forward 1" + "up 1" + "forward 3" + "down 5" + "down 8" + "up 6" + "forward 9" + "forward 4" + "down 2" + "forward 4" + "up 2" + "forward 9" + "down 5" + "down 1" + "down 9" + "forward 5" + "down 9" + "forward 1" + "down 9" + "forward 8" + "down 5" + "forward 9" + "forward 8" + "up 8" + "down 7" + "up 9" + "down 5" + "up 9" + "forward 7" + "forward 7" + "forward 1" + "up 2" + "up 2" + "forward 6" + "up 5" + "up 5" + "down 7" + "forward 2" + "up 8" + "forward 8" + "down 8" + "forward 7" + "forward 8" + "down 7" + "down 8" + "forward 4" + "forward 2" + "down 8" + "up 4" + "down 1" + "up 7" + "forward 4" + "forward 3" + "down 6" + "up 6" + "forward 6" + "down 1" + "forward 3" + "down 6" + "forward 6" + "up 3" + "up 1" + "up 5" + "down 1" + "up 9" + "down 8" + "forward 7" + "forward 9" + "up 2" + "forward 2" + "forward 6" + "up 2" + "up 3" + "forward 1" + "forward 2" + "forward 8" + "down 6" + "forward 5" + "down 8" + "forward 2" + "up 1" + "down 1" + "down 3" + "forward 9" + "down 6" + "down 1" + "down 6" + "down 6" + "down 6" + "down 1" + "up 6" + "down 6" + "forward 6" + "down 5" + "down 2" + "up 5" + "forward 7" + "down 3" + "forward 9" + "forward 2" + "down 2" + "down 1" + "down 9" + "up 3" + "down 2" + "forward 2" + "forward 6" + "down 4" + "up 7" + "up 3" + "up 9" + "forward 3" + "forward 8" + "forward 9" + "forward 4" + "forward 2" + "up 7" + "up 5" + "down 9" + "forward 1" + "up 1" + "down 7" + "forward 5" + "forward 5" + "down 3" + "forward 1" + "forward 6" + "up 8" + "down 4" + "down 2" + "up 2" + "forward 2" + "forward 1" + "down 4" + "up 9" + "down 1" + "down 4" + "down 6" + "forward 3" + "forward 7" + "down 4" + "up 8" + "down 6" + "forward 7" + "forward 4" + "up 2" + "down 8" + "forward 5" + "down 1" + "forward 7" + "down 1" + "up 6" + "down 9" + "forward 3" + "forward 1" + "up 2" + "up 4" + "forward 8" + "forward 1" + "up 8" + "forward 3" + "forward 7" + "up 9" + "up 6" + "up 9" + "down 9" + "down 5" + "forward 8" + "forward 7" + "down 5" + "forward 7" + "forward 6" + "forward 8" + "up 4" + "forward 6" + "down 6" + "up 8" + "down 7" + "up 8" + "forward 6" + "forward 7" + "down 4" + "forward 4" + "up 2" + "forward 9" + "down 6" + "down 2" + "down 8" + "forward 6" + "up 2" + "up 8" + "forward 2" + "down 3" + "forward 7" + "forward 6" + "down 9" + "up 1" + "forward 1" + "down 8" + "down 8" + "forward 4" + "forward 4" + "up 6" + "down 8" + "up 8" + "forward 1" + "forward 1" + "down 6" + "up 3" + "up 7" + "forward 1" + "forward 1" + "up 2" + "forward 2" + "down 4" + "up 8" + "forward 1" + "up 7" + "down 2" + "forward 2" + "down 1" + "forward 1" + "down 4" + "forward 4" + "forward 8" + "up 5" + "down 6" + "up 8" + "forward 4" + "down 3" + "up 6" + "forward 3" + "forward 4" + "down 5" + "forward 1" + "forward 6" + "forward 4" + "forward 1" + "down 7" + "down 4" + "forward 2" + "down 6" + "forward 3" + "down 5" + "forward 3" + "forward 4" + "forward 4" + "forward 9" + "down 5" + "forward 7" + "forward 7" + "forward 9" + "down 1" + "up 6" + "forward 1" + "down 9" + "forward 3" + "down 7" + "up 8" + "up 6" + "down 7" + "forward 2" + "down 9" + "forward 9" + "forward 6" + "down 9" + "forward 6" + "down 8" + "forward 1" + "up 6" + "down 1" + "forward 6" + "down 9" + "forward 6" + "forward 7" + "forward 3" + "forward 8" + "forward 5" + "forward 8" + "down 9" + "down 2" + "forward 3" + "down 3" + "up 4" + "down 4" + "down 3" + "forward 3" + "forward 3" + "down 5" + "forward 5" + "forward 1" + "down 9" + "down 3" + "up 7" + "forward 9" + "up 1" + "down 1" + "down 6" + "up 1" + "forward 2" +; +const size_t INPUT_SIZE=sizeof(INPUT)/sizeof(char); diff --git a/day2/src/main.c b/day2/src/main.c new file mode 100644 index 0000000..f219b33 --- /dev/null +++ b/day2/src/main.c @@ -0,0 +1,34 @@ +#include + +#include + +#include + +extern int part1(result_t* restrict); +extern int part2(result_t* restrict); + +static void print_result(int part, result_t r) +{ +#ifdef DEBUG + printf(">>> [%d] %lu\n", part, r); +#else + (void)part; + printf("%ld\n", r); +#endif +} + +int main() +{ + int r = 0; +#ifdef _PART1 + result_t p1; + if( (r = part1(&p1)) ) return r; + print_result(1, p1); +#endif +#ifdef _PART2 + result_t p2; + if( (r = part2(&p2)) ) return r; + print_result(2, p2); +#endif + return r; +} diff --git a/day2/src/p1.c b/day2/src/p1.c new file mode 100644 index 0000000..f707f25 --- /dev/null +++ b/day2/src/p1.c @@ -0,0 +1,99 @@ +#include +#include +#include +#include + +#include + +#include + +#define RIMAX_STR 48 + +struct proc { + result_t mv; + off_t field; + +// void* jmp; +}; + +#define _LIT_MV_FORWARD "forward" +#define _LIT_MV_UP "up" +#define _LIT_MV_DOWN "down" + +static const struct proc MAP[255] = { + ['f'] = { .mv = 8, .field = (offsetof(struct psum, forward) / sizeof(result_t)) }, + ['u'] = { .mv = 3, .field = (offsetof(struct psum, up) / sizeof(result_t)) }, + ['d'] = { .mv = 5, .field = (offsetof(struct psum, down) / sizeof(result_t)) }, +}; + +static const unsigned char DIGITS[255] = { + ['0' ... '9'] = 1, +}; + +inline static void _fresolv(result_t f[restrict 3], off_t field, result_t v) +{ + f[field & 3] += v; +} + +inline static void fresolv(result_t f[restrict 3], const struct proc* restrict p, result_t v) +{ + _fresolv(f, p->field, v); + //f[p->field] += v; +} + +inline static void mresolv(struct psum* restrict add, const struct proc* p, result_t v) +{ + _fresolv((result_t* restrict)add, p->field, v); + //_fresolv((result_t(*)[3])add, p->field, v); + //*(((result_t*restrict)add)+p->field) += v; +} + + + +inline static __attribute__((const, always_inline, artificial, gnu_inline)) bool is_digit(char c) +{ + return !!c[DIGITS]; +} + +inline static result_t ri64p(const char* restrict *restrict pinput) +{ + char dig[RIMAX_STR] = {0}; + size_t i = 0; + while(is_digit(dig[i]=**pinput)) { i+=1; *pinput+=1; } +#if DEBUG + assert(i 0); +#endif + input+=p.mv; + result_t n = ri64p(&input); + fresolv(res._fields, &p, n); + } + + return res; +} + +int part1(result_t* restrict _res) +{ + position_t final = normalise_position(compute_unnormalised(INPUT)); + *_res = final.hoz * final.depth; + + return 0; +}