Working first part of day04
This commit is contained in:
parent
7a8c20acc6
commit
76da133e30
1
day04/inputs
Symbolic link
1
day04/inputs
Symbolic link
@ -0,0 +1 @@
|
||||
../inputs
|
||||
72
day04/scratchcards.go
Normal file
72
day04/scratchcards.go
Normal file
@ -0,0 +1,72 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"bufio"
|
||||
"os"
|
||||
// "strconv"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func check(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func PrintAndWait(x ...any) {
|
||||
fmt.Print(x)
|
||||
fmt.Scanln()
|
||||
}
|
||||
|
||||
func CalcScore (s []string, t *int) {
|
||||
// Split the two sets into winning and my numbers
|
||||
tempwinningNumbers := s[0]
|
||||
tempMyNumbers := s[1]
|
||||
// Regex to populate a string with numbers
|
||||
renum := regexp.MustCompile("[0-9]+")
|
||||
myNumbers := renum.FindAllString(tempMyNumbers, -1)
|
||||
winningNumbers := renum.FindAllString(tempwinningNumbers, -1)
|
||||
|
||||
matches := 0
|
||||
for i := range myNumbers {
|
||||
for j := range winningNumbers {
|
||||
if (myNumbers[i] == winningNumbers[j]) {
|
||||
matches += 1
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
if(matches > 0) {
|
||||
tempTotal := 1
|
||||
for i := 0; i < matches - 1; i++ {
|
||||
tempTotal *= 2
|
||||
}
|
||||
*t += tempTotal
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
file, err := os.Open("./inputs/day04_input")
|
||||
check(err)
|
||||
defer file.Close()
|
||||
|
||||
var total int = 0
|
||||
var totalPoint *int = &total
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
// Scan every line
|
||||
for scanner.Scan() {
|
||||
// e.g.: Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
||||
line := scanner.Text()
|
||||
cardAndNumbers := strings.Split(line, ":")
|
||||
// At this point, cardAndNumbers has "Card N"
|
||||
// We don't need this information (yet?)
|
||||
allNumbers := strings.Split(cardAndNumbers[1], "|")
|
||||
CalcScore(allNumbers, totalPoint)
|
||||
}
|
||||
fmt.Printf("The scratchcards are worth %d points.", total)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user