package main import ( "bufio" "fmt" "os" "sort" "strconv" "time" ) func main() { start := time.Now() partOne() duration := time.Since(start) partTwo() duration2 := time.Since(start) fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration) } // 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) }