Advertisement
Guest User

AOC2024 Day 3

a guest
Dec 2nd, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.06 KB | Source Code | 0 0
  1. package main
  2.  
  3. import (
  4.     "common"
  5.     "fmt"
  6.     "regexp"
  7.     "strconv"
  8.     "strings"
  9. )
  10.  
  11. // quite the ugly set of solutions, these are
  12. // just had to use regex to find the matching patterns.
  13.  
  14. // I use some common utilities here are there to make fetching files and reading inputs easier
  15.  
  16. func PartOne(filename string) {
  17.     handler, err := common.FetchFile(filename)
  18.     if err != nil {
  19.         panic(err)
  20.     }
  21.  
  22.     lines := make([]string, 4)
  23.     for {
  24.         input, _, err := handler.Reader.ReadLine()
  25.         if err != nil {
  26.             break
  27.         }
  28.         lines = append(lines, string(input))
  29.     }
  30.     input := strings.Join(lines, "")
  31.     fmt.Println(input)
  32.  
  33.     re := regexp.MustCompile(`mul\([0-9]{1,3},[0-9]{1,3}\)`)
  34.     results := re.FindAllStringSubmatchIndex(input, -1)
  35.     sum := 0
  36.     if results != nil {
  37.         for _, result := range results {
  38.             fmt.Println(input[result[0]:result[1]])
  39.             pairString := input[result[0]:result[1]]
  40.             pair := pairString[4 : len(pairString)-1]
  41.             intStrings := strings.Split(string(pair), ",")
  42.             argOne, err := strconv.ParseInt(intStrings[0], 10, 64)
  43.             if err != nil {
  44.                 panic(err)
  45.             }
  46.             argTwo, err := strconv.ParseInt(intStrings[1], 10, 64)
  47.             if err != nil {
  48.                 panic(err)
  49.             }
  50.             sum += int(argOne * argTwo)
  51.         }
  52.     }
  53.     fmt.Println(sum)
  54. }
  55.  
  56. func mostRecent(matches [][]int, currentIndex int) (retVal int) {
  57.     retVal = -1
  58.     fmt.Println(matches, currentIndex)
  59.     for _, match := range matches {
  60.         if match[0] < currentIndex {
  61.             retVal = match[0]
  62.         } else {
  63.             break
  64.         }
  65.     }
  66.     return
  67. }
  68.  
  69. func PartTwo(filename string) {
  70.     handler, err := common.FetchFile(filename)
  71.     if err != nil {
  72.         panic(err)
  73.     }
  74.  
  75.     lines := make([]string, 4)
  76.     for {
  77.         input, _, err := handler.Reader.ReadLine()
  78.         if err != nil {
  79.             break
  80.         }
  81.         lines = append(lines, string(input))
  82.     }
  83.     input := strings.Join(lines, "")
  84.     fmt.Println(input)
  85.  
  86.     re := regexp.MustCompile(`mul\([0-9]{1,3},[0-9]{1,3}\)`)
  87.     dontre := regexp.MustCompile(`don't\(\)`)
  88.     dore := regexp.MustCompile(`do\(\)`)
  89.  
  90.     results := re.FindAllStringSubmatchIndex(input, -1)
  91.     donts := dontre.FindAllStringSubmatchIndex(input, -1)
  92.     dos := dore.FindAllStringSubmatchIndex(input, -1)
  93.  
  94.     sum := 0
  95.  
  96.     if results != nil {
  97.         for _, result := range results {
  98.             pairString := input[result[0]:result[1]]
  99.             pair := pairString[4 : len(pairString)-1]
  100.             intStrings := strings.Split(string(pair), ",")
  101.             argOne, err := strconv.ParseInt(intStrings[0], 10, 64)
  102.             if err != nil {
  103.                 panic(err)
  104.             }
  105.             argTwo, err := strconv.ParseInt(intStrings[1], 10, 64)
  106.             if err != nil {
  107.                 panic(err)
  108.             }
  109.  
  110.             recentDo := mostRecent(dos, result[0])
  111.             recentDont := mostRecent(donts, result[0])
  112.             if recentDo == -1 && recentDont == -1 {
  113.                 // multiplication is enabled by default
  114.                 sum += int(argOne * argTwo)
  115.             } else if recentDo > recentDont {
  116.                 // multiplication is enabled now, since there was a more recent do call than a don't.
  117.                 sum += int(argOne * argTwo)
  118.             }
  119.         }
  120.     }
  121.     fmt.Println(sum)
  122. }
  123.  
  124. func main() {
  125.     filename := common.GetProblemFile(3)
  126.     fmt.Println("part one:")
  127.     PartOne(filename)
  128.     fmt.Println("part two:")
  129.     PartTwo(filename)
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement