- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 157 lines of Turing from the credited upstream file ccc99s2.t.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
12345678 910 1112 1314 15var infile : string := "y2k.in"16var outfile : string := "y2k.out"17var fi, fo : int18var n : int19var line : string20 2122function dn (s : string (1)) : boolean23 if s >= "a" and s <= "z" or24 s >= "A" and s <= "Z" or25 s >= "0" and s <= "9" then26 result true27 else28 result false29 end if30end dn31 3233procedure dmy (i : int, var s : string)34 if i > 1 and dn (s (i - 1)) then35 36 elsif i + 8 <= length (s) and dn (s (i + 8)) then37 38 elsif i + 7 > length (s) then39 40 elsif strintok (s (i .. i + 1)) and strint (s (i .. i + 1)) >= 1 and41 strint (s (i .. i + 1)) <= 31 and42 s (i + 2) = "/" and43 strintok (s (i + 3 .. i + 4)) and strint (s (i + 3 .. i + 4)) >=44 1 and strint (s (i + 3 .. i + 4)) <= 12 and45 s (i + 5) = "/" and46 strintok (s (i + 6 .. i + 7)) and strint (s (i + 6 .. i + 7)) >=47 0 and strint (s (i + 6 .. i + 7)) <= 99 then48 if strint (s (i + 6 .. i + 7)) < 25 then49 s := s (1 .. i + 5) + "20" + s (i + 6 .. *)50 else51 s := s (1 .. i + 5) + "19" + s (i + 6 .. *)52 end if53 end if54end dmy55 5657procedure ymd (i : int, var s : string)58 if i > 1 and dn (s (i - 1)) then59 60 elsif i + 8 <= length (s) and dn (s (i + 8)) then61 62 elsif i + 7 > length (s) then63 64 elsif strintok (s (i .. i + 1)) and strint (s (i .. i + 1)) >= 0 and65 strint (s (i .. i + 1)) <= 99 and66 s (i + 2) = "." and67 strintok (s (i + 3 .. i + 4)) and strint (s (i + 3 .. i + 4)) >=68 1 and strint (s (i + 3 .. i + 4)) <= 12 and69 s (i + 5) = "." and70 strintok (s (i + 6 .. i + 7)) and strint (s (i + 6 .. i + 7)) >=71 1 and strint (s (i + 6 .. i + 7)) <= 31 then72 if strint (s (i .. i + 1)) < 25 then73 s := s (1 .. i - 1) + "20" + s (i .. *)74 else75 s := s (1 .. i - 1) + "19" + s (i .. *)76 end if77 end if78end ymd79 8081procedure mdy (i : int, var s : string)82 if i > 1 and dn (s (i - 1)) then83 84 else85 var x : int86 if i + 6 <= length (s) and s (i .. i + 6) = "January" then87 x := i + 788 elsif i + 7 <= length (s) and s (i .. i + 7) = "February" then89 x := i + 890 elsif i + 4 <= length (s) and s (i .. i + 4) = "March" then91 x := i + 592 elsif i + 4 <= length (s) and s (i .. i + 4) = "April" then93 x := i + 594 elsif i + 2 <= length (s) and s (i .. i + 2) = "May" then95 x := i + 396 elsif i + 3 <= length (s) and s (i .. i + 3) = "June" then97 x := i + 498 elsif i + 3 <= length (s) and s (i .. i + 3) = "July" then99 x := i + 4100 elsif i + 5 <= length (s) and s (i .. i + 5) = "August" then101 x := i + 6102 elsif i + 8 <= length (s) and s (i .. i + 8) = "September" then103 x := i + 9104 elsif i + 6 <= length (s) and s (i .. i + 6) = "October" then105 x := i + 7106 elsif i + 7 <= length (s) and s (i .. i + 7) = "November" then107 x := i + 8108 elsif i + 7 <= length (s) and s (i .. i + 7) = "December" then109 x := i + 8110 else111 x := 99999112 end if113 if x + 7 <= length (s) and dn (s (x + 7)) then114 115 elsif x + 6 > length (s) then116 117 elsif s (x) = " " and118 strintok (s (x + 1 .. x + 2)) and strint (s (x + 1 .. x +119 2)) >= 1 and120 strint (s (x + 1 .. x + 2)) <= 31 and121 s (x + 3 .. x + 4) = ", " and122 strintok (s (x + 5 .. x + 6)) and strint (s (x + 5 .. x +123 6)) >= 0 and strint (s (x + 5 .. x + 6)) <= 99 then124 if strint (s (x + 5 .. x + 6)) < 25 then125 s := s (1 .. x + 4) + "20" + s (x + 5 .. *)126 else127 s := s (1 .. x + 4) + "19" + s (x + 5 .. *)128 end if129 end if130 end if131end mdy132 133 134open : fi, infile, get135open : fo, outfile, put136get : fi, n137for i : 1 .. n138 get : fi, line : *139 var j : int := 1140 loop141 exit when j >= length (line)142 dmy (j, line)143 ymd (j, line)144 mdy (j, line)145 j := j + 1146 end loop147 put : fo, line148end for149 150close : fi151close : fo152 153 154 155 156 157