solve_2.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strconv"
  7. )
  8. func main() {
  9. fmt.Println("Advent of Code 2025 - Day 3 - Part 2")
  10. f,_:= os.Open("3/input")
  11. defer f.Close()
  12. scanner := bufio.NewScanner(f)
  13. var res int64;
  14. var store_comput [100][13]int64
  15. for scanner.Scan(){
  16. bank := scanner.Text()
  17. init_store_comput(&store_comput)
  18. max_volt := max_voltage2(bank, 0, 12, &store_comput)
  19. res += max_volt
  20. }
  21. fmt.Println(res)
  22. }
  23. func init_store_comput(store_comput *[100][13]int64){
  24. for i:= 0; i < 100; i++{
  25. for j:=0; j < 13; j++{
  26. store_comput[i][j] = -1
  27. }
  28. }
  29. }
  30. var pre = [12]int64{
  31. 1, 10, 100,
  32. 1_000, 10_000, 100_000,
  33. 1_000_000, 10_000_000, 100_000_000,
  34. 1_000_000_000, 10_000_000_000, 100_000_000_000}
  35. func max_voltage2(bank string, i,k int, store_comput *[100][13]int64) int64{
  36. if (k == 0) {return 0}
  37. if (i == len(bank)) {return -999999999999999}
  38. if (store_comput[i][k] != -1){
  39. return store_comput[i][k]
  40. }
  41. max1 := max_voltage2(bank, i+1, k, store_comput)
  42. curr,_ := strconv.Atoi(string(bank[i]))
  43. max2 := max_voltage2(bank,i+1,k-1,store_comput) + (int64(curr) * pre[k-1])
  44. store_comput[i][k] = max(max1,max2)
  45. return store_comput[i][k]
  46. }