summary refs log tree commit diff homepage
path: root/2023/day01.awk
blob: 519b414753761eaf51d094677a66066caca6a134 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{
	first = 0;
	for (i = 0; i <= length; i++) {
		digit = 0;
		s = substr($0, i);
		if (s ~ /^one/) {
			digit = 1;
		} else if (s ~ /^two/) {
			digit = 2;
		} else if (s ~ /^three/) {
			digit = 3;
		} else if (s ~ /^four/) {
			digit = 4;
		} else if (s ~ /^five/) {
			digit = 5;
		} else if (s ~ /^six/) {
			digit = 6;
		} else if (s ~ /^seven/) {
			digit = 7;
		} else if (s ~ /^eight/) {
			digit = 8;
		} else if (s ~ /^nine/) {
			digit = 9;
		} else if (s ~ /^[[:digit:]]/) {
			digit = substr(s, 1, 1);
		}
		if (digit) {
			if (!first) first = digit;
			last = digit;
		}
	}
	gsub(/[^[:digit:]]/, "");
	part1 += substr($0, 1, 1) substr($0, length, 1);
	part2 += first last;
}
END {
	print part1;
	print part2;
}