-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.ex
51 lines (37 loc) · 906 Bytes
/
part2.ex
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
include std/io.e
constant MESSAGE_SIZE = 14
integer offset = 0, start = 0
sequence buffer = {}
integer ch
while not find( ch, {EOF,'\r','\n'} ) with entry do
-- increment counter
offset += 1
-- convert ch to integer
ch = ch - 'a' + 1
-- append character to the buffer
buffer = insert( buffer, ch, 1 )
-- check buffer length
if length( buffer ) < MESSAGE_SIZE then
-- not enough characters
ch = getc( STDIN )
continue
end if
-- input characters are all 'a'-'z'
-- so ch value will always be 1-26
sequence counts = repeat( 0, 26 )
-- count each character
for i = 1 to MESSAGE_SIZE do
ch = buffer[i]
counts[ch] += 1
end for
-- here, "counts > 1" will be a sequence of
-- boolean results (0 or 1), so if no count
-- is greater than 1 then we have our start
if find( 1, counts > 1 ) = 0 then
start = offset
exit
end if
entry
ch = getc( STDIN )
end while
? start