-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday19.c3
60 lines (50 loc) · 1.39 KB
/
day19.c3
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
import std::io, std::time, std::collections, std::math;
fn void! main()
{
@pool() {
Clock c = clock::now();
io::printfn("- Part1: %d - %s", solve(&inc)!, c.mark());
io::printfn("- Part2: %d - %s", solve(&add)!, c.mark());
};
}
const String FILE = "input";
// const String FILE = "test";
long[100] combos;
def Calc = fn long(long, long);
fn long inc(long acc, long it) => ++acc;
fn long add(long acc, long it) => acc + it;
fn long! solve(Calc calc)
{
String input = (String) file::load_temp(FILE)!;
String[] part = input.trim().tsplit("\n\n");
String[] towels = part[0].trim().tsplit(", ");
String[] pattern = part[1].trim().tsplit("\n");
long sum;
foreach(p: pattern) {
combos = { [0..99] = -1 };
long combo = p.combo(towels, 0);
if (combo != 0) {
sum = calc(sum, combo);
}
}
return sum;
}
fn long String.combo(self, String[] towels, int idx)
{
if (combos[idx] != -1) return combos[idx];
long res;
String sub_pattern = (String) self[idx..];
foreach(towel: towels)
{
if (towel.len == sub_pattern.len && sub_pattern == towel)
{
res++;
}
if (towel.len < sub_pattern.len && (String) sub_pattern[..towel.len-1] == towel)
{
res += self.combo(towels, idx+towel.len);
}
}
combos[idx] = res;
return res;
}