adventofcode/day10/main.go

57 lines
1.4 KiB
Go

package main
import (
"fmt"
"sort"
"math"
"strconv"
"strings"
//"io/ioutil"
)
func main() {
d, _ := ioutil.ReadFile("input")
strings := strings.Split(string(d),"\n")
ints := []int{0} // start with the connector
for _, s := range strings {
i, err := strconv.Atoi(s)
if err == nil {
ints = append(ints, i)
}
}
sort.Ints(ints)
counts := make(map[int]int)
ints = append(ints, ints[len(ints)-1]+3) // add the phone
// Part 1: Find the number of 1-gaps and 3-gaps
prev := 0
var gaps []int
for _, c := range ints[1:] {
d := c - prev
prev = c
counts[d] += 1
gaps = append(gaps, d)
}
fmt.Println("Part 1:", counts[1] * counts[3])
// Part 2: Find number of skippable adapters
skippable := 0
// Get number of possible skips. Dropping can only happen with multiple 1s in a row.
// Find run-lengths of 1s, add that -1 to the skippables.
run := 0
for _, g := range gaps {
if g == 1 {
run += 1
} else {
if run == 2 {
skippable += 2
} else {
skippable += (run-1)
}
run = 0
}
}
fmt.Println(gaps)
fmt.Println(skippable)
fmt.Println(ints)
fmt.Printf("Part 2: %f\n", math.Pow(2, float64(skippable))) // All possible combinations for n, according to wikipedia
}