Skip to content

Commit 05f28ce

Browse files
author
Jan Steinke
committed
2022/4
1 parent 6d7ab13 commit 05f28ce

File tree

3 files changed

+144
-1
lines changed

3 files changed

+144
-1
lines changed

2022/4/main.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
type (
11+
section int
12+
shift [2]int
13+
shiftPair [2]shift
14+
)
15+
16+
func main() {
17+
content, err := ioutil.ReadFile("2022/4/input.txt")
18+
if err != nil {
19+
fmt.Print(err)
20+
}
21+
input := parse(content)
22+
fmt.Println(countOverlap(input))
23+
}
24+
25+
func parse(b []byte) (sps []shiftPair) {
26+
lines := strings.Split(strings.TrimSpace(string(b)), "\n")
27+
for _, l := range lines {
28+
shifts := strings.Split(l, ",")
29+
firstShift := shiftFromString(shifts[0])
30+
secondShift := shiftFromString(shifts[1])
31+
sps = append(sps, shiftPair{firstShift, secondShift})
32+
}
33+
return
34+
}
35+
36+
func shiftFromString(s string) shift {
37+
sections := strings.Split(strings.TrimSpace(s), "-")
38+
start, err := strconv.Atoi(sections[0])
39+
if err != nil {
40+
panic(err)
41+
}
42+
end, err := strconv.Atoi(sections[1])
43+
if err != nil {
44+
panic(err)
45+
}
46+
return shift{start, end}
47+
}
48+
49+
func countContaining(s []shiftPair) (c int) {
50+
for _, sp := range s {
51+
start1 := sp[0][0]
52+
end1 := sp[0][1]
53+
start2 := sp[1][0]
54+
end2 := sp[1][1]
55+
if start1 <= start2 && end1 >= end2 {
56+
c++
57+
} else if start1 >= start2 && end1 <= end2 {
58+
c++
59+
}
60+
}
61+
return
62+
}
63+
64+
func countOverlap(s []shiftPair) (c int) {
65+
for _, sp := range s {
66+
start1 := sp[0][0]
67+
end1 := sp[0][1]
68+
start2 := sp[1][0]
69+
end2 := sp[1][1]
70+
if start1 <= end2 && end1 >= start2 {
71+
c++
72+
}
73+
}
74+
return
75+
}

2022/4/main_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestParse(t *testing.T) {
9+
input := []byte(`2-4,6-8
10+
2-3,4-5
11+
5-7,7-9
12+
2-8,3-7
13+
6-6,4-6
14+
2-6,4-8`)
15+
16+
got := parse(input)
17+
18+
want := []shiftPair{
19+
{{2, 4}, {6, 8}},
20+
{{2, 3}, {4, 5}},
21+
{{5, 7}, {7, 9}},
22+
{{2, 8}, {3, 7}},
23+
{{6, 6}, {4, 6}},
24+
{{2, 6}, {4, 8}},
25+
}
26+
27+
if !reflect.DeepEqual(got, want) {
28+
t.Errorf("got=%v, want=%v", got, want)
29+
}
30+
}
31+
32+
func TestCountContaining(t *testing.T) {
33+
input := []shiftPair{
34+
{{2, 4}, {6, 8}},
35+
{{2, 3}, {4, 5}},
36+
{{5, 7}, {7, 9}},
37+
{{2, 8}, {3, 7}},
38+
{{6, 6}, {4, 6}},
39+
{{2, 6}, {4, 8}},
40+
}
41+
42+
got := countContaining(input)
43+
44+
want := 2
45+
46+
if got != want {
47+
t.Errorf("got=%v, want=%v", got, want)
48+
}
49+
}
50+
51+
func TestCountOverlap(t *testing.T) {
52+
input := []shiftPair{
53+
{{2, 4}, {6, 8}},
54+
{{2, 3}, {4, 5}},
55+
{{5, 7}, {7, 9}},
56+
{{2, 8}, {3, 7}},
57+
{{6, 6}, {4, 6}},
58+
{{2, 6}, {4, 8}},
59+
}
60+
61+
got := countOverlap(input)
62+
63+
want := 4
64+
65+
if got != want {
66+
t.Errorf("got=%v, want=%v", got, want)
67+
}
68+
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# adventofcode
22

3-
https://adventofcode.com/
3+
<https://adventofcode.com/>

0 commit comments

Comments
 (0)