-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03p2.odin
80 lines (73 loc) · 1.75 KB
/
03p2.odin
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
package main
import "core:fmt"
import "core:strings"
memory_instruction :: struct {
condition: bool,
is_do: bool,
mul: [2]int,
}
D03P2 :: proc() {
input_string := #load("inputs/03.txt", string)
lines := strings.split(input_string, "\n", context.temp_allocator)
sum_of_multiplications := get_sum_of_cond_multiplications(lines)
fmt.printf("Sum of all multiplications: %d\n", sum_of_multiplications)
}
get_sum_of_cond_multiplications :: proc(lines: []string) -> int {
instructions := parse_memory_instructions(lines)
defer delete(instructions)
doing := true
sum := 0
for instruction in instructions {
if instruction.condition {
if instruction.is_do {
doing = true
} else {
doing = false
}
continue
}
if doing {
sum += instruction.mul[0] * instruction.mul[1]
}
}
return sum
}
parse_memory_instructions :: proc(lines: []string) -> (instructions: [dynamic]memory_instruction) {
for line in lines {
i := 0
for i < len(line) {
switch line[i] {
case 'd':
// Check for "do()" or "don't()"
if i + 3 < len(line) && line[i:i + 3] == "do(" {
append(&instructions, memory_instruction{condition = true, is_do = true})
i += 4
} else if i + 6 < len(line) && line[i:i + 6] == "don't(" {
append(&instructions, memory_instruction{condition = true, is_do = false})
i += 7
} else {
i += 1
}
case 'm':
// Check for "mul("
if i + 3 < len(line) && line[i:i + 4] == "mul(" {
i += 4
num1, num2: int
ok: bool
i, num1, num2, ok = parse_multiplication(line, i)
if ok {
append(
&instructions,
memory_instruction{condition = false, mul = [2]int{num1, num2}},
)
}
} else {
i += 1
}
case:
i += 1
}
}
}
return
}