First draft of working part two, heavily unoptimized
This commit is contained in:
parent
76da133e30
commit
8871be0bc9
@ -20,7 +20,8 @@ func PrintAndWait(x ...any) {
|
|||||||
fmt.Scanln()
|
fmt.Scanln()
|
||||||
}
|
}
|
||||||
|
|
||||||
func CalcScore (s []string, t *int) {
|
func SplitSets(s []string) ([]string, []string) {
|
||||||
|
|
||||||
// Split the two sets into winning and my numbers
|
// Split the two sets into winning and my numbers
|
||||||
tempwinningNumbers := s[0]
|
tempwinningNumbers := s[0]
|
||||||
tempMyNumbers := s[1]
|
tempMyNumbers := s[1]
|
||||||
@ -29,15 +30,37 @@ func CalcScore (s []string, t *int) {
|
|||||||
myNumbers := renum.FindAllString(tempMyNumbers, -1)
|
myNumbers := renum.FindAllString(tempMyNumbers, -1)
|
||||||
winningNumbers := renum.FindAllString(tempwinningNumbers, -1)
|
winningNumbers := renum.FindAllString(tempwinningNumbers, -1)
|
||||||
|
|
||||||
|
return myNumbers, winningNumbers
|
||||||
|
}
|
||||||
|
|
||||||
|
func FindMatches(myNum []string, winNum []string) int {
|
||||||
matches := 0
|
matches := 0
|
||||||
for i := range myNumbers {
|
for i := range myNum {
|
||||||
for j := range winningNumbers {
|
for j := range winNum {
|
||||||
if (myNumbers[i] == winningNumbers[j]) {
|
if (myNum[i] == winNum[j]) {
|
||||||
matches += 1
|
matches += 1
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return matches
|
||||||
|
}
|
||||||
|
|
||||||
|
func CalcTickets(s []string, index int, tpr []int ) {
|
||||||
|
|
||||||
|
myNumbers, winningNumbers := SplitSets(s)
|
||||||
|
matches := FindMatches(myNumbers, winningNumbers)
|
||||||
|
if (matches > 0) {
|
||||||
|
for i := index; i < index + matches; i++ {
|
||||||
|
tpr[i+1] += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func CalcScore (s []string, t *int) {
|
||||||
|
|
||||||
|
myNumbers, winningNumbers := SplitSets(s)
|
||||||
|
matches := FindMatches(myNumbers, winningNumbers)
|
||||||
if(matches > 0) {
|
if(matches > 0) {
|
||||||
tempTotal := 1
|
tempTotal := 1
|
||||||
for i := 0; i < matches - 1; i++ {
|
for i := 0; i < matches - 1; i++ {
|
||||||
@ -45,7 +68,6 @@ func CalcScore (s []string, t *int) {
|
|||||||
}
|
}
|
||||||
*t += tempTotal
|
*t += tempTotal
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -53,20 +75,40 @@ func main() {
|
|||||||
check(err)
|
check(err)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
// The total is a simple sum
|
||||||
var total int = 0
|
var total int = 0
|
||||||
var totalPoint *int = &total
|
var totalPoint *int = &total
|
||||||
|
// To keep track of the amount of tickets, an array as long
|
||||||
|
// as the number of lines
|
||||||
|
var lines []string
|
||||||
scanner := bufio.NewScanner(file)
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
lines = append(lines, scanner.Text())
|
||||||
|
}
|
||||||
|
ticketsPerRow := make([]int, len(lines))
|
||||||
|
tprPoint := ticketsPerRow[0:len(lines)]
|
||||||
|
|
||||||
|
for i := range ticketsPerRow {
|
||||||
|
ticketsPerRow[i] = 1
|
||||||
|
}
|
||||||
|
|
||||||
// Scan every line
|
// Scan every line
|
||||||
for scanner.Scan() {
|
for i := 0; i < len(lines); i++ {
|
||||||
// e.g.: Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
// e.g.: Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
||||||
line := scanner.Text()
|
cardAndNumbers := strings.Split(lines[i], ":")
|
||||||
cardAndNumbers := strings.Split(line, ":")
|
|
||||||
// At this point, cardAndNumbers has "Card N"
|
// At this point, cardAndNumbers has "Card N"
|
||||||
// We don't need this information (yet?)
|
// We don't need this information (yet?)
|
||||||
allNumbers := strings.Split(cardAndNumbers[1], "|")
|
allNumbers := strings.Split(cardAndNumbers[1], "|")
|
||||||
CalcScore(allNumbers, totalPoint)
|
CalcScore(allNumbers, totalPoint)
|
||||||
|
for j := 0; j < ticketsPerRow[i]; j++ {
|
||||||
|
CalcTickets(allNumbers, i, tprPoint)
|
||||||
}
|
}
|
||||||
fmt.Printf("The scratchcards are worth %d points.", total)
|
}
|
||||||
|
|
||||||
|
numTickets := 0
|
||||||
|
for i := range ticketsPerRow {
|
||||||
|
numTickets += ticketsPerRow[i]
|
||||||
|
}
|
||||||
|
fmt.Printf("The scratchcards are worth %d points.\n", total)
|
||||||
|
fmt.Printf("In total, I have %d scratchcards.", numTickets)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user