Rename folders
This commit is contained in:
84
07/main.go
Normal file
84
07/main.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func randomOperator(equation []int) [][]int {
|
||||
combinationsTotal := math.Pow(3, float64(len(equation)-2))
|
||||
combinations := [][]int{}
|
||||
for i:=0;i<int(combinationsTotal);i++ {
|
||||
combination := strconv.FormatInt(int64(i), 3)
|
||||
combinationSSlice := strings.Split(combination, "")
|
||||
prepend := len(equation) - 2 - len(combinationSSlice)
|
||||
if prepend>0 {
|
||||
for j:=1;j<=prepend;j++ {
|
||||
combinationSSlice = append([]string{"0"}, combinationSSlice...)
|
||||
}
|
||||
}
|
||||
combinationISlice := []int{}
|
||||
for j:=range combinationSSlice {
|
||||
combinationInt, _ := strconv.Atoi(combinationSSlice[j])
|
||||
combinationISlice = append(combinationISlice, combinationInt)
|
||||
}
|
||||
combinations = append(combinations, combinationISlice)
|
||||
}
|
||||
return combinations
|
||||
}
|
||||
|
||||
func calcResult(tillnow, next, operator int) int {
|
||||
if operator == 0 {
|
||||
return tillnow + next
|
||||
} else if operator == 1 {
|
||||
return tillnow * next
|
||||
} else {
|
||||
// find number of digits
|
||||
digits := 0
|
||||
helper := next
|
||||
for helper > 0 {
|
||||
helper = helper/10
|
||||
digits++
|
||||
}
|
||||
return tillnow*int(math.Pow(10,float64(digits)))+next
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
file, _ := os.Open("test")
|
||||
defer file.Close()
|
||||
scanner := bufio.NewScanner(file)
|
||||
regexNumber := regexp.MustCompile(`[0-9]+`)
|
||||
equations := [][]int{}
|
||||
randomOperators := [][][]int{} // []equation []operators []operator
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
equationStrings := regexNumber.FindAllString(line, -1)
|
||||
equationInts := []int{}
|
||||
for i := range equationStrings {
|
||||
equationInt, _ := strconv.Atoi(equationStrings[i])
|
||||
equationInts = append(equationInts, equationInt)
|
||||
}
|
||||
equations = append(equations, equationInts)
|
||||
randomOperators = append(randomOperators,randomOperator(equationInts))
|
||||
}
|
||||
result := 0
|
||||
for i := range equations { // for each equation
|
||||
for j := range randomOperators[i] { // for each random operator corresponding to the equation
|
||||
calculations := equations[i][1]
|
||||
for k:=2;k<len(equations[i]);k++ {
|
||||
calculations = calcResult(calculations,equations[i][k],randomOperators[i][j][k-2])
|
||||
}
|
||||
if calculations == equations[i][0] {
|
||||
result = result + equations[i][0]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(result)
|
||||
}
|
||||
Reference in New Issue
Block a user