Initial Commit - days 01-12

This commit is contained in:
2020-12-21 14:58:15 +01:00
commit 11844d1904
131 changed files with 16060 additions and 0 deletions

26
templates/Makefile Normal file
View File

@@ -0,0 +1,26 @@
INPUT := INPUT.txt
SHELL := /bin/bash
CFLAGS := -w
TIME := \time -f "\ttime: %E real, %U user, %S sys\n\tcontext-switch:\t%c+%w, page-faults: %F+%R\n"
export PATH := .:$(PATH)
.PHONY: clean all compile deploy ex1 ex2
all: ex1 ex2
compile: ex1-c ex2-c
ex1: ex1-c
@$(TIME) ex1.bash < $(INPUT)
@$(TIME) ex1-c < $(INPUT)
ex2: ex2-c
@$(TIME) ex2.bash < $(INPUT)
@#$(TIME) ex2-sort.bash < $(INPUT)
@$(TIME) ex2-c < $(INPUT)
clean:
@rm -f ex1-c ex2-c core
deploy:
@$(MAKE) -C .. deploy

38
templates/ex1-c.c Normal file
View File

@@ -0,0 +1,38 @@
/* ex1-c: Advent2020 game, day 3/game 1
*/
#include <stdio.h>
#include <stdlib.h>
#define XMOVE 3
int my_strlen(str)
char *str;
{
int i;
for (i=0; *str; ++i, ++str)
;
return i;
}
int main(ac, av)
char **av;
{
int col=0, line=0, linelen=0, mod=0, count=0;
char str[80];
scanf("%s", str); /* ignore 1st line */
while (scanf("%s", str) != EOF) {
line++;
col+=XMOVE;
linelen=my_strlen(str);
mod=col%linelen;
if (*(str+mod) == '#')
count++;
}
printf ("%s : lines:%d pos:%d found:%d\n", *av, line, col, count);
exit (0);
}

21
templates/ex1.bash Executable file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 3/game 1.
CMD=${0##*/}
XMOVE=3 # 3 right
declare -i xpos=0 ypos=0 count=0 linelen=0 modpos=0
read -r line # ignore 1st line
while read -r line; do
(( ypos++ ))
(( xpos += XMOVE )) # move right
(( linelen = ${#line} ))
(( modpos = (xpos % linelen) ))
[[ ${line:modpos:1} = \# ]] && ((count++))
done
printf "%s : lines:%d pos=%d found:%d\n" "$CMD" $ypos $xpos $count
exit 0

60
templates/ex2-c.c Normal file
View File

@@ -0,0 +1,60 @@
/* ex1-c: Advent2020 game, day 3/game 2
*/
#include <stdio.h>
#include <stdlib.h>
struct {
int dx;
int dy;
int pos;
int count;
} set[] = {
{ 1, 1, 0, 0},
{ 3, 1, 0, 0},
{ 5, 1, 0, 0},
{ 7, 1, 0, 0},
{ 1, 2, 0, 0},
{-1, -1, 0, 0} /* end marker - not necessary */
};
int my_strlen(str)
char *str;
{
int i;
for (i=0; *str; ++i, ++str)
;
return i;
}
int main(ac, av)
int ac;
char **av;
{
int line=0, linelen=0, mod=0, i;
unsigned long res=1;
char str[80];
scanf("%s", str); /* ignore 1st line */
while (scanf("%s", str) != EOF) {
line++;
linelen=my_strlen(str);
for (i=0; set[i].dx >= 0; ++i) {
if (! (line % set[i].dy )) { /* line ok for this set */
set[i].pos += set[i].dx; /* increment set column */
mod = set[i].pos % linelen;
if ( str[mod] == '#')
set[i].count++;
}
}
}
printf("%s : ", *av);
for (i=0; set[i].dx != -1; ++i) {
printf("[%d]=[%d, %d, %d, %d] ", i,
set[i].dx, set[i].dy, set[i].pos, set[i].count);
res*=set[i].count;
}
printf ("res:%lu\n", res);
exit (0);
}

34
templates/ex2.bash Executable file
View File

@@ -0,0 +1,34 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 3/game 2.
CMD=${0##*/}
XMOVES=(1 3 5 7 1) # moves right
YMOVES=(1 1 1 1 2) # moves down
declare -i linelen=0 modpos=0 res=1
declare -a xpos xcount
read -r line # ignore 1st line
while read -r line; do
(( ypos++ ))
(( linelen = ${#line} ))
for ((i=0; i<${#XMOVES[@]}; ++i)); do
if ((ypos % YMOVES[i] == 0)); then
(( xpos[i] += XMOVES[i] )) # move right
(( modpos = (xpos[i] % linelen) ))
[[ ${line:modpos:1} = \# ]] && ((xcount[i]++))
fi
done
done
echo "xpos=${xpos[*]} xcount=${xcount[*]}"
printf "%s : " "$CMD"
for ((i=0; i<${#XMOVES[@]}; ++i)); do
printf "[%d]=[%d, %d, %d, %d] " $i "${XMOVES[$i]}" "${YMOVES[$i]}" \
"${xpos[$i]}" "${xcount[$i]}"
(( res *= xcount[i] ))
done
printf "res:%d\n " $res
exit 0