day ten, part one works, part two doesn't
This commit is contained in:
parent
0b851158dc
commit
56cd171588
117
10/main.go
Normal file
117
10/main.go
Normal file
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user