2017 Complete!

This commit is contained in:
Brian Buller 2017-12-26 14:12:20 -06:00
parent e78eb5781e
commit 16ffec256f
9 changed files with 330 additions and 96 deletions

BIN
2016/day13/day13.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 638 KiB

BIN
2016/day22/day22.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 MiB

BIN
2016/day23/day23.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

BIN
2017/day21/2017day21.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 MiB

View File

@ -6,11 +6,13 @@ import (
"math" "math"
"os" "os"
"strings" "strings"
"time"
) )
var rules map[string]string var rules map[string]string
func main() { func main() {
numIters := 18
rules = make(map[string]string) rules = make(map[string]string)
inp := StdinToStrings() inp := StdinToStrings()
@ -19,27 +21,46 @@ func main() {
rules[pts[0]] = pts[2] rules[pts[0]] = pts[2]
rules[Flip(pts[0])] = pts[2] rules[Flip(pts[0])] = pts[2]
rules[Rotate(pts[0], 90)] = pts[2] rules[Rotate(pts[0], 90)] = pts[2]
rules[Flip(Rotate(pts[0], 90))] = pts[2]
rules[Rotate(pts[0], 180)] = pts[2] rules[Rotate(pts[0], 180)] = pts[2]
rules[Flip(Rotate(pts[0], 180))] = pts[2]
rules[Rotate(pts[0], 270)] = pts[2] rules[Rotate(pts[0], 270)] = pts[2]
rules[Flip(Rotate(pts[0], 270))] = pts[2]
} }
pattern := ".#./..#/###" pattern := ".#./..#/###"
for i := 0; i < 2; i++ { PrettyPrint(pattern)
Tick(pattern) for i := 0; i < numIters; i++ {
ClearScreen()
PrettyPrint(pattern)
time.Sleep(time.Second / 2)
pattern = Tick(pattern)
}
fmt.Println("Bits On", strings.Count(pattern, "#"))
}
func PrettyPrint(inp string) {
for _, v := range strings.Split(inp, "/") {
fmt.Println(v)
} }
/*
pattern = rules[pattern]
pts := SplitBlocks(pattern)
for i := range pts {
pts[i] = rules[pts[i]]
}
fmt.Println(pts)
fmt.Println(CombineBlocks(pts))
*/
} }
func Tick(pt string) string { func Tick(pt string) string {
fmt.Println(SplitBlocks(pt)) subPatterns := SplitBlocks(pt)
var tickSubs []string
for i := range subPatterns {
tickSubs = append(tickSubs, ApplyRule(subPatterns[i]))
}
return CombineBlocks(tickSubs)
}
func ApplyRule(inp string) string {
if v, ok := rules[inp]; ok {
return v
}
// This is an error.
fmt.Println("No rule for", inp)
os.Exit(1)
return "" return ""
} }
@ -55,23 +76,43 @@ func SplitBlocks(pt string) []string {
// 2x2 blocks // 2x2 blocks
blkSize = 2 blkSize = 2
} }
for j := 0; j < len(pts); j += blkSize { for j := 0; j < len(pts); j += blkSize {
for i := 0; i < len(pts[j]); i += blkSize { for i := 0; i < len(pts[j]); i += blkSize {
ret = append(ret, pts[j][i:i+blkSize]+"/"+pts[j+1][i:i+blkSize]) if blkSize == 2 {
ret = append(ret, pts[j][i:i+blkSize]+"/"+pts[j+1][i:i+blkSize])
} else {
ret = append(ret, pts[j][i:i+blkSize]+"/"+pts[j+1][i:i+blkSize]+"/"+pts[j+2][i:i+blkSize])
}
} }
} }
return ret return ret
} }
func CombineBlocks(pts []string) string { func CombineBlocks(pts []string) string {
var ret string var ret []string
sz := int(math.Sqrt(float64(len(pts)))) if len(pts) == 1 {
fmt.Println("Combining") return pts[0]
fmt.Println(sz)
//var j int
for i := 0; i < len(pts); i++ {
} }
return ret var subPts [][]string
for i := range pts {
subPts = append(subPts, strings.Split(pts[i], "/"))
}
subPtSz := len(subPts[0])
ptSize := int(math.Sqrt(float64(len(subPts) * subPtSz * subPtSz)))
w, h := (ptSize / subPtSz), (ptSize / subPtSz)
for i := 0; i < h; i++ {
st := i * w
for j := 0; j < subPtSz; j++ {
var bld string
for k := 0; k < w; k++ {
bld += subPts[st+k][j]
}
ret = append(ret, bld)
}
}
return strings.Join(ret, "/")
} }
func Flip(pt string) string { func Flip(pt string) string {
@ -115,6 +156,10 @@ func RevString(s string) string {
return string(runes) return string(runes)
} }
func ClearScreen() {
fmt.Println("\033[H\033[2J")
}
func StdinToStrings() []string { func StdinToStrings() []string {
var input []string var input []string
scanner := bufio.NewScanner(os.Stdin) scanner := bufio.NewScanner(os.Stdin)

View File

@ -10,106 +10,56 @@ import (
) )
var comps []Component var comps []Component
var strongest, best Bridge
// Greater than 907
func main() { func main() {
inp := StdinToStrings() inp := StdinToStrings()
for i := range inp { for i := range inp {
comps = append(comps, NewComponent(inp[i])) comps = append(comps, NewComponent(inp[i]))
} }
// Need to build chains of all permutations FindStrongest([]Component{NewComponent("0/0")})
var perms []Bridge fmt.Println("Strongest:\n", strongest)
for i := range comps {
if comps[i].Has(0) {
perms = append(perms, Bridge([]Component{comps[i]}))
}
}
changed := true FindBest([]Component{NewComponent("0/0")})
for changed { fmt.Println("Best:\n", best)
perms, changed = GetNextPerms(perms)
}
var highB Bridge
highest := 0
for _, b := range perms {
if b.Value() > highest {
highB = b
highest = b.Value()
}
}
fmt.Println(highB)
/*
for _, b := range perms {
tst := GetAllThatFit(b)
for i := range tst {
}
for tst != nil {
b = b.Add(*tst)
tst = GetNextBest(b)
}
fmt.Println(b)
}
*/
} }
func GetNextPerms(perms []Bridge) ([]Bridge, bool) { // Strongest, regardless of length
var ret []Bridge func FindStrongest(bridge Bridge) {
var changed bool if bridge.Strength() > strongest.Strength() {
for i := range perms { strongest = bridge
if !perms[i].IsDone() { }
allComps := GetAllThatFit(perms[i]) for i := range comps {
if len(allComps) == 0 { if comps[i].Has(bridge.Needs()) && !bridge.Has(comps[i]) {
ret = append(ret, perms[i].Add(NewComponent("0/0"))) FindStrongest(append(bridge, comps[i]))
}
for j := range allComps {
ret = append(ret, perms[i].Add(allComps[j]))
changed = true
}
} }
} }
return ret, changed
} }
func GetAllThatFit(b Bridge) []Component { // Longest and Strongest
var ret []Component func FindBest(bridge Bridge) {
port := b.Needs() if bridge.Length() > best.Length() {
for i := range comps { best = append([]Component{}, bridge...)
if comps[i].Has(port) && !b.Has(comps[i]) { } else if bridge.Length() == best.Length() {
ret = append(ret, comps[i]) if bridge.Strength() > best.Strength() {
best = bridge
} }
} }
return ret
}
func GetNextBest(b Bridge) *Component {
var best *Component
port := b.Needs()
for i := range comps { for i := range comps {
if comps[i].Has(port) && !b.Has(comps[i]) { if comps[i].Has(bridge.Needs()) && !bridge.Has(comps[i]) {
if best == nil || comps[i].Value() > best.Value() { FindBest(append(bridge, comps[i]))
best = &comps[i]
}
} }
} }
return best
} }
type Bridge []Component type Bridge []Component
func (b Bridge) IsDone() bool { func (b Bridge) Strength() int {
t := []Component(b)
c := t[len(t)-1]
return c.Value() == 0
}
func (b Bridge) Value() int {
var ret int var ret int
for _, v := range []Component(b) { for _, v := range []Component(b) {
ret += v.Value() ret += v.Strength()
} }
return ret return ret
} }
@ -143,12 +93,16 @@ func (b Bridge) Needs() int {
return c.Other(c1.Side2) return c.Other(c1.Side2)
} }
func (b Bridge) Length() int {
return len([]Component(b))
}
func (b Bridge) String() string { func (b Bridge) String() string {
ret := "[ " ret := "[ "
for _, v := range []Component(b) { for _, v := range []Component(b) {
ret += v.Name + " " ret += v.Name + " "
} }
return fmt.Sprint(ret+"] ", b.Value()) return fmt.Sprint(ret+"(", b.Strength(), ";", b.Length(), ") ]")
} }
type Component struct { type Component struct {
@ -169,7 +123,7 @@ func (c *Component) Equals(t Component) bool {
return c.Side1 == t.Side1 && c.Side2 == t.Side2 return c.Side1 == t.Side1 && c.Side2 == t.Side2
} }
func (c *Component) Value() int { func (c *Component) Strength() int {
return c.Side1 + c.Side2 return c.Side1 + c.Side2
} }
@ -184,6 +138,10 @@ func (c *Component) Other(v int) int {
return c.Side1 return c.Side1
} }
func (c *Component) String() string {
return c.Name
}
func Atoi(i string) int { func Atoi(i string) int {
var ret int var ret int
var err error var err error

147
2017/day25/day25.go Normal file
View File

@ -0,0 +1,147 @@
package main
import (
"bufio"
"errors"
"fmt"
"log"
"os"
"strconv"
"strings"
)
var currState string
var diagAfterSteps, currStep, currPos int
var allStates map[string]State
var tape map[int]bool
func main() {
inp := StdinToStrings()
tape = make(map[int]bool)
allStates = make(map[string]State)
// Parse 'Begin' line
pts := strings.Split(inp[0], " ")
st := pts[len(pts)-1]
currState = st[:len(st)-1]
pts = strings.Split(inp[1], " ")
// Parse 'Diagnostic' line
diagAfterSteps = Atoi(pts[5])
// Parse all of the 'state' lines
for i := 3; i < len(inp); i += 10 {
if state, err := NewState(inp[i:]); err == nil {
allStates[state.Name] = state
}
}
fmt.Println("Beginning in State", currState)
fmt.Println("Diagnostic After", diagAfterSteps)
for i := 0; i < diagAfterSteps; i++ {
//fmt.Printf("Steps: %d; State %s; Diag: %d; Tape: %s\n", i, currState, RunDiagnostic(), GetTapeString())
currState = allStates[currState].Process()
}
fmt.Println("Diagnostic Checksum:", RunDiagnostic())
}
func GetTapeString() string {
ret := "[ "
min, max := 0, 0
for k := range tape {
if k < min {
min = k
}
if k > max {
max = k
}
}
for i := min; i < max; i++ {
ret += fmt.Sprint("(", i, tape[i], ") ")
}
return ret + "]"
}
func RunDiagnostic() int {
var chk int
for _, v := range tape {
if v {
chk++
}
}
return chk
}
type State struct {
Name string
State1Ins Instruction
State0Ins Instruction
}
// NewState takes a slice of strings and returns a state built from the _next_ description found
func NewState(inp []string) (State, error) {
if len(inp) < 9 {
return State{}, errors.New("Invalid state instructions")
}
ret := State{}
// The first line should have the state name
ln := inp[0]
ret.Name = ln[len(ln)-2 : len(ln)-1]
ret.State0Ins = NewInstruction(inp[2:5])
ret.State1Ins = NewInstruction(inp[6:9])
return ret, nil
}
// Process makes the needed changes to the tape and returns the
// next state
func (s State) Process() string {
ins := s.State0Ins
if tape[currPos] {
ins = s.State1Ins
}
tape[currPos] = ins.WriteValue
currPos += ins.Move
return ins.NextState
}
type Instruction struct {
WriteValue bool
Move int
NextState string
}
func NewInstruction(inp []string) Instruction {
ret := Instruction{}
tmp := strings.Split(strings.TrimSpace(inp[0]), " ")
ret.WriteValue = (tmp[4][len(tmp[4])-2] == '1')
tmp = strings.Split(strings.TrimSpace(inp[1]), " ")
ret.Move = 1
if tmp[6] == "left." {
ret.Move = -1
}
tmp = strings.Split(strings.TrimSpace(inp[2]), " ")
state := tmp[len(tmp)-1]
ret.NextState = state[len(state)-2 : len(state)-1]
return ret
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}
func StdinToStrings() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}

62
2017/day25/input Normal file
View File

@ -0,0 +1,62 @@
Begin in state A.
Perform a diagnostic checksum after 12134527 steps.
In state A:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state B.
If the current value is 1:
- Write the value 0.
- Move one slot to the left.
- Continue with state C.
In state B:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state A.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state C.
In state C:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
If the current value is 1:
- Write the value 0.
- Move one slot to the left.
- Continue with state D.
In state D:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state E.
If the current value is 1:
- Write the value 1.
- Move one slot to the left.
- Continue with state C.
In state E:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state F.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
In state F:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state E.

22
2017/day25/testinput Normal file
View File

@ -0,0 +1,22 @@
Begin in state A.
Perform a diagnostic checksum after 6 steps.
In state A:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state B.
If the current value is 1:
- Write the value 0.
- Move one slot to the left.
- Continue with state B.
In state B:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state A.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.