-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.c3
86 lines (72 loc) · 1.85 KB
/
day11.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import std::io;
import std::time;
import std::collections;
import std::math;
fn void main()
{
@pool() {
Clock c = clock::now();
stones.temp_init();
mem.temp_init();
io::printfn("- Part1: %d - %s", solve(25)!!, c.mark());
io::printfn("- Part2: %d - %s", solve(75)!!, c.mark());
};
}
const String FILE = "input.txt";
def Stones = HashMap(<usz, usz>);
def Memoization = HashMap(<usz, List(<usz>)>);
Stones stones;
Memoization mem;
fn bool is_even(usz num) => num > 0 && (string::tformat("%s", num).len % 2) == 0;
fn bool is_zero(usz num) => num == 0;
fn void! parse(String input)
{
foreach(stone: input.tsplit(" ")) {
usz num = stone.to_long()!!;
stones[num] = stones.@get_or_set(num, 1);
}
}
fn List(<usz>)! rule(usz stone)
{
if (mem.has_key(stone)) {
return mem[stone]!;
}
List(<usz>) change;
switch {
case is_zero(stone):
change.push(1);
case is_even(stone):
String num_str = string::tformat("%s", stone);
change.push(num_str[..num_str.len/2 -1].to_long()!);
change.push(num_str[(num_str.len/2)..].to_long()!);
default:
change.push(stone * 2024);
}
mem[stone] = change;
return change;
}
fn void! blink()
{
Stones changed;
changed.temp_init();
stones.@each(; usz stone, usz count) {
List(<usz>) changes = rule(stone)!;
foreach(change: changes) {
changed[change] = changed.@get_or_set(change, 0) + count;
}
};
@swap(stones, changed);
}
fn long! solve(int blinks)
{
String input = (String) file::load_temp(FILE)!;
stones.clear();
mem.clear();
usz sum = 0;
parse(input.trim())!;
for (int b = 0; b <blinks; b++) blink()!;
stones.@each(; usz stone, usz count) {
sum += count;
};
return sum;
}