day4, part2. here come off-by-one errors
This commit is contained in:
@@ -10,13 +10,57 @@ import (
|
||||
func Run() int {
|
||||
fmt.Println("in day 4")
|
||||
cards := ReadAllCards()
|
||||
result := 0
|
||||
fmt.Printf("> got cards %+v\n", cards)
|
||||
// result := 0
|
||||
|
||||
// for _, card := range cards {
|
||||
// // fmt.Printf("> checking card %+v with points %d", card, card.GetPoints())
|
||||
// result += card.GetPoints()
|
||||
// }
|
||||
|
||||
result := ActuallWinCounting(cards)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
// starting from card #1 count how many winning points it has
|
||||
// then add copies to map of 'amount of cards i have'
|
||||
// multiplied to amount of current card i have, ok i guess
|
||||
func ActuallWinCounting(cards []Card) int {
|
||||
cardAmounts := make(map[int]int)
|
||||
// i had off-by-one error, with card 204 not being updated from 196
|
||||
maxCard := len(cards)
|
||||
fmt.Printf("> counting winning, total cards are %d\ncards are %+v\n\n", maxCard, cards)
|
||||
for _, card := range cards {
|
||||
fmt.Printf("> checking card %+v with points %d", card, card.GetPoints())
|
||||
result += card.GetPoints()
|
||||
}
|
||||
fmt.Printf("> checking card %d\n", card.Title)
|
||||
winningNums := card.GetWinNums()
|
||||
curCardAmount, found := cardAmounts[card.Title]
|
||||
if !found {
|
||||
curCardAmount = 1
|
||||
cardAmounts[card.Title] = curCardAmount
|
||||
}
|
||||
fmt.Printf(">> from curCard %d winning points are %d. And it's amount is %d\n", card.Title, winningNums, curCardAmount)
|
||||
for i := 1; i <= winningNums; i++ {
|
||||
childCardNum := card.Title + i
|
||||
if childCardNum > maxCard {
|
||||
fmt.Printf(">>>> should skip increasing child card %d from curCard %d\n", childCardNum, card.Title)
|
||||
break
|
||||
}
|
||||
curAmount, found := cardAmounts[childCardNum]
|
||||
if !found {
|
||||
cardAmounts[childCardNum] = 1 + curCardAmount
|
||||
} else {
|
||||
cardAmounts[childCardNum] += curCardAmount
|
||||
}
|
||||
|
||||
fmt.Printf(">> increasing child card %d from %d to %d by curCard %d\n", childCardNum, curAmount, cardAmounts[childCardNum], card.Title)
|
||||
}
|
||||
}
|
||||
var result int = 0
|
||||
for _, amount := range cardAmounts {
|
||||
result += amount
|
||||
}
|
||||
fmt.Printf("> got card amounts: %+v\n", cardAmounts)
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -27,14 +71,14 @@ type Card struct {
|
||||
}
|
||||
|
||||
func ReadAllCards() []Card {
|
||||
filename := "day4/example"
|
||||
filename := "day4/input"
|
||||
bytes, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("can't read file %s", filename))
|
||||
}
|
||||
text := string(bytes)
|
||||
lines := strings.Split(text, "\n")
|
||||
result := make([]Card, len(lines))
|
||||
result := make([]Card, 0, len(lines))
|
||||
|
||||
for _, line := range lines {
|
||||
if line == "" {
|
||||
@@ -48,12 +92,13 @@ func ReadAllCards() []Card {
|
||||
|
||||
// read line like 'Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53'
|
||||
func ReadCard(line string) Card {
|
||||
fmt.Printf(">> reading line %s\n", line)
|
||||
result := Card{}
|
||||
extractingTitle := strings.Split(line, ":")
|
||||
title := extractingTitle[0]
|
||||
numberPart := extractingTitle[1]
|
||||
|
||||
titleNumStr := strings.Split(title, " ")[1]
|
||||
titleNumStr := strings.Fields(title)[1]
|
||||
titleNum, err := strconv.Atoi(titleNumStr)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("error extracting card title: %s, %s", line, titleNumStr))
|
||||
@@ -84,6 +129,23 @@ func ReadCard(line string) Card {
|
||||
result.OwnNums = append(result.OwnNums, ownNum)
|
||||
}
|
||||
|
||||
fmt.Printf(">>> returning %+v\n", result)
|
||||
return result
|
||||
}
|
||||
|
||||
func (c *Card)GetWinNums() int {
|
||||
winningSet := make(map[int]any)
|
||||
for _, winNum := range c.WinningNums {
|
||||
winningSet[winNum] = struct{}{}
|
||||
}
|
||||
|
||||
result := 0
|
||||
for _, ownNum := range c.OwnNums {
|
||||
_, isWinning := winningSet[ownNum]
|
||||
if isWinning {
|
||||
result += 1
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user