-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3a.pas
62 lines (59 loc) · 1.32 KB
/
d3a.pas
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
PROGRAM d3a;
USES sysutils;
FUNCTION score(c:char) : integer;
CONST
lowA = ord('a');
capA = ord('A');
BEGIN
{lowercase}
IF ord(c) >= lowA THEN score := ord(c) - lowA + 1
{uppercase}
ELSE score := ord(c) - capA + 27;
END;
FUNCTION answer(filename:string) : integer;
VAR
f: text;
l: string = '';
len,
half: integer;
compartments: array[1..2] of string;
c: char;
i: integer;
totscore: integer = 0;
BEGIN
IF NOT FileExists(filename) THEN writeln('file not found: ', filename);
assign(f, filename);
reset(f);
WHILE not eof(f) DO
BEGIN
readln(f, l);
len := length(l);
half := round(len/2);
compartments[1] := copy(l, 1, half);
compartments[2] := copy(l, half+1, len);
FOR i := 1 TO 50 DO
BEGIN
c := compartments[1][i];
IF pos(c, compartments[2]) > 0 THEN
BEGIN
totscore += score(c);
break;
END;
END;
END;
close(f);
answer := totscore;
END;
CONST
testfile = 'd3.test.1';
filename = 'd3.input';
VAR
a : integer;
BEGIN{d3a}
assert(1=score('a'));
assert(27=score('A'));
assert(answer(testfile) = 157, 'test faal');
a := answer(filename);
assert(a > 7862);
writeln('answer: ', a);
END.