logan_2023/day4/d4p2.go

72 lines
1.4 KiB
Go
Raw Normal View History

2023-12-05 06:34:26 -05:00
// +build linux
package main
import (
"fmt"
//"io"
//"bufio"
"os"
"strings"
"regexp"
)
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 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)
}