day 1 solution

main
magical 2023-12-02 06:30:10 +00:00
parent 829c87d96d
commit 1646399f9f
5 changed files with 1031 additions and 0 deletions

1000
day01/input 100644

File diff suppressed because it is too large Load Diff

4
day01/sample1.in 100644
View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

7
day01/sample2.in 100644
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

2
day01/sol.awk 100644
View File

@ -0,0 +1,2 @@
{ gsub("[^0-9]", " "); gsub("[0-9]", "& "); n = $1 $NF; T += n; print n }
END { print T }

18
day01/sol2.awk 100644
View File

@ -0,0 +1,18 @@
{
d1 = "";
d2 = "";
if (match($0, "one|two|three|four|five|six|seven|eight|nine|[0-9]")) { d1 = substr($0,RSTART,RLENGTH) }
for (i = length($0); i > 0; i--) {
s = substr($0, i);
if (match(s, "one|two|three|four|five|six|seven|eight|nine|[0-9]")) { d2 = substr(s,RSTART,RLENGTH); break; }
}
n = d1 d2;
print n;
gsub("one","1",n); gsub("two","2",n); gsub("three","3",n);
gsub("four","4",n); gsub("five","5",n); gsub("six","6",n);
gsub("seven","7",n); gsub("eight","8",n); gsub("nine","9",n);
T += n;
print $0, n;
}
#{ print $0 }
END { print T }