rename repo, subdir for yearly challenges

This commit is contained in:
2021-07-25 11:17:46 +02:00
parent 1806f79e14
commit 4a2318edc9
203 changed files with 212 additions and 1 deletions

370
2020/day18/INPUT.txt Normal file
View File

@@ -0,0 +1,370 @@
2 * (8 + 3 * 3 + (4 + 5)) + 7 * 2
2 * (5 * 7 + (2 * 2 + 7) + (5 + 2 * 8 + 4 * 4) + 9) * (9 * (7 * 2) * (7 + 3 * 3 * 2 + 4) * 9 + (2 * 4 * 9) + 8) + 6 * (8 + 2 * 8)
5 * 3 * 7 * 2 * (5 * 9 + (3 * 5 * 2 + 6 + 7) + (6 + 2 + 9 * 7) + 4)
(9 + (7 * 5 + 9 + 2) * 7 * 7 * (7 * 9)) * (7 + 4 + (2 + 9 + 7 + 5 * 9) * 7) + 5 + 6 + 6 + 5
4 * 2 + 4 + 9 * (4 * 4 * 7 + 9 * 7 * (4 * 2 * 2 + 2 * 4 + 4)) * 8
6 * (6 + 6 * (2 + 2 + 6 * 9 + 4 * 3) + 4 * 4 * (3 * 2 * 8 * 2)) * 7 * 2
5 * 7 * ((7 * 2 * 2 * 2) * 5)
5 * (4 + 9) + 3
4 + (7 + 7 + 3 + 5) + (7 * 8)
8 + 9 + (9 + (6 + 2) * (6 + 9) * (6 + 5 * 2 + 7 + 3) * 8 + 9) * 8 * 3
8 + (5 + (2 + 7 + 9) + 2 * 9 * 5) + 4 + 2 + 7
(3 * 6 + 3 + 6) * 9 * 7 + 2 + 7
9 * 4 + 3 + 3 + 2 + 5
8 + (3 + 8 + 6 + 2) * (9 * 6 * 5) * 3 + 2 + 4
6 * ((7 * 7) * 7 + 9)
5 + 7 + (2 * 5 + (8 + 2 * 5 * 3 * 6)) + 8 * 8
(8 + 5) + 3 * (3 * 5 * 9 * (9 + 3)) * ((5 * 2 + 9 + 3 * 3) + 3) * 9
5 + 4 + 5 + 3 * (5 + 9)
9 + 5 + 5 + 3 * (3 + 5 + 7 * 5 * (4 + 3 + 6)) + 2
2 * ((3 * 7 + 9 + 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
3 * 3 + (2 + (8 + 9 * 4 + 2)) + 5 * 3 * (7 + 2 * 4 * 9 + 5)
4 * 4 * (2 * 9 + 3 + 4) * 4 + (5 + 4 * 6 * 6)
8 + (6 * 6 * 5) * (3 * 8 + 4 * 7 + 8) * 2
9 + 4 + (9 + 3 * 7 + 9) * 7 + 7
2 + 4 + (9 + 3 * 2 * (5 * 7) + (5 + 6 * 4))
(3 + 9 * 8 * 7 * 9) + 4 + 4 * 4 * (9 * 6 + (2 * 7 + 3 + 6 * 5 * 5) + 8 + 7) * (2 * (2 * 6) + 9 + (8 * 6 + 6 + 8))
(2 + 3 * (5 + 8 * 3) * (8 + 3) + 9 * 8) + 5

30
2020/day18/Makefile Normal file
View File

@@ -0,0 +1,30 @@
INPUT := INPUT.txt
SHELL := /bin/bash
#CFLAGS := -w -g
#CFLAGS := -w -g -pg
CFLAGS := -w -O3
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
output:
@$(MAKE) --no-print-directory all 2>&1 > OUTPUT
compile: ex1-c ex2-c
ex1: ex12-c
@$(TIME) ex1.bash < $(INPUT) 2>&1
@$(TIME) ex12-c 1 < $(INPUT) 2>&1
ex2: ex12-c
@$(TIME) ex2.bash < $(INPUT) 2>&1
@$(TIME) ex12-c 2 < $(INPUT) 2>&1
clean:
@rm -f ex1-c ex2-c core
deploy:
@$(MAKE) -C .. deploy

16
2020/day18/OUTPUT Normal file
View File

@@ -0,0 +1,16 @@
ex1.bash : res=650217205854
time: 0:01.80 real, 1.78 user, 0.01 sys
context-switch: 28+1, page-faults: 0+190
ex12-c : res=650217205854
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+75
ex2.bash : res=20394514442037
time: 0:01.56 real, 1.55 user, 0.01 sys
context-switch: 16+1, page-faults: 0+196
ex12-c : res=20394514442037
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+74

66
2020/day18/README Normal file
View File

@@ -0,0 +1,66 @@
--- Day 18: Operation Order ---
As you look out the window and notice a heavily-forested continent slowly appear over the horizon, you are interrupted by the child sitting next to you. They're curious if you could help them with their math homework.
Unfortunately, it seems like this "math" follows different rules than you remember.
The homework (your puzzle input) consists of a series of expressions that consist of addition (+), multiplication (*), and parentheses ((...)). Just like normal math, parentheses indicate that the expression inside must be evaluated before it can be used by the surrounding expression. Addition still finds the sum of the numbers on both sides of the operator, and multiplication still finds the product.
However, the rules of operator precedence have changed. Rather than evaluating multiplication before addition, the operators have the same precedence, and are evaluated left-to-right regardless of the order in which they appear.
For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6 are as follows:
1 + 2 * 3 + 4 * 5 + 6
3 * 3 + 4 * 5 + 6
9 + 4 * 5 + 6
13 * 5 + 6
65 + 6
71
Parentheses can override this order; for example, here is what happens if parentheses are added to form 1 + (2 * 3) + (4 * (5 + 6)):
1 + (2 * 3) + (4 * (5 + 6))
1 + 6 + (4 * (5 + 6))
7 + (4 * (5 + 6))
7 + (4 * 11 )
7 + 44
51
Here are a few more examples:
2 * 3 + (4 * 5) becomes 26.
5 + (8 * 3 + 9 + 3 * 4 * 3) becomes 437.
5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 12240.
((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 13632.
Before you can help with the homework, you need to understand it yourself. Evaluate the expression on each line of the homework; what is the sum of the resulting values?
Your puzzle answer was 650217205854.
--- Part Two ---
You manage to answer the child's questions and they finish part 1 of their homework, but get stuck when they reach the next section: advanced math.
Now, addition and multiplication have different precedence levels, but they're not the ones you're familiar with. Instead, addition is evaluated before multiplication.
For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6 are now as follows:
1 + 2 * 3 + 4 * 5 + 6
3 * 3 + 4 * 5 + 6
3 * 7 * 5 + 6
3 * 7 * 11
21 * 11
231
Here are the other examples from above:
1 + (2 * 3) + (4 * (5 + 6)) still becomes 51.
2 * 3 + (4 * 5) becomes 46.
5 + (8 * 3 + 9 + 3 * 4 * 3) becomes 1445.
5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 669060.
((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 23340.
What do you get if you add up the results of evaluating the homework problems using these new rules?
Your puzzle answer was 20394514442037.
Both parts of this puzzle are complete! They provide two gold stars: **

6
2020/day18/TEST.txt Normal file
View File

@@ -0,0 +1,6 @@
1 + 2 * 3 + 4 * 5 + 6
1 + (2 * 3) + (4 * (5 + 6))
2 * 3 + (4 * 5)
5 + (8 * 3 + 9 + 3 * 4 * 3)
5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))
((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2

121
2020/day18/ex1.bash Executable file
View File

@@ -0,0 +1,121 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 18/game 1.
CMD=${0##*/}
set -o noglob
# shellcheck disable=2034
declare -a STACK=()
declare -a LINE=()
function enqueue() {
local -n stack="$1"
local var="$2"
stack=("$var" "${stack[@]}")
}
function dequeue() {
# shellcheck disable=2178
local -n stack="$1"
local -n var="$2"
local last=$((${#stack[@]} - 1))
var=${stack[$last]}
unset 'stack[$last]'
stack=( "${stack[@]}" ) # pack
}
function eval_op() {
# shellcheck disable=2034
local -n lvalop="$1" loptop="$2"
# shellcheck disable=2034
local v v1 v2 op res=-1
while ((${#lvalop[@]} > 1)); do
dequeue lvalop v1
dequeue lvalop v2
dequeue loptop op
# shellcheck disable=SC1102
v=$((v1 "$op" v2))
enqueue lvalop "$v"
((res++))
done
return "$res"
}
function find_paren() {
local -n expr_="$1"
local -n var_="$2"
local -i i
local k val
var_=-1
for k in "${!expr_[@]}"; do
val=${expr_[$k]}
[[ $val == '(' ]] && i="$k"
if [[ $val == ')' ]]; then
# shellcheck disable=SC2034
var_="$i"
break
fi
done
}
function eval_expr() {
local -i cur="$2" val len=${#LINE[@]}
local op
# shellcheck disable=SC2034
local -a lstack=() lop=() LINE_=()
while ((cur < len)); do
symbol=${LINE[$cur]}
case "$symbol" in
[0-9]*)
enqueue lstack "${LINE[$cur]}"
unset 'LINE[$cur]'
;;
\))
break
;;
[\+\*]*)
enqueue lop "${LINE[$cur]}"
unset 'LINE[$cur]'
;;
*)
exit 1
;;
esac
eval_op lstack lop
((cur++))
done
dequeue lstack val
LINE[$cur]="$val"
LINE=("${LINE[@]}") # pack
}
declare -i res=0 paren=0
while read -r line; do
# cleanup input line, force split around operators and parens
line=${line//\*/ * }
line=${line//+/ + }
line=${line//-/ - }
line=${line//\// \/ }
line=${line//\(/ \( }
line=${line//\)/ \) }
read -ra LINE <<< "$line"
paren=0
while ((${#LINE[@]} > 1 && paren >= 0)); do
find_paren LINE paren
if (( paren >= 0 )); then
unset 'LINE[$paren]'
LINE=("${LINE[@]}") # pack
eval_expr STACK "$paren"
fi
done
if ((${#LINE[@]} > 1)); then
eval_expr STACK 0
fi
((res+=LINE[0]))
done
printf "%s : res=%d\n" "$CMD" "$res"
exit 0

180
2020/day18/ex12-c.c Normal file
View File

@@ -0,0 +1,180 @@
/* ex12-c: Advent2020 game, day 18/tasks 1 & 2
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define T_LPAR (-'(')
#define T_RPAR (-')')
#define T_PLUS (-'+')
#define T_MULT (-'*')
#define T_ERR (-'E')
#define T_OK (-'O')
#define T_END (-'$')
#define LEN_MAX 1024
#define NPUSH(n) (push(&nstack, (n)))
#define OPUSH(o) (push(&ostack, (o)))
#define NPOP() (pop(&nstack))
#define OPOP() (pop(&ostack))
#define NTOP() (top(&nstack))
#define OTOP() (top(&ostack))
#define OEMPTY() (empty(&ostack))
#define DIGIT(c) (((c) >= '0') && ((c) <= '9'))
static struct stack {
int last;
long elt[LEN_MAX];
} nstack, ostack;
static char *saveptr=NULL;
static int prio_1(long op)
{
return op==T_PLUS || op==T_MULT? 1: 0;
}
static int prio_2(long op)
{
return op==T_PLUS? 2: op==T_MULT? 1: 0;
}
static int (*prio)()=&prio_1;
static long push(struct stack *s, long val)
{
s->elt[s->last++]=val;
return val;
}
static long pop(struct stack *s)
{
return s->elt[--s->last];
}
static long top(struct stack *s)
{
return s->elt[s->last-1];
}
static long empty(struct stack *s)
{
return s->last==0;
}
static long get_tok()
{
char *p, c;
long val=0;
p=saveptr;
while (!val) {
c=*p;
switch (c) {
case ' ':
break;
case '(':
case ')':
case '*':
case '+':
val=-c;
break;
case '\n':
case '\0':
val=T_END;
break;
default:
if (! DIGIT(c)) {
val=T_ERR;
break;
}
while (DIGIT(c)) {
val=(val*10 + c - '0');
p++;
c=*p;
}
p--;
break;
}
p++;
}
saveptr=p;
return val;
}
static long eval_top()
{
long val2 = NPOP();
long val1 = NPOP();
char op = OPOP();
NPUSH(op==T_PLUS? val1+val2: val1*val2);
return NTOP();
}
static long eval_expr()
{
long res=T_LPAR;
res=get_tok();
while (res!=T_ERR && res!=T_END) {
switch (res) {
case T_LPAR:
OPUSH(res);
break;
case T_RPAR:
while(!OEMPTY() && OTOP() != T_LPAR)
eval_top();
if(!OEMPTY()) // remove '('
OPOP();
break;
case T_PLUS:
case T_MULT:
while (!OEMPTY() && (*prio)(OTOP()) >= (*prio)(res))
eval_top();
OPUSH(res);
break;
default:
NPUSH(res);
break;
}
res=get_tok();
}
while(!OEMPTY())
eval_top();
return NPOP();
}
int main(ac, av)
int ac;
char **av;
{
char line[1024];
long res=0, tmp;
if (ac != 2) {
fprintf(stderr, "usage: %s [1|2]\n", *av);
exit (1);
}
if (**(av+1) == '2')
prio=&prio_2;
while (fgets(line, sizeof line, stdin)) {
saveptr=line;
tmp=eval_expr();
res+=tmp;
}
printf("%s : res=%ld\n", *av, res);
exit (0);
}

168
2020/day18/ex2.bash Executable file
View File

@@ -0,0 +1,168 @@
#!/bin/bash
#
# ex2.bash: Advent2020 game, day 18/game 2.
CMD=${0##*/}
set -o noglob
# shellcheck disable=2034
declare -a STACK=()
declare -a LINE=()
function enqueue() {
local -n stack="$1"
local var="$2"
stack=("$var" "${stack[@]}")
}
function dequeue() {
# shellcheck disable=2178
local -n stack="$1"
local -n var="$2"
local last=$((${#stack[@]} - 1))
var=${stack[$last]}
unset 'stack[$last]'
stack=( "${stack[@]}" ) # pack
}
function getqueue() {
# shellcheck disable=2178
local -n stack="$1"
local -n var="$2"
local pos="$3"
var=${stack[$pos]}
unset 'stack[$pos]'
#stack=( "${stack[@]}" ) # DO NOT pack !
}
function setqueue() {
local -n stack="$1"
local val="$2"
local pos="$3"
stack[$pos]="$val"
}
function print_stack() {
# shellcheck disable=2178
local -n stack="$1"
local k
printf "stack [%s:%d]: " "$1" "${#stack[@]}"
for k in "${!stack[@]}"; do
printf "%s:%s " "$k" "${stack[$k]}"
done
printf "\n"
}
function eval_plus() {
local -n lvalop="$1" loptop="$2"
local -i v v1 v2 pos last="${#loptop[@]}"
for ((pos=0; pos<last; ++pos)); do
if [[ "${loptop[$pos]}" == '+' ]]; then
getqueue lvalop v1 "$pos"
getqueue lvalop v2 "$((pos+1))"
getqueue loptop op "$pos"
# shellcheck disable=SC1102
v=$((v1 + v2))
setqueue lvalop "$v" $((pos+1))
fi
done
lvalop=("${lvalop[@]}") # pack arrays
loptop=("${loptop[@]}")
}
function eval_op() {
# shellcheck disable=2034,2178
local -n lvalop="$1" loptop="$2"
# shellcheck disable=2034
local v v1 v2 op res=-1
while ((${#lvalop[@]} > 1)); do
dequeue lvalop v1
dequeue lvalop v2
dequeue loptop op
# shellcheck disable=SC1102
v=$((v1 "$op" v2))
enqueue lvalop "$v"
((res++))
done
return "$res"
}
function find_paren() {
local -n expr_="$1"
local -n var_="$2"
local -i i
local k val
var_=-1
for k in "${!expr_[@]}"; do
val=${expr_[$k]}
[[ $val == '(' ]] && i="$k"
if [[ $val == ')' ]]; then
# shellcheck disable=SC2034
var_="$i"
break
fi
done
}
function eval_expr() {
local -i cur="$2" val len=${#LINE[@]}
local op
# shellcheck disable=SC2034
local -a lstack=() lop=() LINE_=()
while ((cur < len)); do
symbol=${LINE[$cur]}
case "$symbol" in
[0-9]*)
enqueue lstack "${LINE[$cur]}"
unset 'LINE[$cur]'
;;
\))
break
;;
[\+\*]*)
enqueue lop "${LINE[$cur]}"
unset 'LINE[$cur]'
;;
*)
exit 1
;;
esac
((cur++))
done
eval_plus lstack lop
eval_op lstack lop
dequeue lstack val
LINE[$cur]="$val"
LINE=("${LINE[@]}") # pack
}
declare -i res=0 paren=0
while read -r line; do
# cleanup input line, force split around operators and parens
line=${line//\*/ * }
line=${line//+/ + }
line=${line//-/ - }
line=${line//\// \/ }
line=${line//\(/ \( }
line=${line//\)/ \) }
read -ra LINE <<< "$line"
paren=0
while ((${#LINE[@]} > 1 && paren >= 0)); do
find_paren LINE paren
if (( paren >= 0 )); then
unset 'LINE[$paren]'
LINE=("${LINE[@]}") # pack
eval_expr STACK "$paren"
fi
done
if ((${#LINE[@]} > 1)); then
eval_expr STACK 0
fi
((res+=LINE[0]))
done
printf "%s : res=%d\n" "$CMD" "$res"
exit 0