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

2199
2020/day06/INPUT.txt Normal file

File diff suppressed because it is too large Load Diff

41
2020/day06/Makefile Normal file
View File

@@ -0,0 +1,41 @@
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: compile
@$(TIME) ex1.bash < $(INPUT) 2>&1
@$(TIME) ex1-bis.bash < $(INPUT) 2>&1
@$(TIME) ex1-c < $(INPUT) 2>&1
ex2: compile
@$(TIME) ex2.bash < $(INPUT) 2>&1
@$(TIME) ex2-bis.bash < $(INPUT) 2>&1
@$(TIME) ex2-c < $(INPUT) 2>&1
bash:
@$(TIME) $(MAKE) --no-print-directory bash-run
c: compile
@$(TIME) $(MAKE) --no-print-directory c-run
bash-run:
@ex1.bash < $(INPUT) 2>&1
@ex2.bash < $(INPUT) 2>&1
c-run:
@ex1-c < $(INPUT) 2>&1
@ex2-c < $(INPUT) 2>&1
clean:
@rm -f ex1-c ex2-c core
deploy:
@$(MAKE) -C .. deploy

24
2020/day06/OUTPUT Normal file
View File

@@ -0,0 +1,24 @@
ex1.bash : groups=484 count=6585
time: 0:01.42 real, 1.26 user, 0.20 sys
context-switch: 135+1393, page-faults: 0+59578
ex1-bis.bash : groups=484 count=6585
time: 0:01.22 real, 1.09 user, 0.16 sys
context-switch: 145+1311, page-faults: 0+60076
ex1-c : groups=484 count=6585
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+71
ex2.bash : groups=484 count=3276
time: 0:01.35 real, 1.18 user, 0.20 sys
context-switch: 139+1360, page-faults: 0+59397
ex2-bis.bash : groups=484 count=3276
time: 0:01.30 real, 1.11 user, 0.22 sys
context-switch: 112+1356, page-faults: 0+57986
ex2-c : groups=484 count=3276
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+72

90
2020/day06/README Normal file
View File

