diff --git a/2022/day16/main.go b/2022/day16/main.go new file mode 100644 index 0000000..19302af --- /dev/null +++ b/2022/day16/main.go @@ -0,0 +1,111 @@ +package main + +import ( + "fmt" + "math" + "strings" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToStringSlice() + part1(inp) +} + +func part1(inp []string) { + volcano := NewVolcano(inp) + timeLeft := 30 + currRoom := volcano.FindRoom("AA") + _, _ = timeLeft, currRoom + fmt.Println("Steps from AA -> JJ:", volcano.CountSteps("AA", "JJ")) +} + +type Volcano struct { + rooms []*Room +} + +func NewVolcano(inp []string) *Volcano { + v := &Volcano{} + for i := range inp { + v.rooms = append(v.rooms, StringToRoom(inp[i])) + } + for i := range v.rooms { + for _, valve := range v.rooms[i].RawTunnels { + v.rooms[i].Tunnels = append(v.rooms[i].Tunnels, v.FindRoom(valve)) + } + } + return v +} + +func (v *Volcano) CountSteps(from string, to string) int { + start, end := v.FindRoom(from), v.FindRoom(to) + if start == nil || end == nil { + fmt.Println("Couldn't find requested rooms:", from, ":", start, " ; ", to, ":", end) + return math.MaxInt + } + return v.TrackStepCount(start, end, []*Room{}) +} + +func (v *Volcano) TrackStepCount(from *Room, to *Room, visited []*Room) int { + if from == to { + return 0 + } + minSteps := math.MaxInt + for _, t := range from.Tunnels { + fmt.Println("TrackStepCount:", from, "->", to, "::", t) + if !IsRoomIn(t, visited) { + fmt.Println(" Haven't Visited") + wrk := v.TrackStepCount(t, to, append(visited, t)) + 1 + minSteps = h.Min(minSteps, wrk) + } + } + return minSteps +} + +func (v *Volcano) FindRoom(valve string) *Room { + for _, r := range v.rooms { + if r.Valve == valve { + return r + } + } + return nil +} + +func IsRoomIn(room *Room, rooms []*Room) bool { + for i := range rooms { + if rooms[i] == room { + return true + } + } + return false +} + +type Room struct { + Valve string + FlowRate int + RawTunnels []string + Tunnels []*Room +} + +func StringToRoom(s string) *Room { + room := &Room{} + r := strings.NewReader(s) + fmt.Fscanf(r, "Valve %s has flow rate=%d; tunnels lead to valve", &room.Valve, &room.FlowRate) + readTunnels := strings.Split(s, " valve")[1] + if readTunnels[0] == ' ' { + readTunnels = readTunnels[1:] + } else { + readTunnels = readTunnels[2:] + } + room.RawTunnels = strings.Split(readTunnels, ", ") + return room +} + +func (r Room) String() string { + return r.Valve +} + +func (r Room) FullString() string { + return fmt.Sprintf("Valve %s has flow rate=%d; tunnels lead to valves %v", r.Valve, r.FlowRate, r.RawTunnels) +} diff --git a/2022/day16/problem b/2022/day16/problem new file mode 100644 index 0000000..7e726fe --- /dev/null +++ b/2022/day16/problem @@ -0,0 +1,214 @@ + Advent of Code + + • [About] + • [Events] + • [Shop] + • [Settings] + • [Log Out] + + br0xen (AoC++) 29* + +       /^2022$/ + + • [Calendar] + • [AoC++] + • [Sponsors] + • [Leaderboard] + • [Stats] + + Our sponsors help make Advent of Code possible: + Sentry.io - More than 3.5 million developers across 85,000 organizations ship better software, faster, with Sentry. What + are you waiting for? + +--- Day 16: Proboscidea Volcanium --- + + The sensors have led you to the origin of the distress signal: yet another handheld device, just like the one the Elves + gave you. However, you don't see any Elves around; instead, the device is surrounded by elephants! They must have gotten + lost in these tunnels, and one of the elephants apparently figured out how to turn on the distress signal. + + The ground rumbles again, much stronger this time. What kind of cave is this, exactly? You scan the cave with your + handheld device; it reports mostly igneous rock, some ash, pockets of pressurized gas, magma... this isn't just a cave, + it's a volcano! + + You need to get the elephants out of here, quickly. Your device estimates that you have 30 minutes before the volcano + erupts, so you don't have time to go back out the way you came in. + + You scan the cave for other options and discover a network of pipes and pressure-release valves. You aren't sure how + such a system got into a volcano, but you don't have time to complain; your device produces a report (your puzzle input) + of each valve's flow rate if it were opened (in pressure per minute) and the tunnels you could use to move between the + valves. + + There's even a valve in the room you and the elephants are currently standing in labeled AA. You estimate it will take + you one minute to open a single valve and one minute to follow any tunnel from one valve to another. What is the most + pressure you could release? + + For example, suppose you had the following scan output: + + Valve AA has flow rate=0; tunnels lead to valves DD, II, BB + Valve BB has flow rate=13; tunnels lead to valves CC, AA + Valve CC has flow rate=2; tunnels lead to valves DD, BB + Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE + Valve EE has flow rate=3; tunnels lead to valves FF, DD + Valve FF has flow rate=0; tunnels lead to valves EE, GG + Valve GG has flow rate=0; tunnels lead to valves FF, HH + Valve HH has flow rate=22; tunnel leads to valve GG + Valve II has flow rate=0; tunnels lead to valves AA, JJ + Valve JJ has flow rate=21; tunnel leads to valve II + + All of the valves begin closed. You start at valve AA, but it must be damaged or jammed or something: its flow rate is + 0, so there's no point in opening it. However, you could spend one minute moving to valve BB and another minute opening + it; doing so would release pressure during the remaining 28 minutes at a flow rate of 13, a total eventual pressure + release of 28 * 13 = 364. Then, you could spend your third minute moving to valve CC and your fourth minute opening it, + providing an additional 26 minutes of eventual pressure release at a flow rate of 2, or 52 total pressure released by + valve CC. + + Making your way through the tunnels like this, you could probably open many or all of the valves by the time 30 minutes + have elapsed. However, you need to release as much pressure as possible, so you'll need to be methodical. Instead, + consider this approach: + + == Minute 1 == + No valves are open. + You move to valve DD. + + == Minute 2 == + No valves are open. + You open valve DD. + + == Minute 3 == + Valve DD is open, releasing 20 pressure. + You move to valve CC. + + == Minute 4 == + Valve DD is open, releasing 20 pressure. + You move to valve BB. + + == Minute 5 == + Valve DD is open, releasing 20 pressure. + You open valve BB. + + == Minute 6 == + Valves BB and DD are open, releasing 33 pressure. + You move to valve AA. + + == Minute 7 == + Valves BB and DD are open, releasing 33 pressure. + You move to valve II. + + == Minute 8 == + Valves BB and DD are open, releasing 33 pressure. + You move to valve JJ. + + == Minute 9 == + Valves BB and DD are open, releasing 33 pressure. + You open valve JJ. + + == Minute 10 == + Valves BB, DD, and JJ are open, releasing 54 pressure. + You move to valve II. + + == Minute 11 == + Valves BB, DD, and JJ are open, releasing 54 pressure. + You move to valve AA. + + == Minute 12 == + Valves BB, DD, and JJ are open, releasing 54 pressure. + You move to valve DD. + + == Minute 13 == + Valves BB, DD, and JJ are open, releasing 54 pressure. + You move to valve EE. + + == Minute 14 == + Valves BB, DD, and JJ are open, releasing 54 pressure. + You move to valve FF. + + == Minute 15 == + Valves BB, DD, and JJ are open, releasing 54 pressure. + You move to valve GG. + + == Minute 16 == + Valves BB, DD, and JJ are open, releasing 54 pressure. + You move to valve HH. + + == Minute 17 == + Valves BB, DD, and JJ are open, releasing 54 pressure. + You open valve HH. + + == Minute 18 == + Valves BB, DD, HH, and JJ are open, releasing 76 pressure. + You move to valve GG. + + == Minute 19 == + Valves BB, DD, HH, and JJ are open, releasing 76 pressure. + You move to valve FF. + + == Minute 20 == + Valves BB, DD, HH, and JJ are open, releasing 76 pressure. + You move to valve EE. + + == Minute 21 == + Valves BB, DD, HH, and JJ are open, releasing 76 pressure. + You open valve EE. + + == Minute 22 == + Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure. + You move to valve DD. + + == Minute 23 == + Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure. + You move to valve CC. + + == Minute 24 == + Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure. + You open valve CC. + + == Minute 25 == + Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + + == Minute 26 == + Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + + == Minute 27 == + Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + + == Minute 28 == + Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + + == Minute 29 == + Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + + == Minute 30 == + Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. + + This approach lets you release the most pressure possible in 30 minutes with this valve layout, 1651. + + Work out the steps to release the most pressure in 30 minutes. What is the most pressure you can release? + + To begin, get your puzzle input. + + Answer: _____________________ [ [Submit] ] + + You can also [Shareon Twitter Mastodon] this puzzle. + +References + + Visible links + . https://adventofcode.com/ + . https://adventofcode.com/2022/about + . https://adventofcode.com/2022/events + . https://teespring.com/stores/advent-of-code + . https://adventofcode.com/2022/settings + . https://adventofcode.com/2022/auth/logout + . Advent of Code Supporter + https://adventofcode.com/2022/support + . https://adventofcode.com/2022 + . https://adventofcode.com/2022 + . https://adventofcode.com/2022/support + . https://adventofcode.com/2022/sponsors + . https://adventofcode.com/2022/leaderboard + . https://adventofcode.com/2022/stats + . https://adventofcode.com/2022/sponsors + . https://sentry.io/ + . https://adventofcode.com/2022/day/16/input + . https://twitter.com/intent/tweet?text=%22Proboscidea+Volcanium%22+%2D+Day+16+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F16&related=ericwastl&hashtags=AdventOfCode + . javascript:void(0); diff --git a/2022/day16/testinput b/2022/day16/testinput new file mode 100644 index 0000000..9f30acc --- /dev/null +++ b/2022/day16/testinput @@ -0,0 +1,10 @@ +Valve AA has flow rate=0; tunnels lead to valves DD, II, BB +Valve BB has flow rate=13; tunnels lead to valves CC, AA +Valve CC has flow rate=2; tunnels lead to valves DD, BB +Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE +Valve EE has flow rate=3; tunnels lead to valves FF, DD +Valve FF has flow rate=0; tunnels lead to valves EE, GG +Valve GG has flow rate=0; tunnels lead to valves FF, HH +Valve HH has flow rate=22; tunnel leads to valve GG +Valve II has flow rate=0; tunnels lead to valves AA, JJ +Valve JJ has flow rate=21; tunnel leads to valve II