2024 day 7 - part 2
This commit is contained in:
parent
3fd95ca53d
commit
942c629d86
@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
file, err := os.Open("./input_test.txt")
|
||||
file, err := os.Open("./input.txt")
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open file: %s\n", err)
|
||||
}
|
||||
@ -49,21 +49,6 @@ func main() {
|
||||
}
|
||||
|
||||
fmt.Println("result part1: ", result1)
|
||||
|
||||
// result1 := 0
|
||||
//
|
||||
// var nums []int
|
||||
// nums = append(nums, 81, 40, 27)
|
||||
//
|
||||
// var mulEq []rune = make([]rune, 0)
|
||||
// mulEq = append(mulEq, '*')
|
||||
// var addEq []rune = make([]rune, 0)
|
||||
// addEq = append(addEq, '+')
|
||||
//
|
||||
// testSolution(&result1, 3267, nums, mulEq)
|
||||
// testSolution(&result1, 3267, nums, addEq)
|
||||
//
|
||||
// fmt.Println("result part1: ", result1)
|
||||
}
|
||||
|
||||
func testSolution(result *bool, answer int, numbers []int, currentEquasion []rune) {
|
||||
@ -75,50 +60,46 @@ func testSolution(result *bool, answer int, numbers []int, currentEquasion []run
|
||||
localNums := make([]int, len(numbers))
|
||||
copy(localNums, numbers)
|
||||
|
||||
// maxlen := len(localNums)
|
||||
for i := len(localNums) - 1; i > 0; i-- {
|
||||
op := localEq[i-1]
|
||||
|
||||
if op == '|' {
|
||||
// fmt.Println("befor: ", answer, localNums, string(localEq))
|
||||
newNum := ""
|
||||
newNum += fmt.Sprint(localNums[i-1])
|
||||
newNum += fmt.Sprint(localNums[i])
|
||||
newInt, err := strconv.Atoi(newNum)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
localNums[i-1] = newInt
|
||||
localNums = slices.Delete(localNums, i, i+1)
|
||||
localEq = slices.Delete(localEq, i-1, i)
|
||||
|
||||
// fmt.Printf("i: %d, op: %s, new: %d\n", i, string(op), newInt)
|
||||
// fmt.Println("after: ", answer, localNums, string(localEq))
|
||||
// fmt.Println()
|
||||
// fmt.Println()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
res := localNums[0]
|
||||
for i := 1; i < len(localNums); i++ {
|
||||
op := localEq[i-1]
|
||||
if op == '*' {
|
||||
res = (res * localNums[i])
|
||||
|
||||
for i := 0; i < len(localNums)-1; i++ {
|
||||
op := localEq[i]
|
||||
newNum := localNums[i]
|
||||
var err error
|
||||
|
||||
switch op {
|
||||
case '|':
|
||||
{
|
||||
newNum, err = strconv.Atoi(fmt.Sprint(localNums[i]) + fmt.Sprint(localNums[i+1]))
|
||||
}
|
||||
case '*':
|
||||
{
|
||||
newNum *= localNums[i+1]
|
||||
}
|
||||
case '+':
|
||||
{
|
||||
newNum += localNums[i+1]
|
||||
}
|
||||
}
|
||||
if op == '+' {
|
||||
res = (res + localNums[i])
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
localNums[i] = newNum
|
||||
localNums = slices.Delete(localNums, i+1, i+2)
|
||||
localEq = slices.Delete(localEq, i, i+1)
|
||||
i--
|
||||
|
||||
if i < 0 {
|
||||
res = newNum
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("ans: %d, nums: %d, eq: %s, res: %d\n", answer, localNums, string(localEq), res)
|
||||
if res == answer {
|
||||
fmt.Println(res, " correct")
|
||||
*result = true
|
||||
}
|
||||
|
||||
// fmt.Println(answer, numbers, currentEquasion, res)
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user