|
@@ -12,6 +12,8 @@ type Card struct {
|
|
|
id int
|
|
|
winning []int
|
|
|
owned []int
|
|
|
+ wins int
|
|
|
+ count int
|
|
|
}
|
|
|
|
|
|
func readNumbers(part string) []int {
|
|
@@ -48,7 +50,7 @@ func readInput(file *os.File) []Card {
|
|
|
break
|
|
|
}
|
|
|
|
|
|
- var current Card
|
|
|
+ current := Card{count: 1}
|
|
|
n, err := fmt.Sscanf(line, "Card %d:", ¤t.id)
|
|
|
if n != 1 || err != nil {
|
|
|
log.Fatalf("Failed to read card id: %s\n%s", line, err)
|
|
@@ -104,6 +106,22 @@ func part1(cards []Card) int {
|
|
|
if count > 0 {
|
|
|
result += pow(count - 1)
|
|
|
}
|
|
|
+
|
|
|
+ cards[i].wins = count
|
|
|
+ }
|
|
|
+
|
|
|
+ return result
|
|
|
+}
|
|
|
+
|
|
|
+func part2(cards []Card) int {
|
|
|
+ var result int
|
|
|
+ for i := range cards {
|
|
|
+ result += cards[i].count
|
|
|
+ if cards[i].wins > 0 {
|
|
|
+ for j := i + 1; j <= i+cards[i].wins; j++ {
|
|
|
+ cards[j].count += cards[i].count
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return result
|
|
@@ -123,4 +141,5 @@ func main() {
|
|
|
|
|
|
cards := readInput(file)
|
|
|
fmt.Println("Part1:", part1(cards))
|
|
|
+ fmt.Println("Part2:", part2(cards))
|
|
|
}
|