From 19d312c0fb638252c93fb50c77d1cd2d2f5af583 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 8 Dec 2021 15:48:36 +0100 Subject: Added solutions for 7th day --- 2021/07/second.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 2021/07/second.go (limited to '2021/07/second.go') diff --git a/2021/07/second.go b/2021/07/second.go new file mode 100644 index 0000000..5133f66 --- /dev/null +++ b/2021/07/second.go @@ -0,0 +1,65 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" +) + +func min(a, b int) int { + if a < b { + return a + } else { + return b + } +} +func abs(a int) int { + if a > 0 { + return a + } else { + return -a + } +} + +func splitOnCommas(data []byte, atEOF bool) (advance int, token []byte, err error) { + var ch byte + for advance, ch = range data { + if ch < '0' || ch > '9' { + break + } + } + if advance > 0 { + token = data[0:advance] + advance++ // skip the comma + } + return +} + +func main() { + positions := make([]int, 0) + + s := bufio.NewScanner(os.Stdin) + s.Split(splitOnCommas) + for s.Scan() { + n, _ := strconv.Atoi(s.Text()) + positions = append(positions, n) + } + + sum := 0 + for _, i := range positions { + sum += i + } + mean := sum / len(positions) + + // doing a mean with integers sucks + total := 0 + total2 := 0 + for _, i := range positions { + diff := abs(i - mean) + total += diff * (diff + 1) / 2 + diff = abs(i - mean - 1) + total2 += diff * (diff + 1) / 2 + } + fmt.Println(min(total, total2)) +} -- cgit v1.2.3