Skip to content

Instantly share code, notes, and snippets.

@kei-q
Last active September 16, 2017 14:53
Show Gist options
  • Save kei-q/0bc3c845da6bb1c5f6fdcfccdd0cd424 to your computer and use it in GitHub Desktop.
Save kei-q/0bc3c845da6bb1c5f6fdcfccdd0cd424 to your computer and use it in GitHub Desktop.
doukaku E17
package main
import (
"fmt"
"strings"
"strconv"
"math"
)
func Reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func list_with_empty(min int, max int, b int) []string {
tmp := []string{""}
for i := min; i < max; i++ {
s := strconv.FormatInt(int64(i), b)
tmp = append(tmp, s)
}
return tmp
}
func solve_(min int64, max int64, b int) int {
x := int(math.Sqrt(float64(max)))
result := 0
for _, s := range list_with_empty(1, x, b) {
rev_s := Reverse(s)
for _, c := range list_with_empty(0, b, b) {
t := fmt.Sprintf("%s%s%s", s, c, rev_s)
n, _ := strconv.ParseInt(t, b, 64)
if min <= n && n < max {
result++
}
}
}
return result
}
func solve(input string) string {
var inputs []int64
for _, v := range strings.Split(input, ",") {
n, _ := strconv.ParseInt(v, 10, 64)
inputs = append(inputs, n)
}
min := inputs[0]
max := inputs[1]
b := inputs[2]
count := solve_(min, max, int(b))
return fmt.Sprint(count)
}
func test(input string, expected string) {
actual := solve(input)
if actual == expected {
fmt.Println("ok")
} else {
fmt.Printf("!!!! %s : %s : %s\n", input, expected, actual)
}
}
func tests() {
test("12,34,5", "5")
test("10,11,10", "0")
test("1,100,3", "18")
test("11,12,10", "1")
test("12,13,10", "0")
test("123,456,7", "33")
test("38,274,14", "17")
test("98,76543,2", "535")
test("987,6543,2", "103")
test("5057,5202,3", "2")
test("98,76543,21", "589")
test("987,6543,21", "264")
test("1097,2889,11", "35")
test("2764,6482,17", "132")
test("16333,24085,8", "121")
test("21759,67173,20", "114")
test("32026,57805,22", "53")
test("188318,407853,6", "523")
test("51669,116065,30", "72")
test("294104,515248,32", "216")
test("444257,740280,15", "1316")
test("645098,2741620,9", "2876")
test("12345,987654321,2", "62684")
test("2467130,8433468,2", "2902")
test("323901,4712975,10", "4389")
test("12345,987654321,36", "67446")
test("3969344,4086910,24", "205")
test("19743263,83912295,5", "11553")
test("6349529,39870823,10", "6637")
test("66160071,153732445,5", "5605")
test("18799557,189007582,14", "33741")
test("78547566,225312226,20", "18346")
test("143084571,506549072,18", "62323")
test("2099642384,2789567569,6", "14787")
}
func main() {
tests()
}
import Data.List.Split (splitOn)
import Control.Lens ((^.), (^?!), re)
import Numeric.Lens (base)
solve = show . gen . map read . splitOn ","
gen [mn,mx,b] = length [n | n <- rs, mn <= n && n < mx]
where
rs = map (toN b) $ tail $ concatMap gen' $ "" : map (toS b) [1..(floor$sqrt$toEnum mx)]
gen' s = [s ++ c ++ reverse s | c <- "" : map (toS b) [0..b-1]]
toN b s = s ^?! base b
toS b n = n ^. re (base b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment