blob: 351c34a6b7b2181487694a528445d9f809ba60b8 (
plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
|
#!/usr/bin/awk
#
# See ../README.md
#
#
# Run as: awk -f ch-2.awk < input-file
#
function next_num (prev_num, tail) {
#
# Find the trailing 3s
#
match (prev_num, /3*$/)
tail = substr (prev_num, RSTART)
#
# Replace them with 3s
#
gsub (/3/, 1, tail)
#
# Put the tail back in, incrementing the number before it.
# If we matched the full number, add a 1
#
if (RLENGTH == length (prev_num)) {
prev_num = 1 tail
}
else {
prev_num = substr (prev_num, 1, RSTART - 2) \
(substr (prev_num, RSTART - 1, 1) + 1) \
tail
}
#
# Replace the trailing 1s with 1212...
#
gsub (/11/, "12", prev_num)
return prev_num
}
{
n = 0
for (i = 0; i < $1; i ++) {
n = next_num(n)
}
print (n)
}
|