-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
172 lines (142 loc) · 3.14 KB
/
main.go
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
// Use Flags to run a part
methodP := flag.String("method", "p1", "The method/part that should be run, valid are p1,p2 and test")
flag.Parse()
switch *methodP {
case "p1":
partOne()
break
case "p2":
partTwo()
break
case "test":
break
}
}
func partOne() {
input := readInput()
deck := makeDeck(10007)
deck = runHand(input, deck)
pos := 0
for i, val := range deck {
if val == 2019 {
pos = i
}
}
fmt.Println("The position of card 2019 is", pos)
}
func makeDeck(deckSize int) []int {
deck := []int{}
for i := 0; i < deckSize; i++ {
deck = append(deck, i)
}
return deck
}
func partTwo() {
//input := readInput()
}
func runHand(input []string, startDeck []int) []int {
newDeck := []int{}
newDeck = startDeck
for _, val := range input {
// Figure out what method we are using, then do it
if strings.Contains(val, "deal into new stack") {
newDeck = dealNewStack(newDeck)
continue
}
if strings.Contains(val, "deal with increment") {
incrementStr := strings.SplitAfter(val, "increment ")[1]
increment, _ := strconv.Atoi(incrementStr)
newDeck = dealWithIncrement(newDeck, increment)
continue
}
if strings.Contains(val, "cut") {
cutStr := strings.SplitAfter(val, "cut ")[1]
cut, _ := strconv.Atoi(cutStr)
if cut < 0 {
newDeck = cutDeckNegative(newDeck, cut*-1)
} else {
newDeck = cutDeckPositive(newDeck, cut)
}
continue
}
}
return newDeck
}
func dealNewStack(deck []int) []int {
newDeck := []int{}
for i := len(deck) - 1; i >= 0; i-- {
newDeck = append(newDeck, deck[i])
}
return newDeck
}
func dealWithIncrement(deck []int, increment int) []int {
newDeck := []int{}
// setup array
for i := 0; i < len(deck); i++ {
newDeck = append(newDeck, -1)
}
startDeckCounter := 0
newDeckCounter := 0
for startDeckCounter < len(deck) {
newDeck[newDeckCounter] = deck[startDeckCounter]
startDeckCounter++
newDeckCounter = mod(newDeckCounter+increment, len(deck))
}
for i, val := range newDeck {
if val == -1 {
fmt.Println(i, ":", val)
}
}
return newDeck
}
func cutDeckPositive(deck []int, cut int) []int {
newDeck := []int{}
// First take bottom half of cards and place them on top
// then take "cut" cards and place them on bottom
for i := cut; i < len(deck); i++ {
newDeck = append(newDeck, deck[i])
}
for i := 0; i < cut; i++ {
newDeck = append(newDeck, deck[i])
}
return newDeck
}
func cutDeckNegative(deck []int, cut int) []int {
newDeck := []int{}
// Opposite of cut deck positive
for i := len(deck) - cut; i < len(deck); i++ {
newDeck = append(newDeck, deck[i])
}
for i := 0; i < len(deck)-cut; i++ {
newDeck = append(newDeck, deck[i])
}
return newDeck
}
// Read data from input.txt
// Return the string, so that we can deal with it however
func readInput() []string {
var input []string
f, _ := os.Open("input.txt")
scanner := bufio.NewScanner(f)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func mod(d, m int) int {
var res int = d % m
if (res < 0 && m > 0) || (res > 0 && m < 0) {
return res + m
}
return res
}