diff --git a/day11-add/day11-add b/day11-add/day11-add new file mode 100755 index 0000000..aeb1375 Binary files /dev/null and b/day11-add/day11-add differ diff --git a/day11-add/input b/day11-add/input new file mode 100644 index 0000000..aca037e --- /dev/null +++ b/day11-add/input @@ -0,0 +1,4 @@ +The first floor contains a thulium generator, a thulium-compatible microchip, a plutonium generator, and a strontium generator. +The second floor contains a plutonium-compatible microchip, and a strontium-compatible microchip. +The third floor contains a promethium generator, a promethium-compatible microchip, a ruthenium generator, and a ruthenium-compatible microchip. +The fourth floor contains nothing relevant. diff --git a/day11-add/main.go b/day11-add/main.go new file mode 100644 index 0000000..8e37f85 --- /dev/null +++ b/day11-add/main.go @@ -0,0 +1,176 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + "os" + "strconv" + "strings" + + "github.com/fatih/color" + termbox "github.com/nsf/termbox-go" +) + +var elevatorFloor int +var floorInv [][]string +var componentRegistry []string +var cursorPosX, cursorPosY int +var selectItems []string + +// This code requires oxford commas in the input +func main() { + input := stdinToStringSlice() + for i := range input { + listIdx := strings.Index(input[i], "a ") + if listIdx == -1 { + floorInv = append(floorInv, []string{}) + continue + } + input[i] = input[i][listIdx:] + floorInv = append(floorInv, strings.Split(input[i], ", ")) + } + for i := range floorInv { + for j := range floorInv[i] { + tp := "G" + if strings.Contains(floorInv[i][j], "microchip") { + tp = "M" + } + floorInv[i][j] = strings.TrimPrefix(floorInv[i][j], "a ") + floorInv[i][j] = strings.TrimPrefix(floorInv[i][j], "and a ") + ele := strings.ToUpper(floorInv[i][j][:2]) + floorInv[i][j] = ele + ":" + tp + componentRegistry = append(componentRegistry, floorInv[i][j]) + } + } + + err := termbox.Init() + if err != nil { + fmt.Println("Error initializing termbox") + os.Exit(1) + } + defer termbox.Close() + + eventQueue := make(chan termbox.Event) + go func() { + for { + eventQueue <- termbox.PollEvent() + } + }() + + PrintScreen() + var doQuit bool + for { + select { + case ev := <-eventQueue: + if ev.Type == termbox.EventKey { + switch { + case ev.Key == termbox.KeyArrowLeft: + if cursorPosX > 0 { + cursorPosX-- + } + case ev.Key == termbox.KeyArrowRight: + if cursorPosX < len(componentRegistry)-1 { + cursorPosX++ + } + case ev.Ch == 'q': + doQuit = true + } + } + ClearScreen() + PrintScreen() + } + if doQuit { + break + } + } + //fmt.Println("Registered Components: ", componentRegistry) + + // a microchip without it's generator cannot be on the same floor + // as a different microchip that _does_ have it's generator + + // Look at what we have on the lowest floor with items + // For each microchip, find it's generator + // if it's on this floor, take them both up a floor + // If we have microchips, see where their generators are + // If they are with us, take both and move up + // If they are one floor above, +} + +func SelectItem() error { + return nil +} + +func FloorHasComponent(flr int, cmp string) bool { + for i := range floorInv[flr] { + if floorInv[flr][i] == cmp { + return true + } + } + return false +} + +func MoveElevator(dir int, take []string) error { + if len(take) < 1 || len(take) > 2 { + return errors.New("Error tried to move with invalid number of items: " + itoa(len(take))) + } + // Are the requested items on the current floor? + // If so, move them to the floor we're going to + for j := range take { + if FloorHasComponent(elevatorFloor, take[j]) { + // TODO: Move the component + floorInv[elevatorFloor+dir] = append(floorInv[elevatorFloor+dir], take[j]) + } else { + return errors.New("Elevator tried to take " + take[j] + " but it's not on floor " + itoa(elevatorFloor+1)) + } + } + return nil +} + +func ClearScreen() { + fmt.Print("\033[H\033[2J") +} + +func PrintScreen() { + c := color.New(color.FgCyan) + w := color.New(color.FgWhite) + g := color.New(color.FgGreen) + g.Add(color.Bold) + for i := len(floorInv); i > 0; i-- { + c.Printf("F%d ", i) + if elevatorFloor == i-1 { + c.Print("E ") + } else { + w.Print(". ") + } + for cr := range componentRegistry { + out := ". " + for j := range floorInv[i-1] { + if floorInv[i-1][j] == componentRegistry[cr] { + out = componentRegistry[cr] + " " + break + } + } + if elevatorFloor == i-1 && cursorPosX == cr { + g.Print(out) + } else { + w.Print(out) + } + } + w.Println("") + } + w.Println("") +} + +func stdinToStringSlice() []string { + var input []string + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + input = append(input, scanner.Text()) + } + return input +} + +func itoa(i int) string { + return strconv.Itoa(i) +} diff --git a/day11-add/testinput b/day11-add/testinput new file mode 100644 index 0000000..a4499e3 --- /dev/null +++ b/day11-add/testinput @@ -0,0 +1,4 @@ +The first floor contains a hydrogen-compatible microchip, and a lithium-compatible microchip. +The second floor contains a hydrogen generator. +The third floor contains a lithium generator. +The fourth floor contains nothing relevant. diff --git a/day11/day11 b/day11/day11 new file mode 100755 index 0000000..4a68fac Binary files /dev/null and b/day11/day11 differ diff --git a/day11/input-part1 b/day11/input-part1 new file mode 100644 index 0000000..aca037e --- /dev/null +++ b/day11/input-part1 @@ -0,0 +1,4 @@ +The first floor contains a thulium generator, a thulium-compatible microchip, a plutonium generator, and a strontium generator. +The second floor contains a plutonium-compatible microchip, and a strontium-compatible microchip. +The third floor contains a promethium generator, a promethium-compatible microchip, a ruthenium generator, and a ruthenium-compatible microchip. +The fourth floor contains nothing relevant. diff --git a/day11/input-part2 b/day11/input-part2 new file mode 100644 index 0000000..6c14f3b --- /dev/null +++ b/day11/input-part2 @@ -0,0 +1,4 @@ +The first floor contains a elerium generator, a elerium-compatible microchip, a dilithium generator, a dilitium-compatible microchip, a thulium generator, a thulium-compatible microchip, a plutonium generator, and a strontium generator. +The second floor contains a plutonium-compatible microchip, and a strontium-compatible microchip. +The third floor contains a promethium generator, a promethium-compatible microchip, a ruthenium generator, and a ruthenium-compatible microchip. +The fourth floor contains nothing relevant. diff --git a/day11/main.go b/day11/main.go new file mode 100644 index 0000000..4400122 --- /dev/null +++ b/day11/main.go @@ -0,0 +1,123 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" + "time" + + "github.com/fatih/color" +) + +// x < 60 + +var elevatorFloor int +var floorInv [][]string +var componentRegistry []string + +// Setting visual to true will output what's happening +var visual bool + +// This code requires oxford commas in the input +func main() { + input := stdinToStringSlice() + for i := range input { + listIdx := strings.Index(input[i], "a ") + if listIdx == -1 { + floorInv = append(floorInv, []string{}) + continue + } + input[i] = input[i][listIdx:] + floorInv = append(floorInv, strings.Split(input[i], ", ")) + } + for i := range floorInv { + for j := range floorInv[i] { + tp := "G" + if strings.Contains(floorInv[i][j], "microchip") { + tp = "M" + } + floorInv[i][j] = strings.TrimPrefix(floorInv[i][j], "a ") + floorInv[i][j] = strings.TrimPrefix(floorInv[i][j], "and a ") + ele := strings.ToUpper(floorInv[i][j][:2]) + floorInv[i][j] = ele + ":" + tp + componentRegistry = append(componentRegistry, floorInv[i][j]) + } + } + var numMoves int + var pt1, pt2 string + for i := 0; i < len(floorInv)-1; i++ { + for len(floorInv[i]) > 1 { + if len(floorInv[i]) > 1 { + // If there are exactly two items on the floor, move them up + if len(floorInv[i]) == 2 { + pt1, pt2, floorInv[i] = floorInv[i][0], floorInv[i][1], floorInv[i][2:] + floorInv[i+1] = append(floorInv[i+1], pt1, pt2) + numMoves++ + } else if len(floorInv[i]) > 2 { + // If more than two, move one up, but add 2 to the move + // To simulate, moving two up then one back down + pt1, floorInv[i] = floorInv[i][0], floorInv[i][1:] + floorInv[i+1] = append(floorInv[i+1], pt1) + numMoves = numMoves + 2 + } + } else { + // Only one left on floor, move it up + pt1, floorInv[i] = floorInv[i][0], []string{} + floorInv[i+1] = append(floorInv[i+1], pt1, pt2) + numMoves++ + } + if visual { + ClearScreen() + PrintScreen() + time.Sleep(time.Millisecond * 250) + } + } + } + fmt.Println("Number of moves:", numMoves) +} + +func ClearScreen() { + fmt.Print("\033[H\033[2J") +} + +func PrintScreen() { + c := color.New(color.FgCyan) + w := color.New(color.FgWhite) + for i := len(floorInv); i > 0; i-- { + c.Printf("F%d ", i) + if elevatorFloor == i-1 { + c.Print("E ") + } else { + w.Print(". ") + } + for cr := range componentRegistry { + var fnd bool + for j := range floorInv[i-1] { + if floorInv[i-1][j] == componentRegistry[cr] { + fnd = true + fmt.Print(componentRegistry[cr] + " ") + break + } + } + if !fnd { + fmt.Print(". ") + } + } + fmt.Println() + } +} + +func stdinToStringSlice() []string { + var input []string + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + input = append(input, scanner.Text()) + } + return input +} + +func itoa(i int) string { + return strconv.Itoa(i) +}