Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 82d160c

Browse filesBrowse files
committed
Day 04 Part 02
1 parent affcd4e commit 82d160c
Copy full SHA for 82d160c

File tree

Expand file treeCollapse file tree

2 files changed

+33
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+33
-0
lines changed

‎day04/main.go

Copy file name to clipboardExpand all lines: day04/main.go
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,36 @@ func partOne(input string) (fullyContained int) {
3636
return
3737
}
3838

39+
func partTwo(input string) (fullyContained int) {
40+
lines := strings.Split(strings.TrimRight(input, "\n"), "\n")
41+
42+
for _, line := range lines {
43+
left, right, _ := strings.Cut(line, ",")
44+
leftFromStr, leftToStr, _ := strings.Cut(left, "-")
45+
rightFromStr, rightToStr, _ := strings.Cut(right, "-")
46+
47+
// fmt.Printf("%s %s %s %s\n", leftFromStr, leftToStr, rightFromStr, rightToStr)
48+
49+
leftFrom, _ := strconv.Atoi(leftFromStr)
50+
leftTo, _ := strconv.Atoi(leftToStr)
51+
rightFrom, _ := strconv.Atoi(rightFromStr)
52+
rightTo, _ := strconv.Atoi(rightToStr)
53+
54+
// fmt.Printf("%d %d %d %d\n", leftFrom, leftTo, rightFrom, rightTo)
55+
56+
if (leftTo >= rightFrom && leftFrom <= rightTo) ||
57+
(rightTo >= leftFrom && rightFrom <= leftTo) {
58+
fullyContained += 1
59+
}
60+
}
61+
62+
return
63+
}
64+
3965
func main() {
4066
score1 := partOne(input2)
4167
fmt.Printf("%d\n", score1)
68+
69+
score2 := partTwo(input2)
70+
fmt.Printf("%d\n", score2)
4271
}

‎day04/main_test.go

Copy file name to clipboardExpand all lines: day04/main_test.go
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ var input1 string
1313
func TestOne(t *testing.T) {
1414
assert.Equal(t, 2, partOne(input1), "they should be equal")
1515
}
16+
17+
func TestTwo(t *testing.T) {
18+
assert.Equal(t, 4, partTwo(input1), "they should be equal")
19+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.