2023 Day 12 Complete!
This commit is contained in:
parent
77d53011a1
commit
ea9a19d034
1000
2023/day12/input
Normal file
1000
2023/day12/input
Normal file
File diff suppressed because it is too large
Load Diff
109
2023/day12/main.go
Normal file
109
2023/day12/main.go
Normal file
@ -0,0 +1,109 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
h "git.bullercodeworks.com/brian/adventofcode/helpers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
inp := h.StdinToStringSlice()
|
||||
part1(inp)
|
||||
fmt.Println()
|
||||
part2(inp)
|
||||
}
|
||||
|
||||
func part1(input []string) {
|
||||
var records []string
|
||||
var groups [][]int
|
||||
for _, ln := range input {
|
||||
pts := strings.Fields(ln)
|
||||
records = append(records, pts[0])
|
||||
var grp []int
|
||||
for _, n := range strings.Split(pts[1], ",") {
|
||||
grp = append(grp, h.Atoi(n))
|
||||
}
|
||||
groups = append(groups, grp)
|
||||
}
|
||||
var result int
|
||||
for i := range records {
|
||||
// Initialize cache for this row
|
||||
cache := make(map[string]int)
|
||||
|
||||
// And solve the row
|
||||
result += solveRow(0, 0, records[i], groups[i], cache)
|
||||
}
|
||||
|
||||
fmt.Println("# Part 1")
|
||||
fmt.Println(result)
|
||||
}
|
||||
|
||||
func part2(input []string) {
|
||||
var records []string
|
||||
var groups [][]int
|
||||
for _, ln := range input {
|
||||
pts := strings.Fields(ln)
|
||||
// Unfold the paper
|
||||
pts[0] = fmt.Sprintf("%s?%s?%s?%s?%s", pts[0], pts[0], pts[0], pts[0], pts[0])
|
||||
pts[1] = fmt.Sprintf("%s,%s,%s,%s,%s", pts[1], pts[1], pts[1], pts[1], pts[1])
|
||||
records = append(records, pts[0])
|
||||
var grp []int
|
||||
for _, n := range strings.Split(pts[1], ",") {
|
||||
grp = append(grp, h.Atoi(n))
|
||||
}
|
||||
groups = append(groups, grp)
|
||||
}
|
||||
|
||||
var result int
|
||||
for i := range records {
|
||||
// Initialize cache for this row
|
||||
cache := make(map[string]int)
|
||||
|
||||
// And solve the row
|
||||
result += solveRow(0, 0, records[i], groups[i], cache)
|
||||
}
|
||||
|
||||
fmt.Println("# Part 2")
|
||||
fmt.Println(result)
|
||||
}
|
||||
|
||||
func ck(i, j int) string { return fmt.Sprintf("%d_%d", i, j) }
|
||||
|
||||
func solveRow(rbIdx, groupIdx int, record string, group []int, cache map[string]int) int {
|
||||
if rbIdx >= len(record) {
|
||||
if groupIdx < len(group) {
|
||||
return 0
|
||||
}
|
||||
return 1
|
||||
}
|
||||
if v, ok := cache[ck(rbIdx, groupIdx)]; ok {
|
||||
return v
|
||||
}
|
||||
res := 0
|
||||
if record[rbIdx] == '.' {
|
||||
res = solveRow(rbIdx+1, groupIdx, record, group, cache)
|
||||
} else {
|
||||
if record[rbIdx] == '?' {
|
||||
res += solveRow(rbIdx+1, groupIdx, record, group, cache)
|
||||
}
|
||||
if groupIdx < len(group) {
|
||||
count := 0
|
||||
for k := rbIdx; k < len(record); k++ {
|
||||
if count > group[groupIdx] || record[k] == '.' || count == group[groupIdx] && record[k] == '?' {
|
||||
break
|
||||
}
|
||||
count += 1
|
||||
}
|
||||
if count == group[groupIdx] {
|
||||
if rbIdx+count < len(record) && record[rbIdx+count] != '#' {
|
||||
res += solveRow(rbIdx+count+1, groupIdx+1, record, group, cache)
|
||||
} else {
|
||||
res += solveRow(rbIdx+count, groupIdx+1, record, group, cache)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
cache[ck(rbIdx, groupIdx)] = res
|
||||
return res
|
||||
}
|
222
2023/day12/problem
Normal file
222
2023/day12/problem
Normal file
@ -0,0 +1,222 @@
|
||||
[1]Advent of Code
|
||||
|
||||
• [2][About]
|
||||
• [3][Events]
|
||||
• [4][Shop]
|
||||
• [5][Settings]
|
||||
• [6][Log Out]
|
||||
|
||||
br0xen [7](AoC++) 24*
|
||||
|
||||
sub y{[8]2023}
|
||||
|
||||
• [9][Calendar]
|
||||
• [10][AoC++]
|
||||
• [11][Sponsors]
|
||||
• [12][Leaderboard]
|
||||
• [13][Stats]
|
||||
|
||||
Our [14]sponsors help make Advent of Code possible:
|
||||
[15]Tailscale - Secure remote access, powered by WireGuard. Replace your
|
||||
VPN :)
|
||||
|
||||
--- 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 [16]researcher 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 [17]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 7716.
|
||||
|
||||
--- 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 18716325559999.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, you should [18]return to your Advent calendar and try
|
||||
another puzzle.
|
||||
|
||||
If you still want to see it, you can [19]get your puzzle input.
|
||||
|
||||
You can also [Shareon [20]Twitter [21]Mastodon] this puzzle.
|
||||
|
||||
References
|
||||
|
||||
Visible links
|
||||
1. https://adventofcode.com/
|
||||
2. https://adventofcode.com/2023/about
|
||||
3. https://adventofcode.com/2023/events
|
||||
4. https://teespring.com/stores/advent-of-code
|
||||
5. https://adventofcode.com/2023/settings
|
||||
6. https://adventofcode.com/2023/auth/logout
|
||||
7. Advent of Code Supporter
|
||||
https://adventofcode.com/2023/support
|
||||
8. https://adventofcode.com/2023
|
||||
9. https://adventofcode.com/2023
|
||||
10. https://adventofcode.com/2023/support
|
||||
11. https://adventofcode.com/2023/sponsors
|
||||
12. https://adventofcode.com/2023/leaderboard
|
||||
13. https://adventofcode.com/2023/stats
|
||||
14. https://adventofcode.com/2023/sponsors
|
||||
15. https://tailscale.com/advent
|
||||
16. https://adventofcode.com/2023/day/11
|
||||
17. https://en.wikipedia.org/wiki/Onsen
|
||||
18. https://adventofcode.com/2023
|
||||
19. https://adventofcode.com/2023/day/12/input
|
||||
20. 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
|
||||
21. javascript:void(0);
|
6
2023/day12/testinput
Normal file
6
2023/day12/testinput
Normal file
@ -0,0 +1,6 @@
|
||||
???.### 1,1,3
|
||||
.??..??...?##. 1,1,3
|
||||
?#?#?#?#?#?#?#? 1,3,1,6
|
||||
????.#...#... 4,1,1
|
||||
????.######..#####. 1,6,5
|
||||
?###???????? 3,2,1
|
Loading…
Reference in New Issue
Block a user