From 56cd171588f5a245c79d438014abda52faf2e2b2 Mon Sep 17 00:00:00 2001 From: David Ashby Date: Fri, 11 Dec 2020 17:30:31 -0500 Subject: [PATCH] day ten, part one works, part two doesn't --- 10/main.go | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 10/main.go diff --git a/10/main.go b/10/main.go new file mode 100644 index 0000000..5eb8196 --- /dev/null +++ b/10/main.go @@ -0,0 +1,117 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "sort" + "strconv" +) + +func main() { + partOne() + partTwo() +} + +// Find a chain that uses all of your adapters to connect the charging outlet to your device's built-in adapter +// and count the joltage differences between the charging outlet, the adapters, and your device. +// What is the number of 1-jolt differences multiplied by the number of 3-jolt differences? + +func partOne() { + f, _ := os.Open("input") + reader := bufio.NewReader(f) + scanner := bufio.NewScanner(reader) + + // wall + adapters := []int{0} + + // load + for scanner.Scan() { + line := scanner.Text() + i, _ := strconv.Atoi(line) + adapters = append(adapters, i) + } + + // sort + sort.Slice(adapters, func(i, j int) bool { + return adapters[i] < adapters[j] + }) + + // device + adapters = append(adapters, adapters[len(adapters)-1]+3) + + one := 0 + three := 0 + + // count gaps + for i, j := range adapters { + if i < len(adapters)-1 { + if adapters[i+1]-j == 3 { + three = three + 1 + } else if adapters[i+1]-j == 1 { + one = one + 1 + } + } + } + + fmt.Println(three * one) +} + +// You glance back down at your bag and try to remember why you brought so many adapters; there must be more than a trillion valid ways to arrange them! +// Surely, there must be an efficient way to count the arrangements. +// What is the total number of distinct ways you can arrange the adapters to connect the charging outlet to your device? + +func partTwo() { + f, _ := os.Open("input") + reader := bufio.NewReader(f) + scanner := bufio.NewScanner(reader) + + // wall + adapters := []int{0} + + for scanner.Scan() { + line := scanner.Text() + i, _ := strconv.Atoi(line) + adapters = append(adapters, i) + } + + // sort + sort.Slice(adapters, func(i, j int) bool { + return adapters[i] < adapters[j] + }) + + // device + adapters = append(adapters, adapters[len(adapters)-1]+3) + + combinations := 1 + ones := 0 + three := 0 + + // count gaps + for i, j := range adapters { + if i < len(adapters)-1 { + if adapters[i+1]-j == 1 { + // small steps... + ones = ones + 1 + } else if adapters[i+1]-j == 3 { + combinations = combinations + 1 + if ones == 2 { + combinations = combinations * 1 + } + if ones == 3 { + combinations = combinations * 2 + } + if ones == 4 { + combinations = combinations * 4 + } + if ones == 5 { + combinations = combinations * 7 + } + ones = 0 + three = three + 1 + } + } + } + + fmt.Println(adapters, combinations) +}