Skip to content

Commit

Permalink
2023-12
Browse files Browse the repository at this point in the history
  • Loading branch information
dbut2 committed Dec 15, 2023
1 parent 3d9e08e commit cd26f93
Show file tree
Hide file tree
Showing 7 changed files with 1,525 additions and 0 deletions.
90 changes: 90 additions & 0 deletions 2023/12/01.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"embed"
"strings"

"github.com/dbut2/advent-of-code/pkg/harness"
"github.com/dbut2/advent-of-code/pkg/sti"
"github.com/dbut2/advent-of-code/pkg/utils"
)

//go:embed input.txt
var input string

//go:embed test*.txt
var tests embed.FS

func main() {
h := harness.New(solve, input, tests)
h.Expect(1, 21)
h.Solve()
}

func solve(input string) int {
s := utils.ParseInput(input)

total := 0

for _, line := range s {
springs := strings.Split(line, " ")[0]
goals := strings.Split(line, " ")[1]

goalNumbers := sti.Stis(strings.Split(goals, ","))

total += validSubsets(springs, goalNumbers)
}

return total
}

func validSubsets(springs string, goalNumbers []int) int {
count := 0

if !strings.Contains(springs, "?") {
if valid(springs, goalNumbers) {
return 1
} else {
return 0
}
}

a := strings.Replace(springs, "?", "#", 1)
b := strings.Replace(springs, "?", ".", 1)

count += validSubsets(a, goalNumbers)
count += validSubsets(b, goalNumbers)

return count
}

func valid(springs string, goalNumbers []int) bool {
realNumbers := []int{}
count := 0
for _, char := range springs {
switch char {
case '#':
count++
case '.':
if count != 0 {
realNumbers = append(realNumbers, count)
}
count = 0
}
}
if count != 0 {
realNumbers = append(realNumbers, count)
}

if len(realNumbers) != len(goalNumbers) {
return false
}

for i := range realNumbers {
if realNumbers[i] != goalNumbers[i] {
return false
}
}

return true
}
161 changes: 161 additions & 0 deletions 2023/12/02.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package main

import (
"embed"
"strings"
"sync"
"sync/atomic"

"github.com/dbut2/advent-of-code/pkg/harness"
"github.com/dbut2/advent-of-code/pkg/math"
"github.com/dbut2/advent-of-code/pkg/sti"
"github.com/dbut2/advent-of-code/pkg/utils"
)

//go:embed input.txt
var input string

//go:embed test*.txt
var tests embed.FS

func main() {
h := harness.New(solve, input, tests)
h.Expect(1, 525152)
h.Solve()
}

func solve(input string) int {
s := utils.ParseInput(input)

wg := &sync.WaitGroup{}
total := atomic.Int64{}

for _, line := range s {
wg.Add(1)
springs := strings.Split(line, " ")[0]
goals := strings.Split(line, " ")[1]

longerSprings := strings.Repeat("?"+springs, 5)[1:]
longerGoalNumbers := sti.Stis(strings.Split(strings.Repeat(","+goals, 5)[1:], ","))

go func() {
total.Add(int64(validSets(longerSprings, longerGoalNumbers)))
wg.Done()
}()
}
wg.Wait()
return int(total.Load())
}

func validSets(springs string, goalNumbers []int) int {
// store the count of unique counts of [2]int{count, goalI}
// when we finish processing the full springs string we can count the amount
// of ways to achieve count=0 and goalI=len(goalNumbers)
// this is the number of valid sets of broken springs permutations
lastRound := map[[2]int]int{
[2]int{0, 0}: 1,
}

lastI := 0
for {
if lastI >= len(springs) {
break
}

nextI := min(lastI+5, len(springs))

p := problem{
springs: springs,
maxI: nextI,
goalNumbers: goalNumbers,
totalWant: math.Sum(goalNumbers),
}

nextRound := map[[2]int]int{}
for state, sets := range lastRound {
nextSets := p.validSubsets(lastI, state[0], state[1])
for nextGoalI, nextSet := range nextSets {
if sets*nextSet > 0 {
nextRound[nextGoalI] += sets * nextSet
}
}
}
lastRound = nextRound
lastI = nextI
}

return lastRound[[2]int{0, len(goalNumbers)}]
}

