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

1000
2020/day09/INPUT.txt Normal file

File diff suppressed because it is too large Load Diff

28
2020/day09/Makefile Normal file
View File

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

16
2020/day09/OUTPUT Normal file
View File

@@ -0,0 +1,16 @@
ex1.bash : res=167829540
time: 0:00.42 real, 0.42 user, 0.00 sys
context-switch: 57+1, page-faults: 0+172
ex1-c : res=167829540
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+76
ex2.bash : res=167829540 sum=28045630
time: 0:03.22 real, 3.01 user, 0.21 sys
context-switch: 359+921, page-faults: 0+49208
ex2-c : res=167829540 sum=28045630
time: 0:00.00 real, 0.00 user, 0.00 sys
context-switch: 0+1, page-faults: 0+75

93
2020/day09/README Normal file
View File

@@ -0,0 +1,93 @@
--- Day 9: Encoding Error ---
With your neighbor happily enjoying their video game, you turn your attention to an open data port on the little screen in the seat in front of you.
Though the port is non-standard, you manage to connect it to your computer through the clever use of several paperclips. Upon connection, the port outputs a series of numbers (your puzzle input).
The data appears to be encrypted with the eXchange-Masking Addition System (XMAS) which, conveniently for you, is an old cypher with an important weakness.
XMAS starts by transmitting a preamble of 25 numbers. After that, each number you receive should be the sum of any two of the 25 immediately previous numbers. The two numbers will have different values, and there might be more than one such pair.
For example, suppose your preamble consists of the numbers 1 through 25 in a random order. To be valid, the next number must be the sum of two of those numbers:
26 would be a valid next number, as it could be 1 plus 25 (or many other pairs, like 2 and 24).
49 would be a valid next number, as it is the sum of 24 and 25.
100 would not be valid; no two of the previous 25 numbers sum to 100.
50 would also not be valid; although 25 appears in the previous 25 numbers, the two numbers in the pair must be different.
Suppose the 26th number is 45, and the first number (no longer an option, as it is more than 25 numbers ago) was 20. Now, for the next number to be valid, there needs to be some pair of numbers among 1-19, 21-25, or 45 that add up to it:
26 would still be a valid next number, as 1 and 25 are still within the previous 25 numbers.
65 would not be valid, as no two of the available numbers sum to it.
64 and 66 would both be valid, as they are the result of 19+45 and 21+45 respectively.
Here is a larger example which only considers the previous 5 numbers (and has a preamble of length 5):
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576
In this example, after the 5-number preamble, almost every number is the sum of two of the previous 5 numbers; the only number that does not follow this rule is 127.
The first step of attacking the weakness in the XMAS data is to find the first number in the list (after the preamble) which is not the sum of two of the 25 numbers before it. What is the first number that does not have this property?
Your puzzle answer was 167829540.
--- Part Two ---
The final step in breaking the XMAS encryption relies on the invalid number you just found: you must find a contiguous set of at least two numbers in your list which sum to the invalid number from step 1.
Again consider the above example:
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576
In this list, adding up all of the numbers from 15 through 40 produces the invalid number from step 1, 127. (Of course, the contiguous set of numbers in your actual list might be much longer.)
To find the encryption weakness, add together the smallest and largest number in this contiguous range; in this example, these are 15 and 47, producing 62.
What is the encryption weakness in your XMAS-encrypted list of numbers?
Your puzzle answer was 28045630.
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.

20
2020/day09/TEST.txt Normal file
View File

@@ -0,0 +1,20 @@
35
20
15
25
47
40
62
55
65
95
102
117
150
182
127
219
299
277
309
576

103
2020/day09/ex1-c.c Normal file
View File

@@ -0,0 +1,103 @@
/* ex1-c: Advent2020 game, day 9/game 1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list {
unsigned size;
unsigned last;
unsigned long long *list;
};
#define DEFNSUM 25
#define BLOCKSIZE 1024 /* number of elements for realloc() */
void print_list(list)
struct list *list;
{
unsigned i, psize=list->last;
unsigned long long *ptr=list->list;
fprintf(stderr, "PROGRAM: address=%p pinstr=%p size=%d\n", list, ptr, psize);
for (i=0; i<psize; ++i)
printf("[%u] %llu\n", i, *(ptr+i));
}
static struct list *add_line(list, line)
struct list *list;
char *line;
{
int val;
unsigned cur, size;
unsigned long long *ptr;
if (!list) {
list=malloc(sizeof(struct list));
list->list=malloc(sizeof(unsigned long long)*BLOCKSIZE);
list->size=BLOCKSIZE;
list->last=0;
}
cur=list->last;
size=list->size;
if (cur == size) {
size+=BLOCKSIZE;
list->size=size;
list->list=realloc(list->list, sizeof(unsigned long long)*size);
fprintf(stderr, "realloc buf: cur=%d size=%d ptr=%p\n", cur, size, list->list);
}
ptr=list->list+cur;
sscanf(line, "%d", &val);
*ptr=val;
list->last++;
return list;
}
unsigned long long calc(list, cur, nsum)
struct list *list;
int cur;
unsigned nsum;
{
unsigned long long *ptr=list->list;
unsigned long long res=*(ptr+cur);
int start=cur-nsum, i, j;
for (i=start; i<cur; ++i) {
for (j=i+1; j<cur; ++j) {
if (ptr[i]+ptr[j] == res)
return 0;
}
}
return res;
}
int main(ac, av)
int ac;
char **av;
{
unsigned nsum=DEFNSUM;
char line[80];
struct list *list=NULL;
unsigned i;
unsigned long long res;
if (ac==2) {
nsum=atoi(*(av+1));
}
while (fgets(line, sizeof line, stdin)) {
list=add_line(list, line);
}
//print_list(list);
for (i=nsum; i<list->last; ++i) {
if (res=calc(list, i, nsum)) {
printf("%s : res=%llu\n", *av, res);
break;
}
}
exit (0);
}

51
2020/day09/ex1.bash Executable file
View File

