Add D1P1 in awk

This commit is contained in:
kageru 2019-12-04 14:48:10 +01:00
parent 53dad7ee15
commit acf8bb2d44
3 changed files with 30 additions and 0 deletions

26
2019/04/day4.awk Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/awk -f
function isAscending(number) {
digit1 = int(substr(number, 1, 1));
digit2 = int(substr(number, 2, 1));
digit3 = int(substr(number, 3, 1));
digit4 = int(substr(number, 4, 1));
digit5 = int(substr(number, 5, 1));
digit6 = int(substr(number, 6, 1));
return (digit1 <= digit2 && digit2 <= digit3 && digit3 <= digit4 && digit4 <= digit5 && digit5 <= digit6);
}
{
split($1, inputs, "-");
lower = inputs[1];
upper = inputs[2];
for (i=lower; i<upper; i++) {
iString = i + "";
# this is what I would have done, but awk regex doesn’t support back references
#if (match(iString, "(.)\1") != 0 && isAscending(iString)) {
if (isAscending(iString))
print iString;
#valid++;
}
}

1
2019/04/input Normal file
View File

@ -0,0 +1 @@
172930-683082

3
2019/04/run_day4.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
awk -f day4.awk input | rg --pcre2 '(.)\1' | wc -l | awk '{printf("Part 1: %s\n", $1);}'