rename directory

This commit is contained in:
VicRen
2020-10-22 14:04:57 +08:00
parent 66813aa5ca
commit acfe8ce353
18 changed files with 0 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
@@ -0,0 +1,356 @@
package main
import (
"reflect"
"testing"
)
func TestGetAdjacent(t *testing.T) {
tt := []struct {
name string
input [][]int
x int
y int
want int
}{
{
"out_of_range",
[][]int{
{1, 2},
{1, 2},
},
0,
3,
0,
},
{
"out_of_range_2",
[][]int{
{1, 2},
{1, 2},
},
-1,
1,
0,
},
{
"get_2",
[][]int{
{1, 2},
{1, 2},
},
1,
1,
2,
},
{
"get_1",
[][]int{
{1, 2},
{1, 2},
},
0,
0,
1,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
got := GetAdjacent(tc.input, tc.x, tc.y)
if got != tc.want {
t.Errorf("GetAdjacent(%v, %d, %d)=%d, want %d", tc.input, tc.x, tc.y, got, tc.want)
}
})
}
}
func TestGetRight4(t *testing.T) {
tt := []struct {
name string
input [][]int
want [][]int
}{
{
"not_enough_digits",
[][]int{
{1, 2, 3},
{1, 2, 3},
},
nil,
},
{
"exact_four",
[][]int{
{1, 2, 3, 4},
{1, 2, 3, 4},
},
[][]int{
{1, 2, 3, 4},
{1, 2, 3, 4},
},
},
{
"more_than_four_1",
[][]int{
{1, 2, 3, 4, 5},
},
[][]int{
{1, 2, 3, 4},
{2, 3, 4, 5},
},
},
{
"more_than_four_2",
[][]int{
{1, 2, 3, 4, 5},
{1, 2, 3, 4, 5},
},
[][]int{
{1, 2, 3, 4},
{2, 3, 4, 5},
{1, 2, 3, 4},
{2, 3, 4, 5},
},
},
{
"more_than_four_3",
[][]int{
{1, 2, 3, 4, 5, 6},
},
[][]int{
{1, 2, 3, 4},
{2, 3, 4, 5},
{3, 4, 5, 6},
},
},
{
"more_than_four_4",
[][]int{
{1, 2, 3, 4, 5, 6},
{1, 2, 3, 4, 5, 6},
},
[][]int{
{1, 2, 3, 4},
{2, 3, 4, 5},
{3, 4, 5, 6},
{1, 2, 3, 4},
{2, 3, 4, 5},
{3, 4, 5, 6},
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
got := GetRight4(tc.input)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("GetRight4(%v)=%v, want %v", tc.input, got, tc.want)
}
})
}
}
func TestGetDown4(t *testing.T) {
tt := []struct {
name string
input [][]int
want [][]int
}{
{
"not_enough_lines",
[][]int{
{1, 2, 3},
{1, 2, 3},
},
nil,
},
{
"one_digits",
[][]int{
{1},
{1},
{1},
{1},
},
[][]int{{1, 1, 1, 1}},
},
{
"one_digits_2",
[][]int{
{1, 2},
{1},
{1, 3},
{1},
},
[][]int{{1, 1, 1, 1}},
},
{
"more_than_one_digits",
[][]int{
{1, 2},
{1, 3},
{1, 3},
{1, 5},
},
[][]int{
{1, 1, 1, 1},
{2, 3, 3, 5},
},
},
{
"more_than_one_digits_2",
[][]int{
{1, 2, 3},
{1, 3, 4},
{1, 3},
{1, 5},
},
[][]int{
{1, 1, 1, 1},
{2, 3, 3, 5},
},
},
{
"more_than_one_digits_3",
[][]int{
{1, 2, 3},
{1, 3, 4},
{1, 3, 5},
{1, 5, 10},
},
[][]int{
{1, 1, 1, 1},
{2, 3, 3, 5},
{3, 4, 5, 10},
},
},
{
"more_than_one_digits_4",
[][]int{
{1, 2},
{1},
{1, 3},
{1},
{2},
},
[][]int{
{1, 1, 1, 1},
{1, 1, 1, 2},
},
},
{
"more_than_one_digits_5",
[][]int{
{1, 2},
{1, 1},
{1, 3},
{1, 4},
{2, 5},
},
[][]int{
{1, 1, 1, 1},
{1, 1, 1, 2},
{2, 1, 3, 4},
{1, 3, 4, 5},
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
got := GetDown4(tc.input)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("GetDown4(%v)=%v, want %v", tc.input, got, tc.want)
}
})
}
}
func TestGetDiagonal4(t *testing.T) {
tt := []struct {
name string
input [][]int
want [][]int
}{
{
"not_enough_lines",
[][]int{
{1, 2, 3},
{1, 2, 3},
},
nil,
},
{
"not_enough_columns",
[][]int{
{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
},
nil,
},
{
"one_result",
[][]int{
{1, 2, 3},
{1, 2, 3},
{1, 2, 3},
{1, 2, 3, 4},
},
[][]int{{1, 2, 3, 4}},
},
{
"more_than_one_result",
[][]int{
{1, 2, 3},
{1, 2, 3},
{1, 2, 3, 4},
{1, 2, 3, 4, 5},
},
[][]int{
{1, 2, 3, 4},
{2, 3, 4, 5},
},
},
{
"more_than_one_result_2",
[][]int{
{1, 2, 3},
{1, 2, 3},
{1, 2, 3, 4},
{1, 2, 3, 4},
{1, 2, 3, 4},
},
[][]int{
{1, 2, 3, 4},
{1, 2, 3, 4},
},
},
{
"more_than_one_result_3",
[][]int{
{1, 2, 3},
{1, 2, 3},
{1, 2, 3, 4},
{1, 2, 3, 4, 5},
{1, 2, 3, 4},
},
[][]int{
{1, 2, 3, 4},
{1, 2, 3, 4},
{2, 3, 4, 5},
},
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
got := GetDiagonal4(tc.input)
if !reflect.DeepEqual(got, tc.want) {
t.Errorf("GetDiagonal4(%v)=%v, want %v", tc.input, got, tc.want)
}
})
}
}
+187
View File
@@ -0,0 +1,187 @@
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
func main() {
fi, err := os.Open("./digits.txt")
if err != nil {
panic(err)
}
defer fi.Close()
br := bufio.NewReader(fi)
var input [][]int
for {
a, _, e := br.ReadLine()
if e == io.EOF {
break
}
got := strings.Split(string(a), " ")
var line []int
for _, v := range got {
n, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
line = append(line, n)
}
input = append(input, line)
}
fmt.Println("input=", input)
d4 := GetDown4(input)
fmt.Println(d4)
var product *Result
for y := 0; y < len(input); y++ {
for x := 0; x < len(input); x++ {
p1 := NewResult()
p2 := NewResult()
p3 := NewResult()
p4 := NewResult()
for i := 0; i < 4; i++ {
p1.Product *= GetAdjacent(input, y, x+i)
p1.Line = append(p1.Line, GetAdjacent(input, y, x+i))
p2.Product *= GetAdjacent(input, y+i, x)
p2.Line = append(p2.Line, GetAdjacent(input, y+i, x))
p3.Product *= GetAdjacent(input, y+i, x+i)
p3.Line = append(p3.Line, GetAdjacent(input, y+i, x+i))
p4.Product *= GetAdjacent(input, y+i, x-i)
p4.Line = append(p4.Line, GetAdjacent(input, y+i, x-i))
}
product = Max(p1, p2, p3, p4, product)
}
}
fmt.Println("product is", product)
}
type Result struct {
Product int
Line []int
}
func NewResult() *Result {
return &Result{
Product: 1,
}
}
func (r *Result) String() string {
return fmt.Sprintln("product is", r.Product, "line:", r.Line)
}
func Max(n, n2, n3, n4, n5 *Result) *Result {
max := NewResult()
if n.Product > max.Product {
max = n
}
if n2.Product > max.Product {
max = n2
}
if n3.Product > max.Product {
max = n3
}
if n4.Product > max.Product {
max = n4
}
if n5 != nil && n5.Product > max.Product {
max = n5
}
return max
}
func GetAdjacent(input [][]int, x, y int) int {
if 0 <= y && y < len(input) && 0 <= x && x < len(input[y]) {
return input[y][x]
}
return 0
}
func GetRight4(input [][]int) [][]int {
var ret [][]int
for _, line := range input {
l := len(line)
if l < 4 {
continue
} else if l == 4 {
ret = append(ret, line)
} else {
for x := 0; x+4 <= l; x++ {
ret = append(ret, line[x:x+4])
}
}
}
return ret
}
func GetDown4(input [][]int) [][]int {
length := len(input)
if length < 4 {
return nil
}
maxL := 0
for _, line := range input {
if len(line) > maxL {
maxL = len(line)
}
}
var ret [][]int
for c := 0; c < maxL; c++ {
for l := 0; l <= length-4; l++ {
var one []int
for d := 0; d < 4; d++ {
if len(input[l+d]) > c {
one = append(one, input[l+d][c])
}
}
if len(one) == 4 {
ret = append(ret, one)
}
}
}
return ret
}
func GetDiagonal4(input [][]int) [][]int {
l := len(input)
if l < 4 {
return nil
}
maxL := 0
for _, line := range input {
if len(line) > maxL {
maxL = len(line)
}
}
if maxL < 4 {
return nil
}
var ret [][]int
for x := 0; x < maxL; x++ {
for y := 0; y < l; y++ {
if l < y+4 {
break
}
var l []int
for d := 0; d < 4; d++ {
line := input[d+y]
if len(line) <= d+x {
break
}
l = append(l, line[x+d])
}
if len(l) == 4 {
ret = append(ret, l)
}
}
}
return ret
}