Last active
November 12, 2017 16:15
-
-
Save kei-q/79a80dccc9cdd264fec2988c047d51c6 to your computer and use it in GitHub Desktop.
doukakuE18 golang http://nabetani.sakura.ne.jp/hena/orde18twintri/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
"strings" | |
) | |
type Triangle struct { | |
x, y int | |
d string | |
h int | |
} | |
type Range struct { | |
min, max int | |
} | |
type Output = int | |
func main() { | |
ret := Solve("7,0R6/3,1B5") | |
fmt.Printf("%v : %v", ret, "15") | |
} | |
func Solve(input string) string { | |
return format(solve(parse(input))) | |
} | |
func parse(s string) (Triangle, Triangle) { | |
r := strings.NewReplacer("R", ",R,", "L", ",L,", "T", ",T,", "B", ",B,", "/", ",") | |
ab := strings.Split(r.Replace(s), ",") | |
return parse_(ab[0:4]), parse_(ab[4:8]) | |
} | |
func parse_(ss []string) Triangle { | |
x, _ := strconv.Atoi(ss[0]) | |
y, _ := strconv.Atoi(ss[1]) | |
h, _ := strconv.Atoi(ss[3]) | |
return Triangle{x: x, y: y, d: ss[2], h: h} | |
} | |
func format(o Output) string { | |
return fmt.Sprint(o) | |
} | |
func solve(a, b Triangle) Output { | |
am := makeMap(a) | |
bm := makeMap(b) | |
ret := 0 | |
for k, v := range am { | |
if bv, ok := bm[k]; ok { | |
ret += countOverlap(v, bv) | |
} | |
} | |
return ret | |
} | |
func countOverlap(a, b Range) int { | |
min := a.min | |
if min < b.min { | |
min = b.min | |
} | |
max := a.max | |
if max > b.max { | |
max = b.max | |
} | |
if max-min < 0 { | |
return 0 | |
} | |
return max - min + 1 | |
} | |
func makeMap(t Triangle) map[int]Range { | |
switch t.d { | |
case "T": | |
return makeMapTB(t, 1) | |
case "B": | |
return makeMapTB(t, -1) | |
case "R": | |
return makeMapLR(t, t.d) | |
case "L": | |
return makeMapLR(t, t.d) | |
default: | |
panic("makeMap") | |
} | |
} | |
func makeMapTB(t Triangle, dir int) map[int]Range { | |
hoge := make(map[int]Range) | |
for i := 0; i < t.h; i++ { | |
hoge[t.y+i*dir] = Range{t.x - i, t.x + i} | |
} | |
return hoge | |
} | |
func makeMapLR(t Triangle, dir string) map[int]Range { | |
hoge := make(map[int]Range) | |
for i := 0; i < t.h; i++ { | |
min := t.x + i | |
max := t.x + t.h - 1 | |
if dir == "R" { | |
min = t.x - t.h + 1 | |
max = t.x - i | |
} | |
hoge[t.y-i] = Range{min, max} | |
hoge[t.y+i] = Range{min, max} | |
} | |
return hoge | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
) | |
var table = []struct { | |
in, out string | |
}{ | |
{"7,0R6/3,1B5", "15"}, | |
{"1,6L4/4,9R9", "4"}, | |
{"0,2R4/1,3B4", "3"}, | |
{"1,2L4/1,2L5", "16"}, | |
{"3,2L5/5,6B4", "8"}, | |
{"4,1B3/6,3B4", "4"}, | |
{"4,4R7/4,3R5", "20"}, | |
{"4,5R9/1,7T3", "7"}, | |
{"4,7T9/1,6T3", "1"}, | |
{"4,8B7/3,7L4", "10"}, | |
{"5,3L3/9,8L4", "0"}, | |
{"5,6B4/4,4R2", "3"}, | |
{"5,6B4/8,5R4", "8"}, | |
{"5,8B9/5,2L2", "4"}, | |
{"6,1L5/7,1T2", "3"}, | |
{"7,2B5/7,2T4", "1"}, | |
{"7,3T9/9,6L6", "11"}, | |
{"8,0R6/8,1R7", "30"}, | |
{"0,4R7/4,6R10", "36"}, | |
{"10,4L4/9,1T6", "9"}, | |
{"2,2T7/6,7T10", "4"}, | |
{"2,7R4/1,6L8", "2"}, | |
{"3,0R10/1,2T7", "7"}, | |
{"3,5T2/3,6B10", "2"}, | |
{"4,7R10/8,2T8", "6"}, | |
{"6,8B10/4,5B6", "36"}, | |
{"9,2B7/1,1B10", "6"}, | |
{"9,3R14/2,4R1", "1"}, | |
{"3,0R10/0,6B10", "54"}, | |
{"4,10T8/4,10T8", "64"}, | |
{"12,11T18/7,18R18", "34"}, | |
{"46,34T34/34,29T14", "30"}, | |
{"291,11T120/258,54B130", "424"}, | |
{"62,170L139/133,172R21", "441"}, | |
{"98,189B116/183,127R27", "240"}, | |
{"646,684B96/435,690R772", "0"}, | |
{"113,668L866/581,859L852", "158404"}, | |
{"309,321B162/137,420B423", "15750"}, | |
{"5474,6459R9089/8177,150R5120", "376996"}, | |
{"2399,1640B2451/1718,2100L1623", "221334"}, | |
{"5621,8460T7612/2715,5697L8851", "861192"}, | |
} | |
func TestSolve(t *testing.T) { | |
for _, tt := range table { | |
s := Solve(tt.in) | |
if s != tt.out { | |
t.Errorf("Solve(%q) => %q, want %q", tt.in, s, tt.out) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment