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/day02/INPUT.txt Normal file

File diff suppressed because it is too large Load Diff

26
2020/day02/Makefile Normal file
View File

@@ -0,0 +1,26 @@
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: ex1-c
@$(TIME) ex1.bash < $(INPUT)
@$(TIME) ex1-c < $(INPUT)
ex2: ex2-c
@$(TIME) ex2.bash < $(INPUT)
@#$(TIME) ex2-sort.bash < $(INPUT)
@$(TIME) ex2-c < $(INPUT)
clean:
@rm -f ex1-c ex2-c core
deploy:
@$(MAKE) -C .. deploy

12
2020/day02/OUTPUT Normal file
View File

@@ -0,0 +1,12 @@
ex1.bash : lines: 1000 matched:607
time: 0:00.03 real, 0.03 user, 0.00 sys
ex1-c : lines: 1000 matched:607
time: 0:00.00 real, 0.00 user, 0.00 sys
ex2.bash : lines: 1000 matched:321
time: 0:00.03 real, 0.03 user, 0.00 sys
ex2-c : lines: 1000 matched:321
time: 0:00.00 real, 0.00 user, 0.00 sys

48
2020/day02/README Normal file
View File

@@ -0,0 +1,48 @@
--- Day 2: Password Philosophy ---
Your flight departs in a few days from the coastal airport; the easiest way down to the coast from here is via toboggan.
The shopkeeper at the North Pole Toboggan Rental Shop is having a bad day. "Something's wrong with our computers; we can't log in!" You ask if you can take a look.
Their password database seems to be a little corrupted: some of the passwords wouldn't have been allowed by the Official Toboggan Corporate Policy that was in effect when they were chosen.
To try to debug the problem, they have created a list (your puzzle input) of passwords (according to the corrupted database) and the corporate policy when that password was set.
For example, suppose you have the following list:
1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc
Each line gives the password policy and then the password. The password policy indicates the lowest and highest number of times a given letter must appear for the password to be valid. For example, 1-3 a means that the password must contain a at least 1 time and at most 3 times.
In the above example, 2 passwords are valid. The middle password, cdefg, is not; it contains no instances of b, but needs at least 1. The first and third passwords are valid: they contain one a or nine c, both within the limits of their respective policies.
How many passwords are valid according to their policies?
Your puzzle answer was 607.
--- Part Two ---
While it appears you validated the passwords correctly, they don't seem to be what the Official Toboggan Corporate Authentication System is expecting.
The shopkeeper suddenly realizes that he just accidentally explained the password policy rules from his old job at the sled rental place down the street! The Official Toboggan Corporate Policy actually works a little differently.
Each policy actually describes two positions in the password, where 1 means the first character, 2 means the second character, and so on. (Be careful; Toboggan Corporate Policies have no concept of "index zero"!) Exactly one of these positions must contain the given letter. Other occurrences of the letter are irrelevant for the purposes of policy enforcement.
Given the same example list from above:
1-3 a: abcde is valid: position 1 contains a and position 3 does not.
1-3 b: cdefg is invalid: neither position 1 nor position 3 contains b.
2-9 c: ccccccccc is invalid: both position 2 and position 9 contain c.
How many passwords are valid according to the new interpretation of the policies?
Your puzzle answer was 321.
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.

3
2020/day02/TEST.txt Normal file
View File

@@ -0,0 +1,3 @@
1-3 a: abcde
1-3 b: cdefg
2-9 c: ccccccccc

31
2020/day02/ex1-c.c Normal file
View File

@@ -0,0 +1,31 @@
/* ex1-c: Advent2020 game, day 2/game 1
*/
#include <stdio.h>
#include <stdlib.h>
int ccount(char *str, char c)
{
int count=0;
for (int i=0; str[i]; i++)
count += (str[i] == c);
return count;
}
int main(ac, av)
char **av;
{
int beg, end, nc, found=0, nlines=0;
char c, str[80];
while (scanf("%d-%d %c: %s", &beg, &end, &c, str) != EOF) {
nc=ccount(str, c);
if (nc >= beg && nc <= end)
found++;
nlines++;
}
printf ("%s : lines: %d matched:%d\n", *av, nlines, found);
exit (0);
}

18
2020/day02/ex1.bash Executable file
View File

@@ -0,0 +1,18 @@
#!/bin/bash
#
# ex1.bash: Advent2020 game, day 2/game 1
CMD=${0##*/}
declare -i beg end num found=0 nlines=0
IFS=$':- \t'
while read -r beg end char str; do
stripped=${str//[^$char]} # keep only specified char
((num = ${#stripped})) # occurences of this char
((num >= beg && num <= end && found++)) # match ?
((nlines++))
done
printf "${CMD} : lines: %d matched:%d\n" "$nlines" "$found"
exit 0

24
2020/day02/ex2-c.c Normal file
View File

@@ -0,0 +1,24 @@
/* ex2-c: Advent2020 game, day 2/game 2
*/
#include <stdio.h>
#include <stdlib.h>
int ccount(char *str, char c, int pos1, int pos2)
{
return ((str[pos1-1] == c) + (str[pos2-1] == c)) == 1;
}
int main(ac, av)
char **av;
{
int beg, end, nc, found=0, nlines=0;
char c, str[80];
while (scanf("%d-%d %c: %s", &beg, &end, &c, str) != EOF) {
found += ccount(str, c, beg, end);
nlines++;
}
printf ("%s : lines: %d matched:%d\n", *av, nlines, found);
exit (0);
}

20
2020/day02/ex2.bash Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/bash
#
# ex2.bash: Advent2020 game, day 2/game 2.
CMD=${0##*/}
declare -i beg end num found=0 nlines=0
IFS=$':- \t'
while read -r beg end char str; do
num=0
[[ ${str:beg-1:1} == "$char" ]] && ((num++))
[[ ${str:end-1:1} == "$char" ]] && ((num++))
#((num == 1 && found++))
((found += num == 1))
((nlines++))
done
printf "${CMD} : lines: %d matched:%d\n" "$nlines" "$found"
exit 0