-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03.go
More file actions
82 lines (65 loc) · 2.04 KB
/
day03.go
File metadata and controls
82 lines (65 loc) · 2.04 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
package main
import (
"flag"
"fmt"
)
func catchUserInput() (string, byte, bool, int, int) {
var debug bool
var slopeX, slopeY int
filenamePtr := flag.String("file", "testInput.txt", "Filename containing the program to run")
execPartPtr := flag.String("part", "a", "Which part of the puzzle do you want to calc (a or b)")
flag.BoolVar(&debug, "debug", false, "Turn debug on")
flag.IntVar(&slopeX, "slopex", 3, "X component of Slope")
flag.IntVar(&slopeY, "slopey", 1, "Y component of Slope")
flag.Parse()
switch *execPartPtr {
case "a":
return *filenamePtr, 'a', debug, slopeX, slopeY
case "b":
return *filenamePtr, 'b', debug, slopeX, slopeY
default:
return *filenamePtr, 'z', debug, slopeX, slopeY
}
}
func howManyTrees(filename string, part byte, debug bool, slopeX int, slopeY int) int {
var currentXPos, currentYPos int
var treeCount int = 0
puzzleInput, _ := readFile(filename)
maxX := len(puzzleInput[0])
maxY := len(puzzleInput)
if debug {
fmt.Printf("Puzzle Side is X:%d Y:%d\n", maxX, maxY)
}
currentXPos = slopeX
currentYPos = slopeY
for ok := true; ok; ok = (currentYPos < maxY) {
if puzzleInput[currentYPos][currentXPos] == '#' {
//fmt.Println("Found a tree")
treeCount++
}
if debug {
fmt.Printf(puzzleInput[currentYPos])
}
currentXPos = (currentXPos + slopeX) % maxX
currentYPos = currentYPos + slopeY
}
return treeCount
}
// Main routine
func main() {
filenamePtr, execPart, debug, slopeX, slopeY := catchUserInput()
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
if execPart == 'a' {
fmt.Println("Number of trees: ", howManyTrees(filenamePtr, execPart, debug, slopeX, slopeY))
} else {
result := howManyTrees(filenamePtr, execPart, debug, 1, 1)
result *= howManyTrees(filenamePtr, execPart, debug, 3, 1)
result *= howManyTrees(filenamePtr, execPart, debug, 5, 1)
result *= howManyTrees(filenamePtr, execPart, debug, 7, 1)
result *= howManyTrees(filenamePtr, execPart, debug, 1, 2)
fmt.Println("Result is: ", result)
}
}
}