|
@ -69,24 +69,42 @@ func (d day04) solve1(r io.Reader) string { |
|
|
return fmt.Sprintf("%d", sum) |
|
|
return fmt.Sprintf("%d", sum) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func expand(cursor, matches int, duplicates []int) []int { |
|
|
|
|
|
if cursor+matches > len(duplicates) { |
|
|
|
|
|
tmp := make([]int, cursor+matches) |
|
|
|
|
|
for i := range duplicates { |
|
|
|
|
|
tmp[i] = duplicates[i] |
|
|
|
|
|
} |
|
|
|
|
|
duplicates = tmp |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for i := cursor; i < cursor+matches; i++ { |
|
|
|
|
|
duplicates[i] = duplicates[i] + 1 |
|
|
|
|
|
|
|
|
|
|
|
// only count duplicates if we are past the first card
|
|
|
|
|
|
if cursor > 0 { |
|
|
|
|
|
duplicates[i] = duplicates[i] + duplicates[cursor-1] |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return duplicates |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func (d day04) solve2(r io.Reader) string { |
|
|
func (d day04) solve2(r io.Reader) string { |
|
|
var ( |
|
|
var ( |
|
|
current int |
|
|
cursor int |
|
|
sum = 1 |
|
|
duplicates = make([]int, 0) |
|
|
cards = make([]int, 185) |
|
|
scanner = bufio.NewScanner(r) |
|
|
scanner = bufio.NewScanner(r) |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
for matches := -1; scanner.Scan() && matches != 0; current++ { |
|
|
for matches := -1; scanner.Scan() && matches != 0; cursor++ { |
|
|
_, line, _ := strings.Cut(scanner.Text(), ": ") |
|
|
_, line, _ := strings.Cut(scanner.Text(), ": ") |
|
|
sum++ |
|
|
matches := countwinings(line) |
|
|
matches = countwinings(line) |
|
|
duplicates = expand(cursor, matches, duplicates) |
|
|
for i := 1; i <= matches; i++ { |
|
|
|
|
|
cards[current+i] = cards[current+i] + 1 + cards[current] |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for _, c := range cards { |
|
|
sum := cursor |
|
|
|
|
|
for _, c := range duplicates { |
|
|
sum += c |
|
|
sum += c |
|
|
} |
|
|
} |
|
|
return fmt.Sprintf("%d", sum) |
|
|
return fmt.Sprintf("%d", sum) |
|
|