-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02_1.go
55 lines (44 loc) · 1.16 KB
/
day02_1.go
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
package day02
import (
"AdventOfCode_2015_Go/utilities"
"regexp"
"sort"
"strconv"
)
type Present struct {
long, width, height int64
}
func Executepart1() int {
var fileName string = "./day02/day02.txt"
// var fileName string = "./day02/test_input_02.txt"
var result int = 0
if fileContent, err := utilities.ReadFileAsLines(fileName); err == nil {
for _, line := range fileContent {
tmp := ExtractParts(line)
result += CalculatePresent(tmp)
}
}
return result
}
func ExtractParts(input string) Present {
r, _ := regexp.Compile(`\d+`)
dim := make([]int64, 3)
if parts := r.FindAllString(input, 3); parts != nil {
dim[0], _ = strconv.ParseInt(parts[0], 10, 0)
dim[1], _ = strconv.ParseInt(parts[1], 10, 0)
dim[2], _ = strconv.ParseInt(parts[2], 10, 0)
return Present{dim[0], dim[1], dim[2]}
}
return Present{}
}
func CalculatePresent(present Present) int {
values := make([]int64, 3)
values[0] = present.long
values[1] = present.width
values[2] = present.height
sort.Sort(utilities.Int64Array(values))
return int(values[0]) +
2*int(present.long*present.width) +
2*int(present.width*present.height) +
2*int(present.long*present.height)
}