re-organise repo
This commit is contained in:
59
2022/go/day04/day04.go
Normal file
59
2022/go/day04/day04.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package day04
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"strconv"
|
||||
// "fmt"
|
||||
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
|
||||
)
|
||||
|
||||
func Part1(input string) int {
|
||||
var count int = 0
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, line := range lines {
|
||||
pairs := strings.Split(line, ",")
|
||||
|
||||
pair1 := strings.Split(pairs[0], "-")
|
||||
pair2 := strings.Split(pairs[1], "-")
|
||||
|
||||
p1set := mapset.NewSet[int](Range(pair1[0], pair1[1])...)
|
||||
p2set := mapset.NewSet[int](Range(pair2[0], pair2[1])...)
|
||||
|
||||
if p1set.IsSubset(p2set) || p2set.IsSubset(p1set) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func Part2(input string) int {
|
||||
var count int = 0
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, line := range lines {
|
||||
pairs := strings.Split(line, ",")
|
||||
|
||||
pair1 := strings.Split(pairs[0], "-")
|
||||
pair2 := strings.Split(pairs[1], "-")
|
||||
|
||||
p1set := mapset.NewSet[int](Range(pair1[0], pair1[1])...)
|
||||
p2set := mapset.NewSet[int](Range(pair2[0], pair2[1])...)
|
||||
|
||||
if len(p1set.Intersect(p2set).ToSlice()) > 0 {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func Range(start string, end string)[]int {
|
||||
s, _ := strconv.Atoi(start)
|
||||
e, _ := strconv.Atoi(end)
|
||||
|
||||
var r []int
|
||||
for i := s; i <= e; i++ {
|
||||
r = append(r, i)
|
||||
}
|
||||
return r
|
||||
}
|
||||
29
2022/go/day04/day04_test.go
Normal file
29
2022/go/day04/day04_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package day04
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(
|
||||
`2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8`)
|
||||
require.Equal(t, 2, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(
|
||||
`2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8`)
|
||||
require.Equal(t, 4, r)
|
||||
}
|
||||
1000
2022/go/day04/input.txt
Normal file
1000
2022/go/day04/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user