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

200
2020/day01/INPUT.txt Normal file
View File

@@ -0,0 +1,200 @@
1899
1358
1824
1541
1801
1950
1441
1848
1259
1715
1222
1097
1127
1531
1330
1841
1662
1075
1631
1844
1209
1940
2006
1085
1615
1132
1345
1736
1885
1919
1995
1893
1464
1701
1373
1390
1717
1238
1707
1088
1566
1971
1804
1630
1920
1445
1948
1123
1917
1944
1448
1965
1118
1986
1498
1847
1730
1418
1771
1352
1207
1276
1716
1502
1922
1473
1528
1038
1228
1983
1746
1695
1698
1910
1283
1145
1967
1304
1651
1898
1450
1042
1051
1619
1505
1643
1136
1517
1907
1354
1960
1733
1769
1942
43
1743
1981
1711
1141
1169
1239
1032
1148
1045
1768
1173
1389
2007
1614
1028
1729
1083
1165
1461
1850
1913
1958
1121
1613
1341
1055
1510
1054
1064
1875
1429
1799
1570
1293
1702
1313
1576
1050
1340
1117
1342
1914
1773
1281
1176
1371
1520
1131
1438
1741
1583
1840
1412
1792
1656
1628
1089
1124
1291
1476
1367
1467
1529
1925
1555
1518
1496
1745
1533
1557
1861
1091
1364
1894
1760
1622
1149
1776
1547
1329
1988
1697
989
1604
1296
319
1459
1860
1916
1838
527
1370
1881
1213
2003
1223
1095
1560
1784
1157
1573
1023
1758
1857
1723

40
2020/day01/Makefile Normal file
View File

@@ -0,0 +1,40 @@
INPUT := INPUT.txt
SHELL := /bin/bash
CFLAGS := -w
COBFLAGS=-fmfcomment -free -x
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 output
all: ex1 ex2
compile: ex1-c ex2-c ex2-pure-sort-cob ex2-sort-cob
ex1: ex1-c ex1-cob
@$(TIME) ex1.bash < $(INPUT)
@echo -n "ex1-cob : res="
@$(TIME) ex1-cob < $(INPUT)
@$(TIME) ex1-c < $(INPUT)
ex2: ex2-c ex2-sort-cob ex2-pure-sort-cob
@$(TIME) ex2.bash < $(INPUT)
@$(TIME) ex2-sort.bash < $(INPUT)
@$(TIME) ex2-pure-sort.bash < $(INPUT)
@echo -n "ex2-sort-cob : res="
@$(TIME) ex2-sort-cob < $(INPUT)
@echo -n "ex2-pure-sort-cob : res="
@$(TIME) ex2-pure-sort-cob < $(INPUT)
@$(TIME) ex2-c < $(INPUT)
clean:
@rm -f ex1-c ex2-c core ex2-galoisgirl-cob ex2-cob
output:
$(MAKE) --no-print-directory all >OUTPUT 2>&1
deploy:
$(MAKE) -C .. deploy
%: %.cob
cobc $(COBFLAGS) $^

36
2020/day01/OUTPUT Normal file
View File

@@ -0,0 +1,36 @@
ex1.bash : 33:1701 180:319 sum=2020 mul=542619
time: 0:00.06 real, 0.06 user, 0.00 sys
context-switch: 20+4, page-faults: 0+305
ex1-cob : res= 542619
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+172
ex1-c : 33:1701 180:319 sum=2020 mul=542619
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+73
ex2.bash : 80:1450 94:43 185:527 sum=2020 mul=32858450
time: 0:10.00 real, 10.00 user, 0.00 sys
context-switch: 1005+3, page-faults: 0+302
ex2-sort.bash : 0:43 2:527 81:1450 sum=2020 mul=32858450
time: 0:00.01 real, 0.01 user, 0.00 sys
context-switch: 2+7, page-faults: 0+479
ex2-pure-sort.bash : 0:43 2:527 81:1450 sum=2020 mul=32858450
time: 0:00.50 real, 0.50 user, 0.00 sys
context-switch: 62+4, page-faults: 0+311
ex2-sort-cob : res= 32858450
time: 0:00.01 real, 0.01 user, 0.00 sys
context-switch: 1+1, page-faults: 0+170
ex2-pure-sort-cob : res= 32858450
time: 0:00.01 real, 0.01 user, 0.00 sys
context-switch: 1+1, page-faults: 0+173
ex2-c : 80:1450 94:43 185:527 sum=2020 mul=32858450
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+75

