From 93a05e3f174f91d00a940b55eaa7a99d5bb0deb3 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Sat, 2 Jan 2021 20:12:01 +0100 Subject: [PATCH] C version, bash cleanup. --- day15/Makefile | 8 ++++---- day15/OUTPUT | 15 +++++++++++---- day15/ex1-c.c | 40 ++++++++++++++++++++++++++++++++++++++++ day15/ex1.bash | 7 ++----- 4 files changed, 57 insertions(+), 13 deletions(-) create mode 100644 day15/ex1-c.c diff --git a/day15/Makefile b/day15/Makefile index 0a34944..5247882 100644 --- a/day15/Makefile +++ b/day15/Makefile @@ -15,13 +15,13 @@ output: compile: ex1-c ex2-c -ex1: +ex1: ex1-c @$(TIME) ex1.bash 2020 < $(INPUT) 2>&1 - @#$(TIME) ex1-c < $(INPUT) 2>&1 + @$(TIME) ex1-c 2020 < $(INPUT) 2>&1 -ex2: +ex2: ex1-c @$(TIME) ex1.bash 30000000 < $(INPUT) 2>&1 - @#$(TIME) ex2-c < $(INPUT) 2>&1 + @$(TIME) ex1-c 30000000 < $(INPUT) 2>&1 clean: @rm -f ex1-c ex2-c core diff --git a/day15/OUTPUT b/day15/OUTPUT index 6d18640..b6f01fe 100644 --- a/day15/OUTPUT +++ b/day15/OUTPUT @@ -1,8 +1,15 @@ -ex1.bash : res=1618 - time: 0:00.07 real, 0.07 user, 0.00 sys - context-switch: 7+1, page-faults: 0+172 +ex1.bash : res[2020]=1618 + time: 0:00.03 real, 0.02 user, 0.00 sys + context-switch: 6+1, page-faults: 0+158 -ex3.bash : res=548531 +ex1-c : res[2020]=1618 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+77 + +ex1.bash : res[30000000]=548531 time: 12:22:02 real, 44488.91 user, 2.70 sys context-switch: 4552388+2, page-faults: 0+98912 +ex1-c : res[30000000]=548531 + time: 0:00.56 real, 0.49 user, 0.06 sys + context-switch: 55+1, page-faults: 0+29369 diff --git a/day15/ex1-c.c b/day15/ex1-c.c new file mode 100644 index 0000000..1f3603b --- /dev/null +++ b/day15/ex1-c.c @@ -0,0 +1,40 @@ +/* ex1-c: Advent2020 game, day 15/games 1 and 2 + */ + +#include +#include +#include + +int main(ac, av) + int ac; + char **av; +{ + char line[1024], *pval; + int target, cur=0, *array; + register int diff, previous=-1; + + if (ac != 2) { + fprintf(stderr, "usage: %s number\n", *av); + } + target=atol(*(av+1)); + array=malloc(sizeof(unsigned long) * target); + for (int i=0; i=0)? cur-previous-1: 0; + previous=array[diff]; + array[diff]=cur; + } + + printf("%s : res[%d]=%d\n", *av, target, diff); + exit (0); +} diff --git a/day15/ex1.bash b/day15/ex1.bash index 2839cfb..fad0c57 100755 --- a/day15/ex1.bash +++ b/day15/ex1.bash @@ -6,7 +6,7 @@ CMD=${0##*/} #shopt -s extglob declare -A prev -declare -i cur=1 last previous +declare -i cur=1 previous function print_binary() { @@ -27,7 +27,6 @@ function print_binary() { function print_array() { local -i i - printf "last: %d\n" "$last" printf "nums:\n" for i in "${!prev[@]}"; do printf " prev[%d] => %d\n" "$i" "${prev[$i]}" >&2 @@ -40,7 +39,6 @@ read -r line set -- $line while (($# > 0)); do prev[$1]=$cur - last=$1 ((cur++)) shift done @@ -53,9 +51,8 @@ for ((previous=0; cur <= TARGET; ++cur)); do fi ((previous=prev[$diff])) prev[$diff]=$cur - last=$diff done -printf "%s : res=%d\n" "$CMD" "$last" +printf "%s : res[%d]=%d\n" "$CMD" "$TARGET" "$diff" exit 0