-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday01.go
More file actions
101 lines (86 loc) · 2.39 KB
/
day01.go
File metadata and controls
101 lines (86 loc) · 2.39 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
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strconv"
)
func catchUserInput() (string, byte, bool) {
var debug bool
filenamePtr := flag.String("file", "input.txt", "Filename containing the program to run")
execPartPtr := flag.String("part", "a", "Which part of day03 do you want to calc (a or b)")
flag.BoolVar(&debug, "debug", false, "Turn debug on")
flag.Parse()
switch *execPartPtr {
case "a":
return *filenamePtr, 'a', debug
case "b":
return *filenamePtr, 'b', debug
default:
return *filenamePtr, 'z', debug
}
}
// Read the text file passed in by name into a array of strings
// Returns the array as the first return variable
func readFile(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func convertInputToNumbers(stringList []string) []int {
var numberList []int
numberList = make([]int, len(stringList))
for i := 0; i < len(stringList); i++ {
numberList[i], _ = strconv.Atoi(stringList[i])
}
return numberList
}
func checkExpenses(filename string, part byte, debug bool) int {
puzzleInput, _ := readFile(filename)
expenses := convertInputToNumbers(puzzleInput)
if part == 'a' {
for i := 0; i < len(expenses); i++ {
for j := 0; j < len(expenses); j++ {
if i != j { // Ignore when we're looking at the same expenses value
if expenses[i]+expenses[j] == 2020 {
fmt.Printf("Got it: %d and %d\n", expenses[i], expenses[j])
return expenses[i] * expenses[j]
}
}
}
}
} else {
for i := 0; i < len(expenses); i++ {
for j := 0; j < len(expenses); j++ {
for k := 0; k < len(expenses); k++ {
if (i != j) && (i != k) && (j != k) { // Ignore when we're looking at the same expenses value
if expenses[i]+expenses[j]+expenses[k] == 2020 {
fmt.Printf("Got it: %d and %d and %d\n", expenses[i], expenses[j], expenses[k])
return expenses[i] * expenses[j] * expenses[k]
}
}
}
}
}
}
fmt.Println(puzzleInput)
return 0
}
// Main routine
func main() {
filenamePtr, execPart, debug := catchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", checkExpenses(filenamePtr, execPart, debug))
}
}