Skip to content

Commit 721ab48

Browse files
committed
2025 day 5
1 parent a7c5d3f commit 721ab48

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

2025/5/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
input:
2+
http "https://adventofcode.com/2025/day/5/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go common.go
6+
7+
main2:
8+
go build -o main2 main2.go common.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input
20+

2025/5/common.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"os"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type Range struct{ Start, End int }
11+
12+
func parseInput() ([]Range, []int) {
13+
scanner := bufio.NewScanner(os.Stdin)
14+
15+
// Scan ranges
16+
var ranges []Range
17+
for scanner.Scan() {
18+
line := scanner.Text()
19+
20+
if line == "" {
21+
break
22+
}
23+
24+
ff := strings.Split(line, "-")
25+
start, _ := strconv.Atoi(ff[0])
26+
end, _ := strconv.Atoi(ff[1])
27+
ranges = append(ranges, Range{start, end})
28+
}
29+
30+
// Scan IDs
31+
var ids []int
32+
for scanner.Scan() {
33+
line := scanner.Text()
34+
id, _ := strconv.Atoi(line)
35+
ids = append(ids, id)
36+
}
37+
38+
return ranges, ids
39+
}

2025/5/main1.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
ranges, ids := parseInput()
7+
8+
count := 0
9+
for _, id := range ids {
10+
if fresh(ranges, id) {
11+
count++
12+
}
13+
}
14+
15+
fmt.Println(count)
16+
}
17+
18+
func fresh(ranges []Range, id int) bool {
19+
for _, r := range ranges {
20+
if id >= r.Start && id <= r.End {
21+
return true
22+
}
23+
}
24+
return false
25+
}

2025/5/main2.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
7+
8+
func main() {
9+
ranges, _ := parseInput()
10+
11+
sort.Slice(ranges, func(i, j int) bool {
12+
if ranges[i].Start == ranges[j].Start {
13+
// when they have the same start, prefer bigger ranges on the left,
14+
// so `contains` will eat them at the beginning of the next loop
15+
return ranges[i].End > ranges[j].End
16+
}
17+
return ranges[i].Start < ranges[j].Start
18+
})
19+
20+
// index of the Work in Progress range
21+
wipIdx := 0
22+
23+
for i := 1; i < len(ranges); i++ {
24+
wipRange := ranges[wipIdx]
25+
26+
// if the WIP contains the current, we ignore the current
27+
if wipRange.contains(ranges[i]) {
28+
continue
29+
}
30+
31+
// if they touch, we merge them inside the WIP
32+
if wipRange.overlaps(ranges[i]) {
33+
ranges[wipIdx] = wipRange.append(ranges[i])
34+
continue
35+
}
36+
37+
// disjoint ranges
38+
// freeze the current WIP by focusing on the next one
39+
// that is the current range
40+
wipIdx++
41+
ranges[wipIdx] = ranges[i]
42+
}
43+
44+
ranges = ranges[:wipIdx+1]
45+
46+
sum := 0
47+
for _, r := range ranges {
48+
sum += r.Len()
49+
}
50+
51+
fmt.Println(sum)
52+
}
53+
54+
func (r Range) overlaps(r2 Range) bool {
55+
return r.Start <= r2.Start && r.End >= r2.Start
56+
}
57+
58+
func (r Range) contains(r2 Range) bool {
59+
return r.Start <= r2.Start && r.End >= r2.End
60+
}
61+
62+
func (r Range) append(r2 Range) Range {
63+
return Range{r.Start, r2.End}
64+
}
65+
66+
func (r Range) Len() int {
67+
return r.End - r.Start + 1
68+
}

0 commit comments

Comments
 (0)