Skip to content

Commit c9a45d9

Browse files
committed
day 4 - part 2
1 parent 37d0993 commit c9a45d9

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

day04.sh

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#! /bin/bash
22

3+
echo 'test'
34
go build -o $(pwd)/bin/day04 ./day04/*.go && bin/day04 $(pwd)/day04/input_test.txt
45

6+
echo 'solution'
57
go build -o $(pwd)/bin/day04 ./day04/*.go && bin/day04 $(pwd)/day04/input.txt

day04/main.go

+26-7
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,40 @@ func main() {
3131
}
3232

3333
fmt.Printf("Part 1: %d\n", p1)
34+
35+
file.Seek(0, 0)
36+
p2, err := CountPartialOverlappingSectionAssignment(file)
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
fmt.Printf("Part 2: %d\n", p2)
41+
3442
}
3543

36-
func MapSections(i []string) (ax, ay, bx, by int) {
37-
ax, _ = strconv.Atoi(i[0])
38-
ay, _ = strconv.Atoi(i[1])
39-
bx, _ = strconv.Atoi(i[2])
40-
by, _ = strconv.Atoi(i[3])
44+
func MapSections(i []string) (x1, x2, y1, y2 int) {
45+
x1, _ = strconv.Atoi(i[0])
46+
x2, _ = strconv.Atoi(i[1])
47+
y1, _ = strconv.Atoi(i[2])
48+
y2, _ = strconv.Atoi(i[3])
4149
return
4250
}
4351

4452
func CountOverlappingSectionAssignment(input *os.File) (overlap int, err error) {
4553
err = IterateLines(input, func(s string) {
46-
var ax, ay, bx, by int = MapSections(re.FindAllString(s, -1))
54+
var x1, x2, y1, y2 int = MapSections(re.FindAllString(s, -1))
4755
// check if A contains B
48-
if ax <= bx && ay >= by || bx <= ax && by >= ay {
56+
if x1 <= y1 && x2 >= y2 || y1 <= x1 && y2 >= x2 {
57+
overlap++
58+
}
59+
})
60+
return
61+
}
62+
63+
func CountPartialOverlappingSectionAssignment(input *os.File) (overlap int, err error) {
64+
err = IterateLines(input, func(s string) {
65+
var x1, x2, y1, y2 int = MapSections(re.FindAllString(s, -1))
66+
// for any overlap
67+
if x1 <= y2 && y1 <= x2 {
4968
overlap++
5069
}
5170
})

0 commit comments

Comments
 (0)