-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.awk
54 lines (45 loc) · 781 Bytes
/
15.awk
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
BEGIN {
for (i = 1; i < 256; i++)
ascii[sprintf("%c", i)] = i
FS = ","
}
{
for (i = 1; i <= NF; i++) {
sum1 += hash($i)
lens = $i
sub(/[=-].*/, "", lens)
box = hash(lens)
if ($i ~ /-/) {
if (lens in id) {
sub(" " id[lens] " ", "", ids[box])
delete id[lens]
}
}
else {
if (!(lens in id)) {
id[lens] = ++seen[box]
ids[box] = ids[box] " " id[lens] " "
}
n = $i
sub(/.*=/, "", n)
value[box, id[lens]] = n
}
}
for (box in ids) {
split(ids[box], slots, " ")
for (i in slots)
sum2 += (box+1)*i*value[box, slots[i]]
}
print sum1
print sum2
}
function hash(s, n, i, ret) {
gsub(/./, "& ", s)
n = split(s, tmp, " ")
for (i = 1; i <= n; i++) {
ret += ascii[tmp[i]]
ret *= 17
ret %= 256
}
return ret
}