-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.ha
80 lines (65 loc) · 1.62 KB
/
part1.ha
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
use fmt;
use io;
use os;
use strings;
use strconv;
type equation = struct {
test: i64,
nums: []i64,
};
fn read_input() []equation = {
const file = os::open("input.txt")!;
defer io::close(file)!;
const lines = strings::split(strings::fromutf8(io::drain(file)!)!, "\n");
let equations: []equation = [];
for (const line .. lines) {
if (len(line) == 0)
continue;
const parts = strings::split(line, " ");
let equation = equation{
test = strconv::stoi64(strings::sub(parts[0], 0, len(parts[0]) - 1))!,
nums = []
};
for (let i = 1z; i < len(parts); i += 1)
append(equation.nums, strconv::stoi64(parts[i])!);
append(equations, equation);
};
return equations;
};
type op = enum {
ADD,
MUL,
};
const ops = [op::ADD, op::MUL];
// Possible optimization: If the test is greater than the product of all numbers, you can exit early
fn permute_ops(equation: equation, into: []op, i: size) bool = {
if (i == len(into)) {
let sum = equation.nums[0];
for (let i = 0z; i < len(into); i += 1) {
switch (into[i]) {
case op::ADD => sum += equation.nums[i + 1];
case op::MUL => sum *= equation.nums[i + 1];
};
};
return sum == equation.test;
};
for (let j = 0z; j < len(ops); j += 1) {
into[i] = ops[j];
if (permute_ops(equation, into, i + 1))
return true;
};
return false;
};
export fn main() void = {
const equations = read_input();
let sum = 0i64;
for (const equation .. equations) {
let ops: []op = [];
defer free(ops);
for (let i = 1z; i < len(equation.nums); i += 1)
append(ops, op::ADD);
if (permute_ops(equation, ops, 0))
sum += equation.test;
};
fmt::printfln("{}", sum)!;
};