Advertisement
Guest User

Advent Of Code 2024 Day 2

a guest
Dec 2nd, 2024
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.17 KB | Source Code | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "log"
  7.     "os"
  8.     "slices"
  9.     "strings"
  10. )
  11.  
  12. func main() {
  13.     puzzleInput := parseFile("input.txt")
  14.     fmt.Println("PartOne: ", partOne(puzzleInput))
  15.     fmt.Println("PartTwo: ", partTwo(puzzleInput))
  16. }
  17.  
  18. func parseFile(fileName string) [][]int {
  19.     file, err := os.Open("input.txt")
  20.     if err != nil {
  21.         log.Fatal(err)
  22.     }
  23.     defer file.Close()
  24.  
  25.     scanner := bufio.NewScanner(file)
  26.  
  27.     reports := make([][]int, 0, 1000)
  28.  
  29.     for scanner.Scan() {
  30.         fields := strings.Fields(scanner.Text())
  31.         var integerFields []int
  32.         for _, s := range fields {
  33.             integerFields = append(integerFields, stringToInt(s))
  34.         }
  35.         reports = append(reports, integerFields)
  36.     }
  37.  
  38.     return reports
  39. }
  40.  
  41. func partOne(reports [][]int) (safeReports int) {
  42.     for _, report := range reports {
  43.         if isValid(report) {
  44.             safeReports++
  45.         }
  46.     }
  47.     return safeReports
  48. }
  49.  
  50. func partTwo(reports [][]int) (safeReports int) {
  51.     for _, report := range reports {
  52.         if isValid(report) {
  53.             safeReports++
  54.         } else if canBeMadeValid(report) {
  55.             safeReports++
  56.         }
  57.     }
  58.     return safeReports
  59. }
  60.  
  61. func isValid(report []int) bool {
  62.     isSafe := true
  63.  
  64.     for i := 1; i < len(report); i++ {
  65.         diff := abs(report[i] - report[i-1])
  66.  
  67.         if diff > 3 || diff < 1 {
  68.             isSafe = false
  69.         }
  70.     }
  71.  
  72.     if !isStrictlyIncreasing(report) && !isStrictlyDecreasing(report) {
  73.         isSafe = false
  74.     }
  75.  
  76.     return isSafe
  77. }
  78.  
  79. func isStrictlyIncreasing(report []int) bool {
  80.     for i := 1; i < len(report); i++ {
  81.         if report[i-1] >= report[i] {
  82.             return false
  83.         }
  84.     }
  85.     return true
  86. }
  87.  
  88. func isStrictlyDecreasing(report []int) bool {
  89.     for i := 1; i < len(report); i++ {
  90.         if report[i] >= report[i-1] {
  91.             return false
  92.         }
  93.     }
  94.     return true
  95. }
  96.  
  97. func canBeMadeValid(report []int) bool {
  98.     for i := range report {
  99.         newSlice := make([]int, len(report))
  100.         copy(newSlice, report)
  101.  
  102.         slices.Delete(newSlice, i, i+1)
  103.         newSlice = newSlice[:len(newSlice)-1]
  104.  
  105.         if isValid(newSlice) {
  106.             return true
  107.         }
  108.     }
  109.     return false
  110. }
  111.  
  112. func abs(a int) int {
  113.     if a < 0 {
  114.         return -1 * a
  115.     }
  116.     return a
  117. }
  118.  
  119. func stringToInt(str string) (num int) {
  120.     for _, r := range str {
  121.         num *= 10
  122.         num += int(r - '0')
  123.     }
  124.  
  125.     return num
  126. }
  127.  
Tags: aoc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement