75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
//"io"
|
|
//"bufio"
|
|
"os"
|
|
"strings"
|
|
"regexp"
|
|
"runtime"
|
|
)
|
|
|
|
func find_num_wins(winners string, numbers string) int {
|
|
wins := 0
|
|
for _, number := range strings.Split(numbers, " ") {
|
|
for _, winner := range strings.Split(winners, " ") {
|
|
if number == winner && len(number) > 0 {
|
|
wins++
|
|
}
|
|
}
|
|
}
|
|
return wins
|
|
}
|
|
|
|
|
|
func main() {
|
|
if runtime.GOOS != "linux" {
|
|
panic("This is for Linux only.")
|
|
}
|
|
|
|
if len(os.Args) < 2 {
|
|
panic("You did not specify an input file.")
|
|
}
|
|
|
|
rawInput, err := os.ReadFile(os.Args[1])
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
stringInput := strings.Trim(string(rawInput), "\n")
|
|
stringInput = regexp.MustCompile(`(Card.*:\ )`).ReplaceAllString(stringInput, "")
|
|
|
|
lines := strings.Split(stringInput, "\n")
|
|
|
|
var cards []int
|
|
cards = make([]int, len(lines))
|
|
|
|
for i,_ := range cards {
|
|
cards[i] = 1
|
|
}
|
|
|
|
sum := 0
|
|
|
|
for card, game := range lines {
|
|
if len(game) < 10 {
|
|
continue
|
|
}
|
|
|
|
game_ := strings.Split(game, " | ")
|
|
winners := game_[0]
|
|
numbers := game_[1]
|
|
|
|
wins := find_num_wins(numbers, winners)
|
|
|
|
sum += cards[card]
|
|
|
|
for i := 0; i < wins; i++ {
|
|
cards[card+i+1] += cards[card]
|
|
}
|
|
|
|
fmt.Println(card+1, cards[card])
|
|
}
|
|
|
|
fmt.Println("Answer: ", sum)
|
|
}
|