type problem struct {
springs string
maxI int
goalNumbers []int
totalWant int
}

func (p *problem) validSubsets(charI int, count int, goalI int) map[[2]int]int {
// we've processed the entire spring string
// only valid if we've used all the goal numbers
if charI == len(p.springs) {
if goalI == len(p.goalNumbers) {
return map[[2]int]int{[2]int{count, goalI}: 1}
}
if goalI == len(p.goalNumbers)-1 && count == p.goalNumbers[goalI] {
return map[[2]int]int{[2]int{0, goalI + 1}: 1}
}
return nil
}

// we've processed up to the maxI
// this is valid up until this point so return 1 count for this state
if charI == p.maxI {
return map[[2]int]int{[2]int{count, goalI}: 1}
}

switch p.springs[charI] {
case '.':
return p.validSubsetPeriod(charI, count, goalI)
case '#':
return p.validSubsetHash(charI, count, goalI)
case '?':
values := map[[2]int]int{[2]int{0, 0}: 0}
a := p.validSubsetPeriod(charI, count, goalI)
for k, v := range a {
values[k] += v
}
b := p.validSubsetHash(charI, count, goalI)
for k, v := range b {
values[k] += v
}
return values
}
return nil
}

// validSubsetPeriod returns the total number of subsets if the current character is a period
func (p *problem) validSubsetPeriod(charI int, count int, goalI int) map[[2]int]int {
if count != 0 {
if p.goalNumbers[goalI] != count {
return nil
}
return p.validSubsets(charI+1, 0, goalI+1)
}

return p.validSubsets(charI+1, 0, goalI)
}

// validSubsetHash returns the total number of subsets if the current character is a hash
func (p *problem) validSubsetHash(charI int, count int, goalI int) map[[2]int]int {
count++

if goalI >= len(p.goalNumbers) {
return nil
}

if count > p.goalNumbers[goalI] {
return nil
}

return p.validSubsets(charI+1, count, goalI)
}
134 changes: 134 additions & 0 deletions 2023/12/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
\--- Day 12: Hot Springs ---
----------

You finally reach the hot springs! You can see steam rising from secluded areas attached to the primary, ornate building.

As you turn to enter, the [researcher](11) stops you. "Wait - I thought you were looking for the hot springs, weren't you?" You indicate that this definitely looks like hot springs to you.

"Oh, sorry, common mistake! This is actually the [onsen](https://en.wikipedia.org/wiki/Onsen)! The hot springs are next door."

You look in the direction the researcher is pointing and suddenly notice the massive metal helixes towering overhead. "This way!"

It only takes you a few more steps to reach the main gate of the massive fenced-off area containing the springs. You go through the gate and into a small administrative building.

"Hello! What brings you to the hot springs today? Sorry they're not very hot right now; we're having a *lava shortage* at the moment." You ask about the missing machine parts for Desert Island.

"Oh, all of Gear Island is currently offline! Nothing is being manufactured at the moment, not until we get more lava to heat our forges. And our springs. The springs aren't very springy unless they're hot!"

"Say, could you go up and see why the lava stopped flowing? The springs are too cold for normal operation, but we should be able to find one springy enough to launch *you* up there!"

There's just one problem - many of the springs have fallen into disrepair, so they're not actually sure which springs would even be *safe* to use! Worse yet, their *condition records of which springs are damaged* (your puzzle input) are also damaged! You'll need to help them repair the damaged records.

In the giant field just outside, the springs are arranged into *rows*. For each row, the condition records show every spring and whether it is *operational* (`.`) or *damaged* (`#`). This is the part of the condition records that is itself damaged; for some springs, it is simply *unknown* (`?`) whether the spring is operational or damaged.

However, the engineer that produced the condition records also duplicated some of this information in a different format! After the list of springs for a given row, the size of each *contiguous group of damaged springs* is listed in the order those groups appear in the row. This list always accounts for every damaged spring, and each number is the entire size of its contiguous group (that is, groups are always separated by at least one operational spring: `####` would always be `4`, never `2,2`).

