Day 11 Cleanup and streamline
This commit is contained in:
parent
ea8d72e5d0
commit
a66482a257
BIN
day11/day11
BIN
day11/day11
Binary file not shown.
@ -11,18 +11,51 @@ import (
|
||||
"github.com/fatih/color"
|
||||
)
|
||||
|
||||
// x < 60
|
||||
|
||||
var elevatorFloor int
|
||||
var floorInv [][]string
|
||||
var componentRegistry []string
|
||||
// This is a purely mathematical solution to this problem
|
||||
// We don't care about what the elements are.
|
||||
// Loop on each floor and consider:
|
||||
// 1: We have less than 3 items on the floor
|
||||
// So move them all up; 1 total move
|
||||
// 2: We have more than 3 items on the floor
|
||||
// So move two up and one back down; 2 total moves
|
||||
|
||||
// Setting visual to true will output what's happening
|
||||
var visual bool
|
||||
|
||||
// This code requires oxford commas in the input
|
||||
func main() {
|
||||
input := stdinToStringSlice()
|
||||
var numMoves int
|
||||
if !visual {
|
||||
var lvlItmCnts []int
|
||||
for i := range input {
|
||||
lvlItmCnts = append(lvlItmCnts, strings.Count(input[i], "generator")+strings.Count(input[i], "microchip"))
|
||||
}
|
||||
for i := 0; i < 3; i++ {
|
||||
for lvlItmCnts[i] > 0 {
|
||||
if lvlItmCnts[i] < 3 {
|
||||
lvlItmCnts[i+1] = lvlItmCnts[i+1] + lvlItmCnts[i]
|
||||
lvlItmCnts[i] = 0
|
||||
numMoves++
|
||||
} else {
|
||||
lvlItmCnts[i] = lvlItmCnts[i] - 1
|
||||
lvlItmCnts[i+1] = lvlItmCnts[i+1] + 1
|
||||
numMoves = numMoves + 2
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Visual mode spits a lot more stuff out, so it has
|
||||
// to track a lot more things...
|
||||
numMoves = VisualMode(input)
|
||||
}
|
||||
fmt.Println("Number of moves:", numMoves)
|
||||
}
|
||||
|
||||
func VisualMode(input []string) int {
|
||||
// This code requires oxford commas in the input :)
|
||||
var elevatorFloor int
|
||||
var floorInv [][]string
|
||||
var componentRegistry []string
|
||||
for i := range input {
|
||||
listIdx := strings.Index(input[i], "a ")
|
||||
if listIdx == -1 {
|
||||
@ -33,6 +66,7 @@ func main() {
|
||||
floorInv = append(floorInv, strings.Split(input[i], ", "))
|
||||
}
|
||||
for i := range floorInv {
|
||||
elevatorFloor = i
|
||||
for j := range floorInv[i] {
|
||||
tp := "G"
|
||||
if strings.Contains(floorInv[i][j], "microchip") {
|
||||
@ -70,19 +104,19 @@ func main() {
|
||||
}
|
||||
if visual {
|
||||
ClearScreen()
|
||||
PrintScreen()
|
||||
PrintScreen(floorInv, elevatorFloor, componentRegistry)
|
||||
time.Sleep(time.Millisecond * 250)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("Number of moves:", numMoves)
|
||||
return numMoves
|
||||
}
|
||||
|
||||
func ClearScreen() {
|
||||
fmt.Print("\033[H\033[2J")
|
||||
}
|
||||
|
||||
func PrintScreen() {
|
||||
func PrintScreen(floorInv [][]string, elevatorFloor int, componentRegistry []string) {
|
||||
c := color.New(color.FgCyan)
|
||||
w := color.New(color.FgWhite)
|
||||
for i := len(floorInv); i > 0; i-- {
|
||||
|
Loading…
Reference in New Issue
Block a user