Solve seven
This commit is contained in:
parent
7d8df0db21
commit
909ff098c7
1 changed files with 27 additions and 31 deletions
|
|
@ -2,10 +2,8 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -15,18 +13,27 @@ func check(err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type beam int
|
|
||||||
type beams struct {
|
type beams struct {
|
||||||
b map[beam]struct{}
|
list map[int]int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *beams) add(pos beam) {
|
func newBeams() beams {
|
||||||
b.b[pos] = {}
|
var b beams
|
||||||
|
b.list = make(map[int]int)
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *beams) add(pos int) {
|
||||||
|
b.list[pos]++
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *beams) del(pos int) {
|
||||||
|
delete(b.list, pos)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
file, err := os.Open("five.txt")
|
file, err := os.Open("seven.txt")
|
||||||
check(err)
|
check(err)
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
|
@ -36,35 +43,24 @@ func main() {
|
||||||
text := scanner.Text()
|
text := scanner.Text()
|
||||||
|
|
||||||
start := strings.IndexRune(text, 'S')
|
start := strings.IndexRune(text, 'S')
|
||||||
beams.add(beam(start))
|
beamSet := make(map[int]int)
|
||||||
|
beamSet[start] = 1
|
||||||
|
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
}
|
text := scanner.Text()
|
||||||
db, err := parse(scanner)
|
for beam, nways := range beamSet {
|
||||||
|
if text[beam] == '^' {
|
||||||
check(err)
|
beamSet[beam-1] += nways
|
||||||
|
beamSet[beam+1] += nways
|
||||||
nRaw := len(db.ranges)
|
delete(beamSet, beam)
|
||||||
|
}
|
||||||
for {
|
|
||||||
before, after := db.collapse()
|
|
||||||
|
|
||||||
fmt.Printf("Collapsed %d to %d ranges\n", before, after)
|
|
||||||
|
|
||||||
if before == after {
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
potentialNFresh := 0
|
timelines := 0
|
||||||
for _, r := range db.ranges {
|
for _, nways := range beamSet {
|
||||||
potentialNFresh += r.nInRange()
|
timelines += nways
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(
|
fmt.Printf("N splits: %d\n", timelines)
|
||||||
"N raw %d and consolidated %d. %d potential fresh ingredients.\n",
|
|
||||||
nRaw,
|
|
||||||
len(db.ranges),
|
|
||||||
potentialNFresh,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue