-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.ex
66 lines (47 loc) · 1.23 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
include std/io.e
include std/text.e
constant GROUP_SIZE = 3
constant PRIORITY = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
integer total_priority = 0
object line
while sequence( line ) with entry do
line = text:trim( line )
-- put this line into a group
line = {line}
-- read more lines into the group
while length( line ) < GROUP_SIZE do
line = append( line, gets(STDIN) )
line[$] = text:trim( line[$] )
end while
integer shared = 0
-- lookup each item in each sack
for i = 1 to GROUP_SIZE do
-- look at the items in the first sack
for j = 1 to length( line[1] ) do
integer count = 1
-- compare it against the other sacks
for k = 2 to length( line ) do
-- increase count if item is found
count += find( line[1][j], line[k] ) != 0
end for
-- is this item in each sack?
if count = GROUP_SIZE then
-- save the item and stop looking
shared = line[1][j]
exit
end if
end for
-- did we find the item?
if shared then
-- stop looking
exit
end if
-- rotate the items to try again
line = line[2..$] & line[1..1]
end for
-- add the priority of the value to total
total_priority += find( shared, PRIORITY )
entry
line = gets( STDIN )
end while
? total_priority