-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.go
51 lines (38 loc) · 1.07 KB
/
day07.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
package main
import (
"collections"
"math"
"mathutil"
"os"
"panic"
"readers"
"strings"
)
func Day07Part1() int {
file, err := os.Open("assets/day07.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
positionStrings := strings.Split(lines[0], ",")
positions := collections.StringSliceToIntSlice(positionStrings)
median := mathutil.MedianInt(positions)
fuelRequired := collections.MapInt(positions, func(i int) int {
distance := math.Abs(float64(i) - median)
return int(distance)
})
return collections.SumInt(fuelRequired)
}
func Day07Part2() int {
file, err := os.Open("assets/day07.txt")
panic.Check(err)
lines, err := readers.ReadStrings(file)
panic.Check(err)
positionStrings := strings.Split(lines[0], ",")
positions := collections.StringSliceToIntSlice(positionStrings)
average := int(mathutil.AverageInt(positions))
fuelRequired := collections.MapInt(positions, func(i int) int {
distance := int(math.Abs(float64(i) - float64(average)))
return mathutil.SumUpTo(distance)
})
return collections.SumInt(fuelRequired)
}