-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday05.go
More file actions
141 lines (118 loc) · 3.58 KB
/
day05.go
File metadata and controls
141 lines (118 loc) · 3.58 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"flag"
"fmt"
)
func catchUserInput() (string, byte, bool) {
var debug bool
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.Parse()
switch *execPartPtr {
case "a":
return *filenamePtr, 'a', debug
case "b":
return *filenamePtr, 'b', debug
default:
return *filenamePtr, 'z', debug
}
}
func calcRange(calcChar rune, lowerLimit int, upperLimit int, debug bool) (int, int) {
var newSize int
var newLowerLimit, newUpperLimit int
size := upperLimit - lowerLimit + 1
if debug {
fmt.Printf("Before calc... size: %d lowerLimit: %d upperLimit: %d\n", size, lowerLimit, upperLimit)
}
newSize = size / 2
if calcChar == 'F' || calcChar == 'L' {
newLowerLimit = lowerLimit
newUpperLimit = lowerLimit + newSize - 1
} else {
newLowerLimit = lowerLimit + newSize
newUpperLimit = upperLimit
}
if debug {
fmt.Printf("After calc... size: %d lowerLimit: %d upperLimit: %d\n", newSize, newLowerLimit, newUpperLimit)
}
return newLowerLimit, newUpperLimit
}
func decodeSinglePass(boardingPass string, part byte, debug bool) int {
if debug {
fmt.Println("Processing: ", boardingPass)
}
//var rows int = 128
var lowerLimit int = 0
var upperLimit int = 127
var rowNumber int = 0
for _, rowChar := range boardingPass[0:7] {
if debug {
fmt.Printf("Row Instruction: %c\n", rowChar)
}
lowerLimit, upperLimit = calcRange(rowChar, lowerLimit, upperLimit, debug)
}
rowNumber = lowerLimit
lowerLimit = 0
upperLimit = 7
var columnNumber int = 0
for _, columnChar := range boardingPass[7:] {
if debug {
fmt.Printf("Column Instruction: %c\n", columnChar)
}
lowerLimit, upperLimit = calcRange(columnChar, lowerLimit, upperLimit, debug)
}
columnNumber = lowerLimit
return (rowNumber * 8) + columnNumber
}
func decodeBoardingPasses(filename string, part byte, debug bool) int {
var highestSeatID int = 0
var currentSeatID int
var seatPlan [1000]bool
puzzleInput, _ := readFile(filename)
for _, boardingPass := range puzzleInput {
currentSeatID = decodeSinglePass(boardingPass, part, debug)
if part == 'a' {
if currentSeatID > highestSeatID {
highestSeatID = currentSeatID
}
} else {
seatPlan[currentSeatID] = true
}
}
if part == 'a' {
return highestSeatID
}
// part b so return the empty seat not next to others, which should be ours
for i := 0; i < 1000; i++ {
if !seatPlan[i] {
if debug {
fmt.Printf("Seat %d is available\n", i)
}
if i > 0 {
if seatPlan[i-1] && seatPlan[i+1] {
// Found a seat on its own. Must be ours
return i
}
}
}
}
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 if execPart == 'a' {
fmt.Println("Highest Seat Number: ", decodeBoardingPasses(filenamePtr, execPart, debug))
} else {
fmt.Println("Our Seat Number: ", decodeBoardingPasses(filenamePtr, execPart, debug))
}
if debug {
fmt.Printf("Boarding Card: %s Seat Number: %d\n", "FBFBBFFRLR", decodeSinglePass("FBFBBFFRLR", execPart, debug))
fmt.Printf("Boarding Card: %s Seat Number: %d\n", "BFFFBBFRRR", decodeSinglePass("BFFFBBFRRR", execPart, debug))
fmt.Printf("Boarding Card: %s Seat Number: %d\n", "FFFBBBFRRR", decodeSinglePass("FFFBBBFRRR", execPart, debug))
fmt.Printf("Boarding Card: %s Seat Number: %d\n", "BBFFBBFRLL", decodeSinglePass("BBFFBBFRLL", execPart, debug))
}
}