-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (33 loc) · 825 Bytes
/
main.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
// -*- compile-command: "go run main.go ../example1.txt ../input.txt"; -*-
package main
import (
"flag"
"fmt"
"log"
"strings"
. "github.com/gmlewis/advent-of-code-2021/enum"
"github.com/gmlewis/advent-of-code-2021/mathfn"
"github.com/gmlewis/advent-of-code-2021/must"
)
var logf = log.Printf
var printf = fmt.Printf
func main() {
flag.Parse()
Each(flag.Args(), process)
}
func process(filename string) {
logf("Processing %v ...", filename)
buf := must.ReadFile(filename)
pos := Map(strings.Split(buf, ","), must.Atoi)
max := Max(pos)
min := Min(pos)
logf("min=%v, max=%v", min, max)
f := func(i int) int {
return Reduce(pos, 0, func(x, acc int) int {
d := mathfn.Abs(i - x)
return acc + (d * (d + 1) / 2)
})
}
bestSum := Min(Map(Range(min, max), f))
printf("Solution: %v\n", bestSum)
}