Working for small, not for large? I am sad.
This commit is contained in:
83
2023/12/README.md
Executable file
83
2023/12/README.md
Executable file
@@ -0,0 +1,83 @@
|
|||||||
|
## \-\-\- 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?_
|
||||||
118
2023/12/code.go
Normal file
118
2023/12/code.go
Normal file
@@ -0,0 +1,118 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/jpillora/puzzler/harness/aoc"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
aoc.Harness(run)
|
||||||
|
}
|
||||||
|
|
||||||
|
// on code change, run will be executed 4 times:
|
||||||
|
// 1. with: false (part1), and example input
|
||||||
|
// 2. with: true (part2), and example input
|
||||||
|
// 3. with: false (part1), and user input
|
||||||
|
// 4. with: true (part2), and user input
|
||||||
|
// the return value of each run is printed to stdout
|
||||||
|
func run(part2 bool, input string) any {
|
||||||
|
// when you're ready to do part 2, remove this "not implemented" block
|
||||||
|
if part2 {
|
||||||
|
return "not implemented"
|
||||||
|
}
|
||||||
|
// solve part 1 here
|
||||||
|
|
||||||
|
sum := 0
|
||||||
|
for _, l := range strings.Split(input, "\n") {
|
||||||
|
if l == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
spl := strings.Split(l, " ")
|
||||||
|
combo := make([]int, 0)
|
||||||
|
for _, c := range strings.Split(spl[1], ",") {
|
||||||
|
ci, _ := strconv.Atoi(c)
|
||||||
|
combo = append(combo, ci)
|
||||||
|
}
|
||||||
|
springs := make([]rune, 0)
|
||||||
|
for _, r := range strings.Split(spl[0], "") {
|
||||||
|
springs = append(springs, rune(r[0]))
|
||||||
|
}
|
||||||
|
p := iteratePossibilities(spl[0]+".", combo, 0)
|
||||||
|
|
||||||
|
sum += p
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
func iteratePossibilities(springs string, combo []int, depth int) int {
|
||||||
|
if len(combo) == 0 {
|
||||||
|
panic("Can't try find possibilities with empty combo list")
|
||||||
|
}
|
||||||
|
sprlen := combo[0] + 1
|
||||||
|
|
||||||
|
print(fmt.Sprintf("%v:", springs), depth)
|
||||||
|
if len(springs) < sprlen {
|
||||||
|
// Can't fit, bad match
|
||||||
|
print(fmt.Sprintf("Cannot fit combo len %v in remaining springs %v", sprlen, springs), depth)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
p := 0
|
||||||
|
for i := 0; i <= len(springs)-sprlen; i++ {
|
||||||
|
match := true
|
||||||
|
forcedMatch := true
|
||||||
|
checking := springs[i : i+sprlen]
|
||||||
|
print(fmt.Sprintf("Testing %v in %v", checking, springs), depth)
|
||||||
|
|
||||||
|
for c, r := range checking {
|
||||||
|
if rune(r) == '#' {
|
||||||
|
if c == sprlen-1 {
|
||||||
|
print("Failed match: ends with damaged (would be non-contiguous)", depth)
|
||||||
|
match = false
|
||||||
|
}
|
||||||
|
} else if rune(r) == '.' {
|
||||||
|
if c != sprlen-1 {
|
||||||
|
print("Failed match: contained undamaged", depth)
|
||||||
|
match = false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if c != sprlen-1 {
|
||||||
|
print(fmt.Sprintf("NOT forced match at %v", c), depth)
|
||||||
|
forcedMatch = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if match {
|
||||||
|
print(fmt.Sprintf("Found %v+1 match (forced=%v) in %v at %v-%v (%v) (remaining: %v)", combo[0], forcedMatch, springs, i, i+sprlen-1, springs[i:i+sprlen], combo[1:]), depth)
|
||||||
|
|
||||||
|
if len(combo) > 1 {
|
||||||
|
p += iteratePossibilities(springs[i+sprlen:], combo[1:], depth+1)
|
||||||
|
} else {
|
||||||
|
p += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if forcedMatch {
|
||||||
|
print("Forced match, not trying any other combos for this", depth)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
|
||||||
|
func print(pr string, depth int) {
|
||||||
|
print := false
|
||||||
|
if print {
|
||||||
|
for i := 0; i < depth; i++ {
|
||||||
|
fmt.Printf("> ")
|
||||||
|
}
|
||||||
|
fmt.Println(pr)
|
||||||
|
}
|
||||||
|
}
|
||||||
6
2023/12/input-example.txt
Executable file
6
2023/12/input-example.txt
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
???.### 1,1,3
|
||||||
|
.??..??...?##. 1,1,3
|
||||||
|
?#?#?#?#?#?#?#? 1,3,1,6
|
||||||
|
????.#...#... 4,1,1
|
||||||
|
????.######..#####. 1,6,5
|
||||||
|
?###???????? 3,2,1
|
||||||
Reference in New Issue
Block a user