@@ -0,0 +1,90 @@
--- Day 6: Custom Customs ---
As your flight approaches the regional airport where you'll switch to a much larger plane, customs declaration forms are distributed to the passengers.
The form asks a series of 26 yes-or-no questions marked a through z. All you need to do is identify the questions for which anyone in your group answers "yes". Since your group is just you, this doesn't take very long.
However, the person sitting next to you seems to be experiencing a language barrier and asks if you can help. For each of the people in their group, you write down the questions for which they answer "yes", one per line. For example:
abcx
abcy
abcz
In this group, there are 6 questions to which anyone answered "yes": a, b, c, x, y, and z. (Duplicate answers to the same question don't count extra; each question counts at most once.)
Another group asks for your help, then another, and eventually you've collected answers from every group on the plane (your puzzle input). Each group's answers are separated by a blank line, and within each group, each person's answers are on a single line. For example:
abc
a
b
c
ab
ac
a
a
a
a
b
This list represents answers from five groups:
The first group contains one person who answered "yes" to 3 questions: a, b, and c.
The second group contains three people; combined, they answered "yes" to 3 questions: a, b, and c.
The third group contains two people; combined, they answered "yes" to 3 questions: a, b, and c.
The fourth group contains four people; combined, they answered "yes" to only 1 question, a.
The last group contains one person who answered "yes" to only 1 question, b.
In this example, the sum of these counts is 3 + 3 + 3 + 1 + 1 = 11.
For each group, count the number of questions to which anyone answered "yes". What is the sum of those counts?
Your puzzle answer was 6585.
--- Part Two ---
As you finish the last group's customs declaration, you notice that you misread one word in the instructions:
You don't need to identify the questions to which anyone answered "yes"; you need to identify the questions to which everyone answered "yes"!
Using the same example as above:
abc
a
b
c
ab
ac
a
a
a
a
b
This list represents answers from five groups:
In the first group, everyone (all 1 person) answered "yes" to 3 questions: a, b, and c.
In the second group, there is no question to which everyone answered "yes".
In the third group, everyone answered yes to only 1 question, a. Since some people did not answer "yes" to b or c, they don't count.
In the fourth group, everyone answered yes to only 1 question, a.
In the fifth group, everyone (all 1 person) answered "yes" to 1 question, b.
In this example, the sum of these counts is 3 + 0 + 1 + 1 + 1 = 6.
For each group, count the number of questions to which everyone answered "yes". What is the sum of those counts?
Your puzzle answer was 3276.
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.
If you still want to see it, you can get your puzzle input.
You can also [Share] this puzzle.

15
2020/day06/TEST.txt Normal file
View File

@@ -0,0 +1,15 @@
abc
a
b
c
ab
ac
a
a
a
a
b

4
2020/day06/bash.sh Executable file
View File

@@ -0,0 +1,4 @@
#! /bin/bash
PATH=.:$PATH
IN="$1"
time { ex1.bash < $IN; ex2.bash < $IN; } 2>&1

5
2020/day06/c.sh Executable file
View File

@@ -0,0 +1,5 @@
#! /bin/bash
PATH=.:$PATH
IN="$1"
echo IN=$IN
time for i in ex1-c ex2-c; do "$i" < "$IN"; done 2>&1

73
2020/day06/ex1-bis.bash Executable file
View File

@@ -0,0 +1,73 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 6/game 1.
CMD=${0##*/}
shopt -s extglob
declare -a Q=()
printf -v AC "%d" "'a"
printf -v ZC "%d" "'z"
(( NC = ZC-AC+1 ))
function reset_array() {
Q=()
#for (( elt=AC; elt<=ZC; ++elt )); do
# Q[$elt]=0
#done
}
function count_array() {
#local -i count
echo ${#Q[@]}
#for ${!Q[@]}; do
# ((Q[elt] > 0)) && ((count++))
# #printf "%d=%d (%d)" "$elt" "${Q[$elt]}" "$count"
#done
#echo $count
}
function set_array() {
local str="$1"
local c asc
for ((i=0; i<${#str}; ++i)); do
c=${str:$i:1}
if [[ "$c" =~ [a-z] ]]; then
printf -v asc "%d" "'$c"
Q[$asc]=1
fi
done
}
declare -i people=0 group=0 count=0
declare line grouped
function calcgroup() {
set_array "$grouped"
((group++))
i=$(count_array)
#printf "c=%d" $i
((count += i))
((people=0))
grouped=""
reset_array
}
while read -r line; do
if [[ -z "$line" ]]; then
#echo "G=$grouped"
#set_array "$grouped"
calcgroup
#for (( elt=AC; elt<=ZC; ++elt )); do
#printf "%s=%s " "$elt" "${Q[$elt]}"
#done
#echo
continue
fi
((people++))
grouped+="$line"
done
calcgroup
printf "%s : groups=%d count=%d\n" "$CMD" $group $count
exit 0

78
2020/day06/ex1-c.c Normal file
View File

@@ -0,0 +1,78 @@
/* ex1-c: Advent2020 game, day 6/game 1
*/
#include <stdio.h>
#include <stdlib.h>
#define QSIZE ('z'-'a'+1)
static int Q[QSIZE];
void reset_array(){
int i;
for (i=0; i<QSIZE; ++i)
Q[i]=0;
}
void print_array()
{
int i;
for (i=0; i<QSIZE; i++) {
printf("%d ", Q[i]);
}
printf("\n");
}
void set_array(str)
char *str;
{
for (; *str; str++) {
if (*str >= 'a' && *str <= 'z')
Q[*str-'a']++;
}
}
int count_array ()
{
int count=0, i;
for (i=0; i<QSIZE; ++i) {
if (Q[i] > 0)
count++;
}
return count;
}
void calcgroup(group, count, people)
int *group, *count, *people;
{
(*group)++;
*count += count_array();
*people=0;
reset_array();
}
int main(ac, av)
char **av;
{
int people=0, group=0, count=0;
char line[80];
while (fgets(line, sizeof line, stdin)) {
if (*line == '\n') {
calcgroup(&group, &count, &people);
continue;
}
people++;
set_array(line);
//sscanf(line, "%[FB]%[RL]", rowstr, colstr);
//cur=(str2int(rowstr)<<3) ^ str2int(colstr);
// *(pseats+cur)=1;
}
//for (; *pseats == 0; ++pseats)
// ;
//for (; *pseats == 1; ++pseats)
// ;
//printf("%s : lines=%d seat=%ld\n", *av, count, pseats-(int *)seats);
calcgroup(&group, &count, &people);
printf("%s : groups=%d count=%d\n", *av, group, count);
exit (0);
}

55
2020/day06/ex1.bash Executable file
View File

@@ -0,0 +1,55 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 6/game 1.
CMD=${0##*/}
shopt -s extglob
declare -A Q=()
function reset_array() {
for elt in {a..z}; do
Q[$elt]=0
done
}
function count_array() {
local -i count
for elt in "${!Q[@]}"; do
(( Q[$elt] > 0 )) && ((count++))
done
echo $count
}
function set_array() {
local str="$1"
local c
for ((i=0; i<${#str}; ++i)); do
c=${str:$i:1}
if [[ "$c" =~ [a-z] ]]; then
Q[$c]=1
fi
done
}
declare -i people=0 group=0 count=0
function calcgroup() {
((group++))
((count += $(count_array)))
((people=0))
reset_array
}
while read -r line; do
if [[ -z "$line" ]]; then
calcgroup
continue
fi
((people++))
set_array "$line"
done
calcgroup
printf "%s : groups=%d count=%d\n" "$CMD" $group $count
exit 0

59
2020/day06/ex2-bis.bash Executable file
View File

@@ -0,0 +1,59 @@
#!/bin/bash
#
# ex2-bis.bash: Advent2020 game, day 6/game 2.
CMD=${0##*/}
shopt -s extglob
declare -A Q=()
function reset_array() {
Q=()
#for elt in {a..z}; do
# Q[$elt]=0
#done
}
function count_array() {
local -i count=0 people="$1"
for elt in "${!Q[@]}"; do
if (( Q[$elt] == people )); then
((count++))
fi
done
echo $count
}
function set_array() {
local str="$1"
local c
for ((i=0; i<${#str}; ++i)); do
c=${str:$i:1}
if [[ "$c" =~ [a-z] ]]; then
(( Q[$c]++ ))
fi
done
}
declare -i people=0 group=0 count=0
function calcgroup() {
local -i i
i=$(count_array $people)
((group++))
(( count += i ))
((people=0))
reset_array
}
while read -r line; do
if [[ -z "$line" ]]; then
calcgroup
continue
fi
((people++))
set_array "$line"
done
calcgroup
printf "%s : groups=%d count=%d\n" "$CMD" $group $count
exit 0

79
2020/day06/ex2-c.c Normal file
View File

@@ -0,0 +1,79 @@
/* ex2-c: Advent2020 game, day 6/game 2
*/
#include <stdio.h>
#include <stdlib.h>
#define QSIZE ('z'-'a'+1)
static int Q[QSIZE];
void reset_array(){
int i;
for (i=0; i<QSIZE; ++i)
Q[i]=0;
}
void print_array()
{
int i;
for (i=0; i<QSIZE; i++) {
printf("%d ", Q[i]);
}
printf("\n");
}
void set_array(str)
char *str;
{
for (; *str; str++) {
if (*str >= 'a' && *str <= 'z')
Q[*str-'a']++;
}
}
int count_array (people)
int people;
{
int count=0, i;
for (i=0; i<QSIZE; ++i) {
if (Q[i] == people)
count++;
}
return count;
}
void calcgroup(group, count, people)
int *group, *count, *people;
{
(*group)++;
*count += count_array(*people);
*people=0;
reset_array();
}
int main(ac, av)
char **av;
{
int people=0, group=0, count=0;
char line[80];
while (fgets(line, sizeof line, stdin)) {
if (*line == '\n') {
calcgroup(&group, &count, &people);
continue;
}
people++;
set_array(line);
//sscanf(line, "%[FB]%[RL]", rowstr, colstr);
//cur=(str2int(rowstr)<<3) ^ str2int(colstr);
// *(pseats+cur)=1;
}
//for (; *pseats == 0; ++pseats)
// ;
//for (; *pseats == 1; ++pseats)
// ;
//printf("%s : lines=%d seat=%ld\n", *av, count, pseats-(int *)seats);
calcgroup(&group, &count, &people);
printf("%s : groups=%d count=%d\n", *av, group, count);
exit (0);
}

58
2020/day06/ex2.bash Executable file
View File

@@ -0,0 +1,58 @@
#!/bin/bash
#
# ex2.bash: Advent2020 game, day 6/game 2.
CMD=${0##*/}
shopt -s extglob
declare -A Q=()
function reset_array() {
for elt in {a..z}; do
Q[$elt]=0
done
}
function count_array() {
local -i count=0 people="$1"
for elt in "${!Q[@]}"; do
if (( Q[$elt] == people )); then
((count++))
fi
done
echo $count
}
function set_array() {
local str="$1"
local c
for ((i=0; i<${#str}; ++i)); do
c=${str:$i:1}
if [[ "$c" =~ [a-z] ]]; then
(( Q[$c]++ ))
fi
done
}
declare -i people=0 group=0 count=0
function calcgroup() {
local -i i
i=$(count_array $people)
((group++))
(( count += i ))
((people=0))
reset_array
}
while read -r line; do
if [[ -z "$line" ]]; then
calcgroup
continue
fi
((people++))
set_array "$line"
done
calcgroup
printf "%s : groups=%d count=%d\n" "$CMD" $group $count
exit 0