summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2023-12-01 11:03:36 -0500
committerJune McEnroe <june@causal.agency>2023-12-01 11:03:36 -0500
commitbc46bac92724c8b6055eb5b7274d36a5bca65a2a (patch)
tree679f24faf0393024f21872f4209791d076dff85f
parentSolve day 1 part 1 (diff)
downloadaoc-bc46bac92724c8b6055eb5b7274d36a5bca65a2a.tar.gz
aoc-bc46bac92724c8b6055eb5b7274d36a5bca65a2a.zip
Solve day 1 part 2
-rw-r--r--2023/day01.awk34
1 files changed, 33 insertions, 1 deletions
diff --git a/2023/day01.awk b/2023/day01.awk
index 3127556..519b414 100644
--- a/2023/day01.awk
+++ b/2023/day01.awk
@@ -1,7 +1,39 @@
 {
-	gsub("[^[:digit:]]", "");
+	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;
 }