45
2020/day01/README Normal file
View File

@@ -0,0 +1,45 @@
--- Day 1: Report Repair ---
After saving Christmas five years in a row, you've decided to take a vacation at a nice resort on a tropical island. Surely, Christmas will go on without you.
The tropical island has its own currency and is entirely cash-only. The gold coins used there have a little picture of a starfish; the locals just call them stars. None of the currency exchanges seem to have heard of them, but somehow, you'll need to find fifty of these coins by the time you arrive so you can pay the deposit on your room.
To save your vacation, you need to get all fifty stars by December 25th.
Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
Before you leave, the Elves in accounting just need you to fix your expense report (your puzzle input); apparently, something isn't quite adding up.
Specifically, they need you to find the two entries that sum to 2020 and then multiply those two numbers together.
For example, suppose your expense report contained the following:
1721
979
366
299
675
1456
In this list, the two entries that sum to 2020 are 1721 and 299. Multiplying them together produces 1721 * 299 = 514579, so the correct answer is 514579.
Of course, your expense report is much larger. Find the two entries that sum to 2020; what do you get if you multiply them together?
Your puzzle answer was 542619.
--- Part Two ---
The Elves in accounting are thankful for your help; one of them even offers you a starfish coin they had left over from a past vacation. They offer you a second one if you can find three numbers in your expense report that meet the same criteria.
Using the above example again, the three entries that sum to 2020 are 979, 366, and 675. Multiplying them together produces the answer, 241861950.
In your expense report, what is the product of the three entries that sum to 2020?
Your puzzle answer was 32858450.
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.

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

@@ -0,0 +1,6 @@
1721
979
366
299
675
1456

29
2020/day01/ex1-c.c Normal file
View File

@@ -0,0 +1,29 @@
/* ex1-c:c: Advent2020 game, day 1/game 1
*/
#include <stdio.h>
#include <stdlib.h>
int main(ac, av)
char **av;
{
int numbers[1000]; /* should do differently :
* either read twice, either create list
*/
int n=0, i, j, n1, n2;
while (scanf("%d", numbers+n) != EOF)
n++;
for (i=0; i<n; ++i) {
n1=numbers[i];
for (j=i+1; j<n; ++j) {
n2=numbers[j];
if (n1+n2 == 2020) {
printf("%s : %d:%d %d:%d sum=%d mul=%d\n", *av,
i, n1, j, n2, n1+n2, n1*n2);
exit (0);
}
}
}
}

79
2020/day01/ex1-cob.cob Normal file
View File

@@ -0,0 +1,79 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. AoC-2020-day-1-part-1.
*> Inspired by https://github.com/GaloisGirl/Coding
*> Compilation: cobc -F -fmfcomment -std=rm -x ex2-cob.cob
*> Utilisation: ./ex2-cob < INPUT.txt
AUTHOR. Bruno Raoult.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUTFILE ASSIGN TO KEYBOARD
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUTFILE.
01 INPUTRECORD PIC 9(4).
WORKING-STORAGE SECTION.
01 FILE-STATUS PIC 9 VALUE 0.
01 LEN PIC 9(3) VALUE 0.
01 ARRAY.
02 ARR OCCURS 0 TO 1024 DEPENDING ON LEN.
05 VAL PIC 9(5).
01 S PIC 9(4).
01 P PIC 9(12).
01 FMT PIC Z(12)9.
LOCAL-STORAGE SECTION.
01 I USAGE UNSIGNED-INT VALUE 1.
01 J USAGE UNSIGNED-INT VALUE 1.
01 K USAGE UNSIGNED-INT VALUE 1.
01 TMP USAGE UNSIGNED-INT VALUE 1.
PROCEDURE DIVISION.
01-MAIN.
OPEN INPUT INPUTFILE.
PERFORM 02-READ UNTIL FILE-STATUS = 1.
CLOSE INPUTFILE.
*> PERFORM 06-PRINT.
SORT ARR ASCENDING KEY VAL
*> DISPLAY "=========================".
*> PERFORM 06-PRINT.
PERFORM 04-LOOP
STOP RUN.
02-READ.
READ INPUTFILE
AT END MOVE 1 TO FILE-STATUS
NOT AT END PERFORM 03-WRITE-TO-TABLE
END-READ.
03-WRITE-TO-TABLE.
ADD 1 TO LEN.
UNSTRING INPUTRECORD INTO VAL(LEN).
*> Wrong: "234" becomes 2340 instead of 0234
*> COMPUTE VAL(LEN) = INPUTRECORD.
*> DISPLAY VAL(LEN) " " LEN.
04-LOOP.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN
ADD I 1 GIVING TMP
PERFORM VARYING J FROM TMP BY 1 UNTIL J > LEN
ADD VAL(I) VAL(J) GIVING S
*> DISPLAY I J K
IF S = 2020 THEN
MULTIPLY VAL(I) BY VAL(J) GIVING P
MOVE P TO FMT
DISPLAY FMT
EXIT PERFORM
ELSE
IF S > 2020 THEN
EXIT PERFORM
END-IF
END-IF
END-PERFORM
END-PERFORM.
06-PRINT.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN
DISPLAY VAL(I)
END-PERFORM.

