Day 10 part 1
This commit is contained in:
parent
5a8cd089c9
commit
2d02a46cc3
5
main.go
5
main.go
@ -7,6 +7,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
fmt.Printf("===Problem===\t==solution==\t ===time===\n")
|
||||||
|
|
||||||
runAndBenchmark("01", "1", false, solutions.Day01Part01)
|
runAndBenchmark("01", "1", false, solutions.Day01Part01)
|
||||||
runAndBenchmark("01", "2", false, solutions.Day01Part02)
|
runAndBenchmark("01", "2", false, solutions.Day01Part02)
|
||||||
|
|
||||||
@ -33,6 +35,9 @@ func main() {
|
|||||||
|
|
||||||
runAndBenchmark("09", "1", false, solutions.Day09Part01)
|
runAndBenchmark("09", "1", false, solutions.Day09Part01)
|
||||||
runAndBenchmark("09", "2", false, solutions.Day09Part02)
|
runAndBenchmark("09", "2", false, solutions.Day09Part02)
|
||||||
|
|
||||||
|
runAndBenchmark("10", "1", false, solutions.Day10Part01)
|
||||||
|
runAndBenchmark("10", "2", false, solutions.Day10Part02)
|
||||||
}
|
}
|
||||||
|
|
||||||
type execute func(bool) int
|
type execute func(bool) int
|
||||||
|
43
solutions/day10.go
Normal file
43
solutions/day10.go
Normal file
@ -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
|
||||||
|
}
|
@ -1,11 +1,14 @@
|
|||||||
package solutions
|
package solutions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DIR = "/home/fernando/GolandProjects/advent-20/"
|
const DIR = "./"
|
||||||
|
|
||||||
func ReadInput(day string, isTest bool) string {
|
func ReadInput(day string, isTest bool) string {
|
||||||
testStr := ""
|
testStr := ""
|
||||||
@ -21,3 +24,37 @@ func ReadInput(day string, isTest bool) string {
|
|||||||
|
|
||||||
return string(bytes)
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user