re-organise repo

This commit is contained in:
2023-11-16 10:48:53 +00:00
parent f62c52d8a9
commit f133e157a1
87 changed files with 11792 additions and 0 deletions

59
2022/go/day04/day04.go Normal file
View 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
}

View 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

File diff suppressed because it is too large Load Diff