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

1
2020/day15/INPUT.txt Normal file
View File

@@ -0,0 +1 @@
0,13,1,8,6,15

30
2020/day15/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: ex1-c
@$(TIME) ex1.bash 2020 < $(INPUT) 2>&1
@$(TIME) ex1-c 2020 < $(INPUT) 2>&1
ex2: ex1-c
@$(TIME) ex1.bash 30000000 < $(INPUT) 2>&1
@$(TIME) ex1-c 30000000 < $(INPUT) 2>&1
clean:
@rm -f ex1-c ex2-c core
deploy:
@$(MAKE) -C .. deploy

15
2020/day15/OUTPUT Normal file
View File

@@ -0,0 +1,15 @@
ex1.bash : res[2020]=1618
time: 0:00.03 real, 0.02 user, 0.00 sys
context-switch: 6+1, page-faults: 0+158
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

8
2020/day15/OUTPUT.bash Normal file
View File

@@ -0,0 +1,8 @@
ex1.bash : res=1618
time: 0:00.07 real, 0.07 user, 0.00 sys
context-switch: 7+1, page-faults: 0+172
ex3.bash : res=548531
time: 12:22:02 real, 44488.91 user, 2.70 sys
context-switch: 4552388+2, page-faults: 0+98912

65
2020/day15/README Normal file
View File

@@ -0,0 +1,65 @@
--- Day 15: Rambunctious Recitation ---
You catch the airport shuttle and try to book a new flight to your vacation island. Due to the storm, all direct flights have been cancelled, but a route is available to get around the storm. You take it.
While you wait for your flight, you decide to check in with the Elves back at the North Pole. They're playing a memory game and are ever so excited to explain the rules!
In this game, the players take turns saying numbers. They begin by taking turns reading from a list of starting numbers (your puzzle input). Then, each turn consists of considering the most recently spoken number:
If that was the first time the number has been spoken, the current player says 0.
Otherwise, the number had been spoken before; the current player announces how many turns apart the number is from when it was previously spoken.
So, after the starting numbers, each turn results in that player speaking aloud either 0 (if the last number is new) or an age (if the last number is a repeat).
For example, suppose the starting numbers are 0,3,6:
Turn 1: The 1st number spoken is a starting number, 0.
Turn 2: The 2nd number spoken is a starting number, 3.
Turn 3: The 3rd number spoken is a starting number, 6.
Turn 4: Now, consider the last number spoken, 6. Since that was the first time the number had been spoken, the 4th number spoken is 0.
Turn 5: Next, again consider the last number spoken, 0. Since it had been spoken before, the next number to speak is the difference between the turn number when it was last spoken (the previous turn, 4) and the turn number of the time it was most recently spoken before then (turn 1). Thus, the 5th number spoken is 4 - 1, 3.
Turn 6: The last number spoken, 3 had also been spoken before, most recently on turns 5 and 2. So, the 6th number spoken is 5 - 2, 3.
Turn 7: Since 3 was just spoken twice in a row, and the last two turns are 1 turn apart, the 7th number spoken is 1.
Turn 8: Since 1 is new, the 8th number spoken is 0.
Turn 9: 0 was last spoken on turns 8 and 4, so the 9th number spoken is the difference between them, 4.
Turn 10: 4 is new, so the 10th number spoken is 0.
(The game ends when the Elves get sick of playing or dinner is ready, whichever comes first.)
Their question for you is: what will be the 2020th number spoken? In the example above, the 2020th number spoken will be 436.
Here are a few more examples:
Given the starting numbers 1,3,2, the 2020th number spoken is 1.
Given the starting numbers 2,1,3, the 2020th number spoken is 10.
Given the starting numbers 1,2,3, the 2020th number spoken is 27.
Given the starting numbers 2,3,1, the 2020th number spoken is 78.
Given the starting numbers 3,2,1, the 2020th number spoken is 438.
Given the starting numbers 3,1,2, the 2020th number spoken is 1836.
Given your starting numbers, what will be the 2020th number spoken?
Your puzzle answer was 1618.
--- Part Two ---
Impressed, the Elves issue you a challenge: determine the 30000000th number spoken. For example, given the same starting numbers as above:
Given 0,3,6, the 30000000th number spoken is 175594.
Given 1,3,2, the 30000000th number spoken is 2578.
Given 2,1,3, the 30000000th number spoken is 3544142.
Given 1,2,3, the 30000000th number spoken is 261214.
Given 2,3,1, the 30000000th number spoken is 6895259.
Given 3,2,1, the 30000000th number spoken is 18.
Given 3,1,2, the 30000000th number spoken is 362.
Given your starting numbers, what will be the 30000000th number spoken?
Your puzzle answer was 548531.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
Your puzzle input was 0,13,1,8,6,15.
You can also [Share] this puzzle.

1
2020/day15/TEST.txt Normal file
View File

@@ -0,0 +1 @@
0,3,6

1
2020/day15/TEST2.txt Normal file
View File

@@ -0,0 +1 @@
1,3,2

1
2020/day15/TEST3.txt Normal file
View File

@@ -0,0 +1 @@
2,1,3

View File

@@ -0,0 +1,65 @@
#!/bin/bash
#
# ex1-2-slow.bash: Advent2020 game, day 15/games 1 and 2.
# ===> Too slow for exercise 2, needed to rewrite the algorithm.
CMD=${0##*/}
#shopt -s extglob
declare -A prev nums
declare -i cur=0 last
function print_binary() {
local -i num=$1 exp
local str="" format="%0${2}d"
for ((exp=1; num>exp; exp<<=1)); do
if ((num & exp)); then
str=1"$str"
else
str=0"$str"
fi
echo " str=$str"
done
printf "$format" $str
}
function print_array() {
local -i i
printf "last: %d\n" "$last"
printf "nums:\n"
for i in "${!nums[@]}"; do
printf " %6d => %d\n" "$i" "${nums[$i]}" >&2
if [[ -v prev[$i] ]]; then
printf " prev => %d\n" "${prev[$i]}" >&2
fi
done
}
TARGET=$1
IFS=","
read -r line
set -- $line
while (($# > 0)); do
nums[$1]=$cur
last=$1
((cur++))
shift
done
for ((; cur != TARGET; ++cur)); do
diff=0
if [[ -v prev[$last] ]]; then
((diff=nums[$last]-prev[$last]))
prev[$last]=${nums[$last]}
fi
[[ -v nums[$diff] ]] && prev[$diff]=${nums[$diff]}
nums[$diff]=$cur
last=$diff
done
printf "%s : res=%d\n" "$CMD" "$last"
exit 0

40
2020/day15/ex1-c.c Normal file
View 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);
}

34
2020/day15/ex1.bash Executable file
View File

@@ -0,0 +1,34 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 15/games 1 and 2.
CMD=${0##*/}
#shopt -s extglob
declare -A prev
declare -i cur=1 previous
TARGET=$1
IFS=","
read -r line
set -- $line
while (($# > 0)); do
prev[$1]=$cur
((cur++))
shift
done
for ((previous=0; cur <= TARGET; ++cur)); do
if ((previous)); then
((diff=cur-previous-1))
else
diff=0
fi
((previous=prev[$diff]))
prev[$diff]=$cur
done
printf "%s : res[%d]=%d\n" "$CMD" "$TARGET" "$diff"
exit 0