-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13p1.odin
87 lines (72 loc) · 1.91 KB
/
13p1.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
81
82
83
84
85
86
87
package main
import "core:fmt"
import "core:strconv"
import "core:strings"
D13P1 :: proc() {
input_string := #load("./inputs/13.txt", string)
lines := strings.split(input_string, "\n", context.temp_allocator)
visible_dots := fold_transparent_paper(lines, false)
fmt.printfln("Total visible dots: %v", visible_dots)
}
fold_transparent_paper :: proc(lines: []string, all: bool) -> int {
dots, folds := parse_fold_instructions(lines)
defer delete(dots)
defer delete(folds)
if !all {
apply_fold(&dots, folds[0])
return len(dots)
}
for fold in folds {
apply_fold(&dots, fold)
}
draw_dots(dots)
return len(dots)
}
parse_fold_instructions :: proc(lines: []string) -> (dots: [dynamic]Vec2, folds: [dynamic]string) {
instruction_start: int
for line in lines {
if line == "" {
break
}
dot := Vec2{0, 0}
line_parts := strings.split(line, ",", context.temp_allocator)
dot.x = strconv.atoi(line_parts[0])
dot.y = strconv.atoi(line_parts[1])
append(&dots, dot)
instruction_start += 1
}
for i := instruction_start + 1; i < len(lines); i += 1 {
line := lines[i]
fold_prefix := "fold along "
if len(line) > len(fold_prefix) {
fold := line[len(fold_prefix):]
append(&folds, fold)
}
}
return
}
apply_fold :: proc(dots: ^[dynamic]Vec2, fold: string) {
if strings.contains(fold, "x=") {
fold_x := strconv.atoi(strings.split(fold, "=", context.temp_allocator)[1])
for i := 0; i < len(dots); i += 1 {
if dots[i].x > fold_x {
dots[i].x = fold_x - (dots[i].x - fold_x)
}
}
} else if strings.contains(fold, "y=") {
fold_y := strconv.atoi(strings.split(fold, "=", context.temp_allocator)[1])
for i := 0; i < len(dots); i += 1 {
if dots[i].y > fold_y {
dots[i].y = fold_y - (dots[i].y - fold_y)
}
}
}
// remove duplicates
for i := 0; i < len(dots); i += 1 {
for j := i + 1; j < len(dots); j += 1 {
if dots[i] == dots[j] {
unordered_remove(dots, j)
}
}
}
}