So, condition records with no unknown spring conditions might look like this:

```
#.#.### 1,1,3
.#...#....###. 1,1,3
.#.###.#.###### 1,3,1,6
####.#...#... 4,1,1
#....######..#####. 1,6,5
.###.##....# 3,2,1
```

However, the condition records are partially damaged; some of the springs' conditions are actually *unknown* (`?`). For example:

```
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1
```

Equipped with this information, it is your job to figure out *how many different arrangements* of operational and broken springs fit the given criteria in each row.

In the first line (`???.### 1,1,3`), there is exactly *one* way separate groups of one, one, and three broken springs (in that order) can appear in that row: the first three unknown springs must be broken, then operational, then broken (`#.#`), making the whole row `#.#.###`.

The second line is more interesting: `.??..??...?##. 1,1,3` could be a total of *four* different arrangements. The last `?` must always be broken (to satisfy the final contiguous group of three broken springs), and each `??` must hide exactly one of the two broken springs. (Neither `??` could be both broken springs or they would form a single contiguous group of two; if that were true, the numbers afterward would have been `2,3` instead.) Since each `??` can either be `#.` or `.#`, there are four possible arrangements of springs.

The last line is actually consistent with *ten* different arrangements! Because the first number is `3`, the first and second `?` must both be `.` (if either were `#`, the first number would have to be `4` or higher). However, the remaining run of unknown spring conditions have many different ways they could hold groups of two and one broken springs:

```
?###???????? 3,2,1
.###.##.#...
.###.##..#..
.###.##...#.
.###.##....#
.###..##.#..
.###..##..#.
.###..##...#
.###...##.#.
.###...##..#
.###....##.#
```

In this example, the number of possible arrangements for each row is:

* `???.### 1,1,3` - `*1*` arrangement
* `.??..??...?##. 1,1,3` - `*4*` arrangements
* `?#?#?#?#?#?#?#? 1,3,1,6` - `*1*` arrangement
* `????.#...#... 4,1,1` - `*1*` arrangement
* `????.######..#####. 1,6,5` - `*4*` arrangements
* `?###???????? 3,2,1` - `*10*` arrangements

Adding all of the possible arrangement counts together produces a total of `*21*` arrangements.

For each row, count all of the different arrangements of operational and broken springs that meet the given criteria. *What is the sum of those counts?*

Your puzzle answer was `6958`.

\--- Part Two ---
----------

As you look out at the field of springs, you feel like there are way more springs than the condition records list. When you examine the records, you discover that they were actually *folded up* this whole time!

To *unfold the records*, on each row, replace the list of spring conditions with five copies of itself (separated by `?`) and replace the list of contiguous groups of damaged springs with five copies of itself (separated by `,`).

So, this row:

```
.# 1
```

Would become:

```
.#?.#?.#?.#?.# 1,1,1,1,1
```

The first line of the above example would become:

```
???.###????.###????.###????.###????.### 1,1,3,1,1,3,1,1,3,1,1,3,1,1,3
```

In the above example, after unfolding, the number of possible arrangements for some rows is now much larger:

* `???.### 1,1,3` - `*1*` arrangement
* `.??..??...?##. 1,1,3` - `*16384*` arrangements
* `?#?#?#?#?#?#?#? 1,3,1,6` - `*1*` arrangement
* `????.#...#... 4,1,1` - `*16*` arrangements
* `????.######..#####. 1,6,5` - `*2500*` arrangements
* `?###???????? 3,2,1` - `*506250*` arrangements

After unfolding, adding all of the possible arrangement counts together produces `*525152*`.

Unfold your condition records; *what is the new sum of possible arrangement counts?*

Your puzzle answer was `6555315065024`.

Both parts of this puzzle are complete! They provide two gold stars: \*\*

At this point, you should [return to your Advent calendar](/2023) and try another puzzle.

If you still want to see it, you can [get your puzzle input](12/input).

You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+%22Hot+Springs%22+%2D+Day+12+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F12&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle.
Loading

0 comments on commit cd26f93

Please sign in to comment.