-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.odin
53 lines (43 loc) · 826 Bytes
/
day01.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
package day01
import "core:fmt"
import "core:os"
import "core:slice"
import "core:strconv"
import "core:strings"
main :: proc() {
content, ok := os.read_entire_file("input/day01.txt")
if !ok {
// could not read file
return
}
defer delete(content)
lines := strings.split_lines(string(content))
l1 := make([]int, len(lines))
l2 := make([]int, len(lines))
for line, i in lines {
nums := strings.split(line, " ")
l1[i] = strconv.atoi(nums[0])
l2[i] = strconv.atoi(nums[1])
}
slice.sort(l1)
slice.sort(l2)
diff := 0
for i in 0 ..< len(l1) {
diff += abs(l1[i] - l2[i])
}
// Part 1
fmt.println(diff)
sim := 0
for i in 0 ..< len(l1) {
times := 0
num := l1[i]
for j in 0 ..< len(l2) {
if (num == l2[j]) {
times += 1
}
}
sim += num * times
}
// Part 2
fmt.println(sim)
}