-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2201.go
67 lines (56 loc) · 1.03 KB
/
aoc2201.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
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"io/ioutil"
"strings"
"strconv"
"sort"
)
func main() {
var r1, r2 int
r1, r2 = solve("_inputs/2201.0")
fmt.Println("data")
fmt.Println("Star 1:", r1)
fmt.Println("Star 2:", r2, "\n")
r1, r2 = solve("_inputs/2201.1")
fmt.Println("test")
fmt.Println("Star 1:", r1)
fmt.Println("Star 2:", r2)
}
func solve(path string) (int, int) {
data, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
lines := string(data)
var a []int
for _, line := range strings.Split(lines, "\n\n") {
sum := 0
for _, l := range strings.Split(line, "\n") {
n, err := strconv.Atoi(l)
if err != err {
panic(err)
}
sum += n
}
a = append(a, sum)
}
// part 1 . find max in slice
res := 0
for i, n := range a {
if i == 0 || res < n {
res = n
}
}
// part 2 . sum of the max 3
// way 1 . inituitive
/*
sort.Ints(a)
n := len(a) - 1
res2 := a[n] + a[n - 1] + a[n - 2]
*/
// way 2 . reverse sort
sort.Sort(sort.Reverse(sort.IntSlice(a)))
res2 := a[0] + a[1] + a[2]
return res, res2
}