Skip to content

Commit 783c7f6

Browse files
author
Robb Kidd
committed
part 2
1 parent fede8e9 commit 783c7f6

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

2022/go/day04/main.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,39 @@ func fullyCovers(pair Pair) bool {
3737
return false
3838
}
3939

40+
func p2(lines []string) int {
41+
var overlapped int
42+
for _, line := range lines {
43+
pair := lineToPair(line)
44+
45+
if overlaps(pair) {
46+
overlapped++
47+
continue
48+
}
49+
}
50+
return overlapped
51+
}
52+
53+
func overlaps(pair Pair) bool {
54+
if fullyCovers(pair) {
55+
return true
56+
}
57+
58+
if pair.left.start >= pair.right.start && pair.left.start <= pair.right.stop {
59+
return true
60+
}
61+
if pair.right.start >= pair.left.start && pair.right.stop <= pair.left.stop {
62+
return true
63+
}
64+
if pair.left.stop >= pair.right.start && pair.left.stop <= pair.right.stop {
65+
return true
66+
}
67+
if pair.right.stop >= pair.left.start && pair.right.stop <= pair.left.stop {
68+
return true
69+
}
70+
return false
71+
}
72+
4073
func lineToPair(line string) Pair {
4174
p := strings.Split(line, ",")
4275
left := strings.Split(p[0], "-")
@@ -59,10 +92,6 @@ func lineToPair(line string) Pair {
5992
}
6093
}
6194

62-
func p2(input []string) int {
63-
return 0
64-
}
65-
6695
type Pair struct {
6796
left Assignment
6897
right Assignment

2022/go/day04/main_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@ func TestExamplePart1(t *testing.T) {
1212
p1(parseInput(example)))
1313
}
1414

15-
// func TestExamplePart2(t *testing.T) {
16-
// assert.Equal(t,
17-
// 'heyo',
18-
// p2(parseInput(example)))
19-
// }
15+
func TestExamplePart2(t *testing.T) {
16+
assert.Equal(t,
17+
4,
18+
p2(parseInput(example)))
19+
}
2020

2121
const (
2222
example = `2-4,6-8
2323
2-3,4-5
2424
5-7,7-9
2525
2-8,3-7
2626
6-6,4-6
27-
2-6,4-8`
27+
2-6,4-8
28+
`
2829
)

0 commit comments

Comments
 (0)