Last active
November 4, 2019 14:44
-
-
Save ethicalvats/4cf330b8c3d016e45817b02dba658f92 to your computer and use it in GitHub Desktop.
movement in arrays
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 ( | |
"bufio" | |
"fmt" | |
"math" | |
"os" | |
"strconv" | |
"strings" | |
) | |
func check(err error){ | |
if err != nil{ | |
panic(err) | |
} | |
} | |
func sqrt(n int) int{ | |
return int(math.Sqrt(float64(n))) | |
} | |
func primeFactors(n int) []int{ | |
var arr []int | |
// Print the number of 2s that divide n | |
for n % 2 == 0{ | |
n = n/2 | |
arr = append(arr, 2) | |
} | |
// n must be odd at this point. So we can skip | |
// one element (Note i = i +2) | |
for i := 3; i <= sqrt(n); i = i + 2{ | |
// While i divides n, print i and divide n | |
for n % i == 0{ | |
n = n/i | |
arr = append(arr, i) | |
} | |
} | |
// This condition is to handle the case when n | |
// is a prime number greater than 2 | |
if n > 2 { | |
arr = append(arr, n) | |
} | |
return arr | |
} | |
func process(arr []int, moves int){ | |
fmt.Println("processing: ", arr, moves) | |
factMap := make(map[int][]int) | |
for _, val := range arr{ | |
factors := primeFactors(val) | |
factMap[val] = factors | |
} | |
movesCount := 0 | |
outer: | |
for i:=0;i<len(arr);{ | |
fmt.Println("processing: ", arr, moves) | |
fmt.Println("original index: ", i) | |
fmt.Println("moves left: ", moves-movesCount) | |
pf := /*primeFactors(arr[i])*/ factMap[arr[i]] | |
fmt.Println("prime factors: ", pf, arr[i]) | |
if len(pf) > 0{ | |
inner: | |
for j:=0;j<len(pf);j++{ | |
if movesCount >= moves { | |
if i == len(arr)-1{ | |
fmt.Println("YES") | |
break outer | |
}else{ | |
fmt.Println("NO") | |
break outer | |
} | |
}else{ | |
fmt.Println("index: ",i) | |
val := pf[j] | |
if val > moves{ | |
fmt.Println("NO") | |
break outer | |
} | |
fmt.Println("factor: ", val) | |
movesCount++ | |
if i + val < len(arr) - 1{ | |
i += val | |
factMap[arr[i]] = pf[1:] | |
break inner | |
}else if i + val > len(arr) - 1{ | |
i -= val | |
factMap[arr[i]] = pf[1:] | |
break inner | |
}else if i - val < 0{ | |
if j < len(pf)-1{ | |
factMap[arr[i]] = pf[1:] | |
continue inner | |
}else{ | |
factMap[arr[i]] = pf[1:] | |
continue outer | |
} | |
}else if i + val == len(arr)-1 && movesCount == moves{ | |
fmt.Println("YES") | |
break outer | |
} | |
} | |
} | |
}else{ | |
i += arr[i] | |
fmt.Println("No prime factor: ", i) | |
} | |
} | |
} | |
func start(problems []Problem){ | |
fmt.Println("") | |
for _, p := range problems{ | |
var strToInt []int | |
for _, s := range strings.Split(p.inputs, " "){ | |
i, _ := strconv.Atoi(s) | |
strToInt = append(strToInt, i) | |
} | |
process(strToInt, p.moves) | |
} | |
} | |
type Problem struct { | |
inputs string | |
moves int | |
} | |
func main(){ | |
var count int | |
var maxCount int | |
var problemIncrementCount = 0 | |
var problems []Problem | |
var arrIn string | |
var moves int | |
reader := bufio.NewReader(os.Stdin) | |
for { | |
//fmt.Println(count, maxCount) | |
text, _ := reader.ReadString('\n') | |
if count == 0{ | |
str := strings.TrimSuffix(text, "\n") | |
maxCount, _ = strconv.Atoi(str) | |
}else{ | |
//fmt.Print("Enter text: ") | |
if problemIncrementCount >= 3{ | |
problemIncrementCount = 0 | |
arrIn = "" | |
moves = 0 | |
} | |
if problemIncrementCount == 1{ | |
arrIn = strings.TrimSuffix(text, "\n") | |
}else if problemIncrementCount == 2 { | |
moves, _ = strconv.Atoi(strings.TrimSuffix(text, "\n")) | |
} | |
problemIncrementCount++ | |
if arrIn != "" && moves != 0{ | |
problem := Problem{ | |
inputs: arrIn, | |
moves: moves, | |
} | |
problems = append(problems, problem) | |
} | |
} | |
if len(problems) == maxCount{ | |
start(problems) | |
break | |
} | |
count ++ | |
} | |
} |
only works for the last case, need to update with the prime factors
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
just to gather input for processing