C version, bash cleanup.
This commit is contained in:
@@ -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
|
||||
|
15
day15/OUTPUT
15
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
|
||||
|
40
day15/ex1-c.c
Normal file
40
day15/ex1-c.c
Normal file
@@ -0,0 +1,40 @@
|
||||
/* ex1-c: Advent2020 game, day 15/games 1 and 2
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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<target; ++i) {
|
||||
array[i]=-1;
|
||||
}
|
||||
|
||||
fgets(line, sizeof line, stdin);
|
||||
pval=strtok(line, ",");
|
||||
do {
|
||||
diff=atoi(pval);
|
||||
array[diff]=cur++;
|
||||
} while (pval=strtok(NULL, ","));
|
||||
|
||||
for (; cur<target; ++cur) {
|
||||
diff = (previous>=0)? cur-previous-1: 0;
|
||||
previous=array[diff];
|
||||
array[diff]=cur;
|
||||
}
|
||||
|
||||
printf("%s : res[%d]=%d\n", *av, target, diff);
|
||||
exit (0);
|
||||
}
|
@@ -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
|
||||
|
Reference in New Issue
Block a user