From 2d02a46cc3575b849b3fff07be135a1c726b222c Mon Sep 17 00:00:00 2001 From: Araozu Date: Thu, 2 May 2024 09:17:00 -0500 Subject: [PATCH] Day 10 part 1 --- main.go | 5 +++++ solutions/day10.go | 43 +++++++++++++++++++++++++++++++++++++++++++ solutions/utils.go | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 solutions/day10.go diff --git a/main.go b/main.go index 0c4b9b1..18bb94a 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,8 @@ import ( ) func main() { + fmt.Printf("===Problem===\t==solution==\t ===time===\n") + runAndBenchmark("01", "1", false, solutions.Day01Part01) runAndBenchmark("01", "2", false, solutions.Day01Part02) @@ -33,6 +35,9 @@ func main() { runAndBenchmark("09", "1", false, solutions.Day09Part01) runAndBenchmark("09", "2", false, solutions.Day09Part02) + + runAndBenchmark("10", "1", false, solutions.Day10Part01) + runAndBenchmark("10", "2", false, solutions.Day10Part02) } type execute func(bool) int diff --git a/solutions/day10.go b/solutions/day10.go new file mode 100644 index 0000000..b8705c8 --- /dev/null +++ b/solutions/day10.go @@ -0,0 +1,43 @@ +package solutions + +import ( + "fmt" + "os" + "slices" +) + +func Day10Part01(isTest bool) int { + numbers := ReadAndMapInt("10", isTest) + slices.Sort(numbers) + + // The highest jolt value + highestValue := numbers[len(numbers)-1] + // Add the built-in adapter jolt value + numbers = append(numbers, highestValue+3) + + jolt1Difference := 0 + jolt3Difference := 0 + currentJoltValue := 0 + + for _, value := range numbers { + difference := value - currentJoltValue + switch difference { + case 1: + jolt1Difference += 1 + case 2: + continue + case 3: + jolt3Difference += 1 + default: + fmt.Printf("Found an invalid jolt difference: %d %d %d \n", currentJoltValue, value, difference) + os.Exit(-1) + } + currentJoltValue = value + } + + return jolt1Difference * jolt3Difference +} + +func Day10Part02(isTest bool) int { + return -1 +} diff --git a/solutions/utils.go b/solutions/utils.go index eb2bc37..8ed0c36 100644 --- a/solutions/utils.go +++ b/solutions/utils.go @@ -1,11 +1,14 @@ package solutions import ( + "bufio" "fmt" + "log" "os" + "strconv" ) -const DIR = "/home/fernando/GolandProjects/advent-20/" +const DIR = "./" func ReadInput(day string, isTest bool) string { testStr := "" @@ -21,3 +24,37 @@ func ReadInput(day string, isTest bool) string { return string(bytes) } + +func ReadAndMapInt(day string, isTest bool) []int { + testStr := "" + if isTest { + testStr = "_test" + } + + filePath := DIR + "inputs" + testStr + "/" + day + ".txt" + + file, err := os.Open(filePath) + if err != nil { + log.Fatal(err) + } + defer file.Close() + + numbers := make([]int, 0) + + scanner := bufio.NewScanner(file) + + for scanner.Scan() { + valueStr := scanner.Text() + number, err := strconv.Atoi(valueStr) + if err != nil { + log.Fatal(err) + } + numbers = append(numbers, number) + } + + if err := scanner.Err(); err != nil { + log.Fatal(err) + } + + return numbers +}