-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday06.chpl
More file actions
34 lines (29 loc) · 888 Bytes
/
day06.chpl
File metadata and controls
34 lines (29 loc) · 888 Bytes
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
/*
day06
usage:
chpl day06.chpl
./day06< input06a.txt
looked up
https://chapel-lang.org/docs/language/spec/data-parallelism.html?highlight=loop%20expression#forall-expressions
*/
use IO, Set;
config var subsetSize = 4;
var line : string;
readLine(line);
// uniqueSubsetIdx[i] will be set to i if the subsetSize characters
// before i in the input are unique and line.size othersize.
// This way we can do a min reduction on uniqueSubsetIdx to get the answer.
var searchDomain = {subsetSize-1..<line.size};
var uniqueSubsetIdx : [searchDomain] int;
forall i in searchDomain {
var charSet : set(string);
for j in 0..<subsetSize {
charSet.add(line[i-j]);
}
if charSet.size==subsetSize {
uniqueSubsetIdx[i] = i+1; // they want 1 indexing on characters
} else {
uniqueSubsetIdx[i] = searchDomain.size;
}
}
writeln( min reduce uniqueSubsetIdx );