-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday02.go
More file actions
88 lines (75 loc) · 1.86 KB
/
day02.go
File metadata and controls
88 lines (75 loc) · 1.86 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"AdventOfCode-go/advent2017/utils"
"fmt"
"strconv"
"strings"
)
func calcRowDifferenceDivisors(row string, debug bool) int {
/*
For each row, determine the difference between the largest value and the smallest value;
the checksum is the sum of all of these differences.
*/
var currValue1, currValue2 int
for _, i := range strings.Fields(row) {
for _, j := range strings.Fields(row) {
if i != j {
currValue1, _ = strconv.Atoi(i)
currValue2, _ = strconv.Atoi(j)
if currValue1 > currValue2 {
if currValue1%currValue2 == 0 {
return currValue1 / currValue2
}
} else {
if currValue2%currValue1 == 0 {
return currValue2 / currValue1
}
}
}
}
}
return 0
}
func calcRowDifference(row string, debug bool) int {
/*
For each row, determine the difference between the largest value and the smallest value;
the checksum is the sum of all of these differences.
*/
var minValue int = 9999999
var maxValue int = 0
var currValue int
for _, j := range strings.Fields(row) {
currValue, _ = strconv.Atoi(j)
if currValue < minValue {
minValue = currValue
}
if currValue > maxValue {
maxValue = currValue
}
}
return maxValue - minValue
}
func solveChecksum(filename string, part byte, debug bool) int {
var checksum int
puzzleInput, _ := utils.ReadFile(filename)
if part == 'a' {
for i := range puzzleInput {
checksum += calcRowDifference(puzzleInput[i], debug)
}
return checksum
} else {
for i := range puzzleInput {
checksum += calcRowDifferenceDivisors(puzzleInput[i], debug)
}
return checksum
}
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", solveChecksum(filenamePtr, execPart, debug))
}
}