Started 2022 with go
This commit is contained in:
90
2022/go/04/answer.go
Normal file
90
2022/go/04/answer.go
Normal file
@ -0,0 +1,90 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Range struct {
|
||||
low, high int
|
||||
}
|
||||
|
||||
func (r *Range) contians(other *Range) bool {
|
||||
return r.low <= other.low && other.high <= r.high
|
||||
}
|
||||
|
||||
func (r *Range) overlaps(other *Range) bool {
|
||||
return (r.low <= other.low && other.low <= r.high) ||
|
||||
(r.low <= other.high && other.high <= r.high)
|
||||
}
|
||||
|
||||
func fromString(s string) Range {
|
||||
parts := strings.Split(s, "-")
|
||||
low, err := strconv.Atoi(parts[0])
|
||||
check(err, "Can't convert %q to int", parts[0])
|
||||
high, err := strconv.Atoi(parts[1])
|
||||
check(err, "Can't convert %q to int", parts[1])
|
||||
return Range{low, high}
|
||||
}
|
||||
|
||||
type Input struct {
|
||||
ranges []struct{ a, b Range }
|
||||
}
|
||||
|
||||
func input(filename string) *Input {
|
||||
file, err := os.Open(filename)
|
||||
check(err, "Couldn't open %q", filename)
|
||||
defer file.Close()
|
||||
|
||||
input := &Input{}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
scanner.Split(bufio.ScanLines)
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
sets := strings.Split(line, ",")
|
||||
input.ranges = append(input.ranges, struct{ a, b Range }{fromString(sets[0]), fromString(sets[1])})
|
||||
}
|
||||
|
||||
return input
|
||||
}
|
||||
|
||||
func result(inp *Input, part int8) string {
|
||||
var res string
|
||||
score := 0
|
||||
|
||||
for _, pair := range inp.ranges {
|
||||
if part == 1 {
|
||||
if pair.a.contians(&pair.b) || pair.b.contians(&pair.a) {
|
||||
score++
|
||||
}
|
||||
} else {
|
||||
if pair.a.overlaps(&pair.b) || pair.b.overlaps(&pair.a) {
|
||||
score++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res = fmt.Sprint(score)
|
||||
return res
|
||||
}
|
||||
|
||||
func check(e error, msg string, vals ...any) {
|
||||
if e != nil {
|
||||
log.Printf("ERROR: "+msg, vals)
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
result := result(input("sample1"), 1)
|
||||
|
||||
log.Println(result)
|
||||
fmt.Println(result)
|
||||
}
|
47
2022/go/04/answer_test.go
Normal file
47
2022/go/04/answer_test.go
Normal file
@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInputSample1(t *testing.T) {
|
||||
filename := "sample1"
|
||||
expected := &Input{
|
||||
ranges: []struct{ a, b Range }{
|
||||
{Range{2, 4}, Range{6, 8}},
|
||||
{Range{2, 3}, Range{4, 5}},
|
||||
{Range{5, 7}, Range{7, 9}},
|
||||
{Range{2, 8}, Range{3, 7}},
|
||||
{Range{6, 6}, Range{4, 6}},
|
||||
{Range{2, 6}, Range{4, 8}},
|
||||
},
|
||||
}
|
||||
var got *Input = input(filename)
|
||||
|
||||
if !reflect.DeepEqual(expected, got) {
|
||||
t.Errorf("input(%q) = %v, want %v", filename, got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResult(t *testing.T) {
|
||||
tests := []struct {
|
||||
part int8
|
||||
filename string
|
||||
expected string
|
||||
}{
|
||||
{1, "sample1", "2"},
|
||||
{1, "input", "530"},
|
||||
|
||||
{2, "sample1", "4"},
|
||||
{2, "input", "903"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
var got string = result(input(test.filename), test.part)
|
||||
|
||||
if got != test.expected {
|
||||
t.Errorf("result = %q, want %q", got, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
3
2022/go/04/go.mod
Normal file
3
2022/go/04/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module aoc/4
|
||||
|
||||
go 1.20
|
1000
2022/go/04/input
Normal file
1000
2022/go/04/input
Normal file
File diff suppressed because it is too large
Load Diff
6
2022/go/04/sample1
Normal file
6
2022/go/04/sample1
Normal file
@ -0,0 +1,6 @@
|
||||
2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
2-8,3-7
|
||||
6-6,4-6
|
||||
2-6,4-8
|
Reference in New Issue
Block a user