@@ -0,0 +1,51 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 9/game 1.
CMD=${0##*/}
shopt -s extglob
declare -i N=5 SIZE
declare -a LIST=()
function push25 () {
local i
for ((i=0; i<25; ++i)); do
read -r i
LIST+=("$i")
done
}
function push() {
local i
while read -r i; do
LIST+=("$i")
done
SIZE=${#LIST[@]}
}
function badnum() {
local -i cur="$1" i j
local -i res=${LIST[$cur]}
local -i start=$((cur-N-1))
for ((i=start; i<cur; ++i)); do
for ((j=i+1; j<cur; ++j)); do
if (((LIST[i]+LIST[j]) == res)); then
return 0
fi
done
done
return 1
}
[[ $# = 1 ]] && N=$1
push
declare -i i target
for ((i=N; i<SIZE; ++i)); do
badnum $i || break
done
target="${LIST[$i]}"
printf "%s : res=%d\n" "$CMD" "$target"
exit 0

134
2020/day09/ex2-c.c Normal file
View File

@@ -0,0 +1,134 @@
/* ex2-c: Advent2020 game, day 9/game 2
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list {
unsigned size;
unsigned last;
unsigned long long *list;
};
#define DEFNSUM 25
#define BLOCKSIZE 1024 /* number of elements for realloc() */
void print_list(list)
struct list *list;
{
unsigned i, psize=list->last;
unsigned long long *ptr=list->list;
fprintf(stderr, "PROGRAM: address=%p pinstr=%p size=%d\n", list, ptr, psize);
for (i=0; i<psize; ++i)
printf("[%u] %llu\n", i, *(ptr+i));
}
static struct list *add_line(list, line)
struct list *list;
char *line;
{
int val;
unsigned cur, size;
unsigned long long *ptr;
if (!list) {
list=malloc(sizeof(struct list));
list->list=malloc(sizeof(unsigned long long)*BLOCKSIZE);
list->size=BLOCKSIZE;
list->last=0;
}
cur=list->last;
size=list->size;
if (cur == size) {
size+=BLOCKSIZE;
list->size=size;
list->list=realloc(list->list, sizeof(unsigned long long)*size);
fprintf(stderr, "realloc buf: cur=%d size=%d ptr=%p\n", cur, size, list->list);
}
ptr=list->list+cur;
sscanf(line, "%d", &val);
*ptr=val;
list->last++;
return list;
}
unsigned long long badnum(list, cur, nsum)
struct list *list;
int cur;
unsigned nsum;
{
unsigned long long *ptr=list->list;
unsigned long long res=*(ptr+cur);
int start=cur-nsum, i, j;
for (i=start; i<cur; ++i) {
for (j=i+1; j<cur; ++j) {
if (ptr[i]+ptr[j] == res)
return 0;
}
}
return res;
}
unsigned long long findsum(list, target, start)
struct list *list;
unsigned long long target;
int start;
{
unsigned long long sum=0;
unsigned int i;
unsigned size=list->last;
unsigned long long *ptr=list->list, cur;
unsigned long long min=*(ptr+start), max=min;
for (i=start; i<size && sum<target; ++i) {
cur=*(ptr+i);
sum+=cur;
if (cur < min)
min=cur;
if (cur > max)
max=cur;
if (sum == target) {
return min+max;
}
}
return 0;
}
int main(ac, av)
int ac;
char **av;
{
unsigned nsum=DEFNSUM;
char line[80];
struct list *list=NULL;
unsigned i;
unsigned long long res1, res2;
if (ac==2) {
nsum=atoi(*(av+1));
}
while (fgets(line, sizeof line, stdin)) {
list=add_line(list, line);
}
//print_list(list);
for (i=nsum; i<list->last; ++i) {
if (res1=badnum(list, i, nsum)) {
//printf("%s : res=%llu\n", *av, res1);
break;
}
}
for (i=0; i<list->last; ++i) {
if (res2=findsum(list, res1, i)) {
printf("%s : res=%llu sum=%llu\n", *av, res1, res2);
break;
}
}
exit (0);
}

75
2020/day09/ex2.bash Executable file
View File

@@ -0,0 +1,75 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 9/game 2.
CMD=${0##*/}
shopt -s extglob
declare -i N=5 SIZE
declare -a LIST=()
function push25 () {
local i
for ((i=0; i<25; ++i)); do
read -r i
LIST+=("$i")
done
}
function push() {
local i
while read -r i; do
LIST+=("$i")
done
SIZE=${#LIST[@]}
}
function badnum() {
local -i cur="$1" i j
local -i res=${LIST[$cur]}
local -i start=$((cur-N-1))
for ((i=start; i<cur; ++i)); do
for ((j=i+1; j<cur; ++j)); do
if (((LIST[i]+LIST[j]) == res)); then
return 0
fi
done
done
return 1
}
function findsum() {
local -i target=$1 start=$2 i
local -i sum=0
local -i small=${LIST[$start]}
local -i high=$small
for ((i=start; i<SIZE && sum<target; ++i)); do
((cur=${LIST[$i]}))
((sum+=cur))
((cur<small)) && small=$cur
((cur>high)) && high=$cur
if ((sum==target)); then
echo $((small+high))
return 0
fi
done
return 1
}
[[ $# = 1 ]] && N=$1
push
declare -i i target num
for ((i=N; i<SIZE; ++i)); do
badnum $i || break
done
target="${LIST[$i]}"
for ((i=0; i<SIZE; ++i)); do
num=$(findsum "$target" $i) && break
done
printf "%s : res=%d sum=%d\n" "$CMD" "$target" "$num"
exit 0