23
2020/day01/ex1.bash Executable file
View File

@@ -0,0 +1,23 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 1/game 1
CMD=$(basename "$0")
readarray -t numbers
declare -i i j a b
for ((i=0; i<${#numbers[@]}; ++i)); do
a=$((numbers[i]))
for ((j=i+1; j<${#numbers[@]}; ++j)); do
b=$((numbers[j]))
if ((a+b == 2020)); then
printf "${CMD} : %d:%d %d:%d sum=%d mul=%d\n" \
$i $a $j $b \
$((a+b)) $((a*b))
break 2
fi
done
done
exit 0

31
2020/day01/ex2-c.c Normal file
View File

@@ -0,0 +1,31 @@
/* ex2-c:c: Advent2020 game, day 1/game 2
*/
#include <stdio.h>
#include <stdlib.h>
int main(ac, av)
char **av;
{
int numbers[1000]; /* should do differently :
* either read twice, either create list
*/
int n=0, i, j, k, n1, n2, n3;
while (scanf("%d", numbers+n) != EOF)
n++;
for (i=0; i<n; ++i) {
n1=numbers[i];
for (j=i+1; j<n; ++j) {
n2=numbers[j];
for (k=j+1; k<n; ++k) {
n3=numbers[k];
if (n1+n2+n3 == 2020) {
printf("%s : %d:%d %d:%d %d:%d sum=%d mul=%d\n", *av,
i, n1, j, n2, k, n3, n1+n2+n3, n1*n2*n3);
exit (0);
}
}
}
}
}

View File

@@ -0,0 +1,96 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. AoC-2020-day-1-part-2.
*> Inspired by https://github.com/GaloisGirl/Coding
*> Compilation: cobc -F -fmfcomment -std=rm -x ex2-cob.cob
*> Utilisation: ./ex2-cob < INPUT.txt
AUTHOR. Bruno Raoult.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUTFILE ASSIGN TO KEYBOARD
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUTFILE.
01 INPUTRECORD PIC 9(4).
WORKING-STORAGE SECTION.
01 FILE-STATUS PIC 9 VALUE 0.
01 LEN PIC 9(3) VALUE 0.
01 ARR.
05 VAL PIC 9(5) OCCURS 1024.
01 S PIC 9(4).
01 P PIC 9(12).
01 FMT PIC Z(12)9.
LOCAL-STORAGE SECTION.
01 I USAGE UNSIGNED-INT VALUE 1.
01 J USAGE UNSIGNED-INT VALUE 1.
01 K USAGE UNSIGNED-INT VALUE 1.
01 TMP USAGE UNSIGNED-INT VALUE 1.
PROCEDURE DIVISION.
01-MAIN.
OPEN INPUT INPUTFILE.
PERFORM 02-READ UNTIL FILE-STATUS = 1.
CLOSE INPUTFILE.
*> PERFORM 06-PRINT.
PERFORM 05-BUBBLE
*> DISPLAY "=========================".
*> PERFORM 06-PRINT.
PERFORM 04-LOOP
STOP RUN.
02-READ.
READ INPUTFILE
AT END MOVE 1 TO FILE-STATUS
NOT AT END PERFORM 03-WRITE-TO-TABLE
END-READ.
03-WRITE-TO-TABLE.
ADD 1 TO LEN.
UNSTRING INPUTRECORD INTO VAL(LEN).
*> Wrong: "234" becomes 2340 instead of 0234
*> COMPUTE VAL(LEN) = INPUTRECORD.
*> DISPLAY VAL(LEN) " " LEN.
04-LOOP.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN
ADD I 1 GIVING TMP
PERFORM VARYING J FROM TMP BY 1 UNTIL J > LEN
ADD J 1 GIVING TMP
PERFORM VARYING K FROM TMP BY 1 UNTIL K > LEN
ADD VAL(I) VAL(J) VAL(K) GIVING S
*> DISPLAY I J K
IF S = 2020 THEN
MULTIPLY VAL(I) BY VAL(J) GIVING P
MULTIPLY P BY VAL(K) GIVING P
MOVE P TO FMT
DISPLAY FMT
EXIT PERFORM
ELSE
IF S > 2020 THEN
EXIT PERFORM
END-IF
END-IF
END-PERFORM
END-PERFORM
END-PERFORM.
05-BUBBLE.
MOVE 1 TO K
PERFORM UNTIL K = 0
MOVE 0 TO K
PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN - 1
ADD 1 I GIVING J
IF VAL(I) > VAL(J)
MOVE VAL(I) TO TMP
MOVE VAL(J) TO VAL(I)
MOVE TMP TO VAL(J)
MOVE 1 TO K
END-IF
END-PERFORM
END-PERFORM.
06-PRINT.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN
DISPLAY VAL(I)
END-PERFORM.

55
2020/day01/ex2-pure-sort.bash Executable file
View File

@@ -0,0 +1,55 @@
#!/bin/bash
#
# ex2-pure-sort.bash: Advent2020 game, day 1/game 2, bash bubble sort.
CMD=${0##*/}
readarray -t numbers
# simple bubble sort for numeric array (ascending)
# args: 1: the array
# todo: add a parameter for asc/desc order.
sort_n() {
local -a array=( "$@" )
local -i max=$(( ${#array[@]} - 1 ))
for (( max= $(( ${#array[@]} - 1 )); max > 0; max-- )); do
local -i i
for (( i=0; i<max; i++ )); do
local -i val1=${array[$i]}
local -i val2=${array[$((i + 1))]}
# switch if necessary
if (( val1 > val2 )); then
local tmp=$val1
array[$i]=$val2
array[$((i + 1))]=$tmp
fi
done
done
echo "${array[@]}"
}
sorted=( $(sort_n "${numbers[@]}") )
declare -i i j k a b c
for ((i=0; i<${#sorted[@]}; ++i)); do
a=$((sorted[i]))
for ((j=i+1; j<${#sorted[@]}; ++j)); do
b=$((sorted[j]))
((a+b > 2020)) && break
for ((k=j+1; k<${#sorted[@]}; ++k)); do
c=$((sorted[k]))
((a+b+c > 2020)) && break
if ((a+b+c == 2020)); then
printf "${CMD} : %d:%d %d:%d %d:%d sum=%d mul=%d\n" \
$i $a $j $b $k $c \
$((a+b+c)) \
$((a*b*c))
break 3
fi
done
done
done
exit 0

View File

@@ -0,0 +1,99 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. AoC-2020-day-1-part-2.
*> Inspired by https://github.com/GaloisGirl/Coding
*> Compilation: cobc -F -fmfcomment -std=rm -x ex2-cob.cob
*> Utilisation: ./ex2-cob < INPUT.txt
AUTHOR. Bruno Raoult.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUTFILE ASSIGN TO KEYBOARD
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUTFILE.
01 INPUTRECORD PIC 9(4).
WORKING-STORAGE SECTION.
01 FILE-STATUS PIC 9 VALUE 0.
01 LEN PIC 9(3) VALUE 0.
01 ARRAY.
02 ARR OCCURS 0 TO 1024 DEPENDING ON LEN.
05 VAL PIC 9(5).
01 S PIC 9(4).
01 P PIC 9(12).
01 FMT PIC Z(12)9.
LOCAL-STORAGE SECTION.
01 I USAGE UNSIGNED-INT VALUE 1.
01 J USAGE UNSIGNED-INT VALUE 1.
01 K USAGE UNSIGNED-INT VALUE 1.
01 TMP USAGE UNSIGNED-INT VALUE 1.
PROCEDURE DIVISION.
01-MAIN.
OPEN INPUT INPUTFILE.
PERFORM 02-READ UNTIL FILE-STATUS = 1.
CLOSE INPUTFILE.
*> PERFORM 06-PRINT.
MOVE 1 TO K
*> PERFORM 05-BUBBLE
SORT ARR ASCENDING KEY VAL
*> DISPLAY "=========================".
*> PERFORM 06-PRINT.
PERFORM 04-LOOP
STOP RUN.
02-READ.
READ INPUTFILE
AT END MOVE 1 TO FILE-STATUS
NOT AT END PERFORM 03-WRITE-TO-TABLE
END-READ.
03-WRITE-TO-TABLE.
ADD 1 TO LEN.
UNSTRING INPUTRECORD INTO VAL(LEN).
*> Wrong: "234" becomes 2340 instead of 0234
*> COMPUTE VAL(LEN) = INPUTRECORD.
*> DISPLAY VAL(LEN) " " LEN.
04-LOOP.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN
ADD I 1 GIVING TMP
PERFORM VARYING J FROM TMP BY 1 UNTIL J > LEN
ADD J 1 GIVING TMP
PERFORM VARYING K FROM TMP BY 1 UNTIL K > LEN
ADD VAL(I) VAL(J) VAL(K) GIVING S
*> DISPLAY I J K
IF S = 2020 THEN
MULTIPLY VAL(I) BY VAL(J) GIVING P
MULTIPLY P BY VAL(K) GIVING P
MOVE P TO FMT
DISPLAY FMT
EXIT PERFORM
ELSE
IF S > 2020 THEN
EXIT PERFORM
END-IF
END-IF
END-PERFORM
END-PERFORM
END-PERFORM.
05-BUBBLE.
MOVE 1 TO K
PERFORM UNTIL K = 0
MOVE 0 TO K
PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN - 1
ADD 1 I GIVING J
IF VAL(I) > VAL(J)
MOVE VAL(I) TO TMP
MOVE VAL(J) TO VAL(I)
MOVE TMP TO VAL(J)
MOVE 1 TO K
END-IF
END-PERFORM
END-PERFORM.
06-PRINT.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > LEN
DISPLAY VAL(I)
END-PERFORM.

28
2020/day01/ex2-sort.bash Executable file
View File

@@ -0,0 +1,28 @@
#!/bin/bash
#
# ex2-sort.bash: Advent2020 game, day 1/game 2, with sort.
CMD=$(basename "$0")
readarray -t numbers <<< "$(sort -n)"
declare -i i j k a b c m1 m2
for ((i=0; i<${#numbers[@]}; ++i)); do
a=$((numbers[i]))
for ((j=i+1; j<${#numbers[@]}; ++j)); do
b=$((numbers[j]))
((a+b > 2020)) && break
for ((k=j+1; k<${#numbers[@]}; ++k)); do
c=$((numbers[k]))
((a+b+c > 2020)) && break
if ((a+b+c == 2020)); then
printf "${CMD} : %d:%d %d:%d %d:%d sum=%d mul=%d\n" \
$i $a $j $b $k $c \
$((a+b+c)) \
$((a*b*c))
break 3
fi
done
done
done
exit 0

24
2020/day01/ex2.bash Executable file
View File

@@ -0,0 +1,24 @@
#!/bin/bash
#
# ex2.bash: Advent2020 game, day 1/game 2
CMD=$(basename "$0")
readarray -t numbers
declare -i i j k
for ((i=0; i<${#numbers[@]}; ++i)); do
for ((j=i+1; j<${#numbers[@]}; ++j)); do
for ((k=j+1; k<${#numbers[@]}; ++k)); do
if ((numbers[i] + numbers[j] + numbers[k] == 2020)); then
printf "${CMD} : %d:%d %d:%d %d:%d sum=%d mul=%d\n" \
$i $((numbers[i])) $j $((numbers[j])) $k $((numbers[k])) \
$((numbers[i]+numbers[j]+numbers[k])) \
$((numbers[i]*numbers[j]*numbers[k]))
break 3
fi
done
done
done
exit 0