-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.um
78 lines (65 loc) · 1.48 KB
/
day7.um
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
import "std.um"
type P = struct {
n: uint
a: []uint
b: []uint // bases
}
fn solve(p: ^P, num: uint, i: uint, part: int): bool {
if num == p.n && i == len(p.a) {
return true
}
if i >= len(p.a) {
return false
}
if num > p.n {
return false
}
if part == 2 {
return solve(p, num+p.a[i], i+1, part) ||
solve(p, num*p.a[i], i+1, part) ||
solve(p, num*p.b[i]+p.a[i], i+1, part)
}
return solve(p, num+p.a[i], i+1, part) ||
solve(p, num*p.a[i], i+1, part)
}
fn main() {
nums := []str{}
for p := ""; scanf("%s", &p) == 1 {
nums = append(nums, p)
}
arr := []uint{}
cur := 0
inp := []P{}
for _, num in nums {
if num[len(num)-1] == ":" {
if len(arr) > 0 {
inp = append(inp, P{n: cur, a: arr})
}
arr = {}
cur = std::atoi(num)
} else {
arr = append(arr, std::atoi(num))
}
}
if len(arr) > 0 {
inp = append(inp, P{n: cur, a: arr})
}
res1, res2 := 0, 0
for _, p in inp {
for _, n in p.a {
base := 1
for n > 0 {
base *= 10
n /= 10
}
p.b = append(p.b, base)
}
if solve(&p, p.a[0], 1, 1) {
res1 += p.n
}
if solve(&p, p.a[0], 1, 2) {
res2 += p.n
}
}
printf("%v %v\n", res1, res2)
}