Advertisement
Guest User

Untitled

a guest
Dec 8th, 2023
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 3.50 KB | None | 0 0
  1. package day5
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "os"
  7.     "strconv"
  8.     "strings"
  9. )
  10.  
  11. const path = "day5/input.txt"
  12. const testpath = "day5/test.txt"
  13.  
  14. type mapping []submapping
  15.  
  16. type submapping struct {
  17.     source, size, offset int
  18. }
  19.  
  20. // Range represents a range with a start and length
  21. type Range struct {
  22.     Start int
  23.     End   int
  24. }
  25.  
  26. func Run() {
  27.     // Open the file
  28.     file, err := os.Open(path)
  29.     if err != nil {
  30.         fmt.Println("Error opening file:", err)
  31.         return
  32.     }
  33.     defer file.Close()
  34.  
  35.     // Create a scanner to read the file
  36.     scanner := bufio.NewScanner(file)
  37.  
  38.     var seeds []int
  39.     var mappings []mapping
  40.  
  41.     currentMap := mapping{}
  42.  
  43.     // Iterate through each line
  44.     for scanner.Scan() {
  45.         line := scanner.Text()
  46.  
  47.         // Skip empty lines
  48.         if line == "" {
  49.             continue
  50.         }
  51.  
  52.         if strings.Contains(line, "seeds: ") {
  53.             seedsString := strings.Split(line, "seeds: ")
  54.             seedList := strings.Split(seedsString[1], " ")
  55.             for _, seedItem := range seedList {
  56.                 seeds = append(seeds, StringToInt(seedItem))
  57.             }
  58.             continue
  59.         }
  60.  
  61.         if strings.Contains(line, "-") {
  62.             // Current map was not empty so we have to start over for the new mappings
  63.             if len(currentMap) > 0 {
  64.                 mappings = append(mappings, currentMap)
  65.             }
  66.             currentMap = mapping{}
  67.             continue
  68.         }
  69.  
  70.         values := strings.Split(line, " ")
  71.  
  72.         currentMap = append(currentMap, submapping{
  73.             source: StringToInt(values[1]),
  74.             size:   StringToInt(values[2]),
  75.             offset: StringToInt(values[0]) - StringToInt(values[1]),
  76.         })
  77.  
  78.     }
  79.     // Add the current map when reaching the end of file
  80.     if len(currentMap) > 0 {
  81.         mappings = append(mappings, currentMap)
  82.     }
  83.  
  84.     // Check for scanner errors
  85.     if err := scanner.Err(); err != nil {
  86.         fmt.Println("Error reading file:", err)
  87.     }
  88.  
  89.     lowest := int(^uint(0) >> 1) // Initialize to the maximum positive int value
  90.     for _, seed := range seeds {
  91.         val := seed
  92.         for _, mapping := range mappings {
  93.             for _, submapping := range mapping {
  94.                 if val >= submapping.source && val <= submapping.source+submapping.size {
  95.                     val += submapping.offset
  96.                     break
  97.                 }
  98.             }
  99.         }
  100.         lowest = min(val, lowest)
  101.     }
  102.  
  103.     fmt.Println("Part 1 - Lowest location is", lowest)
  104.  
  105.     var ranges []Range
  106.     for i := 0; i < len(seeds); i += 2 {
  107.         rangeStart := seeds[i]
  108.         rangeLength := seeds[i+1]
  109.         ranges = append(ranges, Range{Start: rangeStart, End: rangeStart + rangeLength - 1})
  110.     }
  111.     fmt.Println(ranges)
  112.  
  113.     // Do a reverse lookup, start from location = 0 and find if we have the seed, keep going until the first seed
  114.     var lowestReverse int
  115.     for location := 0; location >= 0; {
  116.         val := location
  117.         for i := len(mappings) - 1; i >= 0; i-- {
  118.             mapping := mappings[i]
  119.             for _, submapping := range mapping {
  120.                 if val-submapping.offset >= submapping.source && val-submapping.offset <= submapping.source+submapping.size {
  121.                     val -= submapping.offset
  122.                     break
  123.                 }
  124.             }
  125.         }
  126.  
  127.         for _, r := range ranges {
  128.             if r.InRange(val) {
  129.                 lowestReverse = location
  130.                 fmt.Println(location)
  131.                 break
  132.             }
  133.         }
  134.  
  135.         // If val is in one of the ranges break and print val, also assign lowest to current location iteration
  136.         if lowestReverse > 0 {
  137.             break
  138.         }
  139.         location += 1
  140.     }
  141.  
  142.     fmt.Println("Part 2 - Lowest location of any of the initial seeds is", lowestReverse)
  143. }
  144.  
  145. func StringToInt(s string) int {
  146.     i, err := strconv.Atoi(s)
  147.     if err != nil {
  148.         panic(err.Error())
  149.     }
  150.     return i
  151. }
  152.  
  153. // InRange checks if a number is in the given range
  154. func (r Range) InRange(num int) bool {
  155.     return num >= r.Start && num <= r.End
  156. }
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement