2025 Day 5 Complete!

This commit is contained in:
2025-12-05 07:48:39 -06:00
parent 2f05062091
commit 9e23f49a94
4 changed files with 1419 additions and 0 deletions

1183
2025/day05/input Normal file

File diff suppressed because it is too large Load Diff

111
2025/day05/main.go Normal file
View File

@@ -0,0 +1,111 @@
package main
import (
"fmt"
"os"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
part1(inp)
fmt.Println()
part2(inp)
}
func part1(inp []string) {
var ret int
fresh, ing := parseDB(inp)
for i := range ing {
for j := range fresh {
if fresh[j].Contains(ing[i]) {
ret++
break
}
}
}
fmt.Println("# Part 1")
fmt.Println(ret)
}
func part2(inp []string) {
fresh, _ := parseDB(inp)
for {
var toDel []*Range
for i := 0; i < len(fresh)-1; i++ {
for j := i + 1; j < len(fresh); j++ {
if fresh[i].Overlaps(fresh[j]) {
fresh[i].Merge(fresh[j])
toDel = append(toDel, fresh[j])
}
}
if len(toDel) > 0 {
break
}
}
if len(toDel) == 0 {
break
}
for i := range toDel {
for j := 0; j < len(fresh); j++ {
if fresh[j] == toDel[i] {
fresh = append(fresh[:j], fresh[j+1:]...)
break
}
}
}
}
// At this point, all ranges in 'fresh' should be non-overlapping
var ret int
for i := range fresh {
ret += fresh[i].Size()
}
fmt.Println("# Part 2")
fmt.Println(ret)
}
type Range struct {
beg, end int
}
func NewRange(inp string) *Range {
r := Range{}
fmt.Sscanf(inp, "%d-%d", &r.beg, &r.end)
return &r
}
func (r *Range) Contains(i int) bool { return r.beg <= i && i <= r.end }
func (r *Range) Overlaps(r2 *Range) bool {
return r.Contains(r2.beg) || r.Contains(r2.end) || r2.Contains(r.beg) || r2.Contains(r.end)
}
func (r *Range) Merge(r2 *Range) {
if !r.Overlaps(r2) {
fmt.Println("Panic! Tried to merge non-overlapping ranges")
os.Exit(1)
}
r.beg = h.Min(r.beg, r2.beg)
r.end = h.Max(r.end, r2.end)
}
func (r *Range) Size() int { return (r.end - r.beg) + 1 }
func (r Range) String() string { return fmt.Sprintf("%d to %d", r.beg, r.end) }
func parseDB(inp []string) ([]*Range, []int) {
var retRng []*Range
var retInt []int
inRanges := true
for i := range inp {
if inp[i] == "" {
inRanges = false
continue
}
if inRanges {
retRng = append(retRng, NewRange(inp[i]))
continue
}
var ri int
fmt.Sscanf(inp[i], "%d", &ri)
retInt = append(retInt, ri)
}
return retRng, retInt
}

114
2025/day05/problem Normal file
View File

@@ -0,0 +1,114 @@
Advent of Code
br0xen [7](AoC++) 10*
--- Day 5: Cafeteria ---
As the forklifts break through the wall, the Elves are delighted to
discover that there was a cafeteria on the other side after all.
You can hear a commotion coming from the kitchen. "At this rate, we won't
have any time left to put the wreaths up in the dining hall!" Resolute in
your quest, you investigate.
"If only we hadn't switched to the new inventory management system right
before Christmas!" another Elf exclaims. You ask what's going on.
The Elves in the kitchen explain the situation: because of their
complicated new inventory management system, they can't figure out which
of their ingredients are fresh and which are spoiled. When you ask how it
works, they give you a copy of their database (your puzzle input).
The database operates on ingredient IDs. It consists of a list of fresh
ingredient ID ranges, a blank line, and a list of available ingredient
IDs. For example:
3-5
10-14
16-20
12-18
1
5
8
11
17
32
The fresh ID ranges are inclusive: the range 3-5 means that ingredient IDs
3, 4, and 5 are all fresh. The ranges can also overlap; an ingredient ID
is fresh if it is in any range.
The Elves are trying to determine which of the available ingredient IDs
are fresh. In this example, this is done as follows:
 Ingredient ID 1 is spoiled because it does not fall into any range.
 Ingredient ID 5 is fresh because it falls into range 3-5.
 Ingredient ID 8 is spoiled.
 Ingredient ID 11 is fresh because it falls into range 10-14.
 Ingredient ID 17 is fresh because it falls into range 16-20 as well as
range 12-18.
 Ingredient ID 32 is spoiled.
So, in this example, 3 of the available ingredient IDs are fresh.
Process the database file from the new inventory management system. How
many of the available ingredient IDs are fresh?
Your puzzle answer was 848.
--- Part Two ---
The Elves start bringing their spoiled inventory to the trash chute at the
back of the kitchen.
So that they can stop bugging you when they get new inventory, the Elves
would like to know all of the IDs that the fresh ingredient ID ranges
consider to be fresh. An ingredient ID is still considered fresh if it is
in any range.
Now, the second section of the database (the available ingredient IDs) is
irrelevant. Here are the fresh ingredient ID ranges from the above
example:
3-5
10-14
16-20
12-18
The ingredient IDs that these ranges consider to be fresh are 3, 4, 5, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, and 20. So, in this example, the fresh
ingredient ID ranges consider a total of 14 ingredient IDs to be fresh.
Process the database file again. How many ingredient IDs are considered to
be fresh according to the fresh ingredient ID ranges?
Your puzzle answer was 334714395325710.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should [16]return to your Advent calendar and try
another puzzle.
If you still want to see it, you can [17]get your puzzle input.
References
Visible links
1. https://adventofcode.com/
2. https://adventofcode.com/2025/about
3. https://adventofcode.com/2025/events
4. https://adventofcode.com/2025/shop
5. https://adventofcode.com/2025/settings
6. https://adventofcode.com/2025/auth/logout
7. Advent of Code Supporter
https://adventofcode.com/2025/support
8. https://adventofcode.com/2025
9. https://adventofcode.com/2025
10. https://adventofcode.com/2025/support
11. https://adventofcode.com/2025/sponsors
12. https://adventofcode.com/2025/leaderboard/private
13. https://adventofcode.com/2025/stats
14. https://adventofcode.com/2025/sponsors
15. https://adventofcode.com/2025/sponsors/redirect?url=https%3A%2F%2Fwww%2Eonechronos%2Ecom
16. https://adventofcode.com/2025
17. https://adventofcode.com/2025/day/5/input

11
2025/day05/testinput Normal file
View File

@@ -0,0 +1,11 @@
3-5
10-14
16-20
12-18
1
5
8
11
17
32