diff --git a/day17/day17.go b/day17/day17.go index 3cfd13a..f27b8b4 100644 --- a/day17/day17.go +++ b/day17/day17.go @@ -3,6 +3,7 @@ package main import ( "AOC2022/helper" "fmt" + "os" ) type Field struct { @@ -17,59 +18,44 @@ type Block struct { } func main() { - //args := os.Args[1:] - lines := helper.ReadTextFile("day17/input") + args := os.Args[1:] + lines := helper.ReadTextFile(args[0]) field := Field{createField(), Block{}, 0} field.addBlock() fieldCopy := Field{createField(), Block{}, 0} fieldCopy.addBlock() heightForBlocks := part1(fieldCopy, lines) currentPrefix := 0 - currentCycle := len(lines[0]) + currentCycle := 1 finished := false for !finished { if currentPrefix < currentCycle { currentPrefix++ - fmt.Println(currentPrefix) } else { - fmt.Println(currentCycle) - currentCycle += len(lines[0]) + currentPrefix = 0 + currentCycle++ } fieldCopy = Field{createField(), Block{}, 0} fieldCopy.addBlock() - finished = checkCycle(currentPrefix, currentCycle, fieldCopy, lines) + finished = checkCycle(currentPrefix, currentCycle, heightForBlocks) } fmt.Println(currentCycle) - part2(field, lines) + part2(currentPrefix, currentCycle, field, lines) } -func checkCycle(prefix, cycle int, field Field, lines []string) bool { - currentJetMove := 0 - values := make(map[int]int) - for field.spawnedBlocks != 4*cycle+prefix { - field.move(rune(lines[0][currentJetMove])) - if (field.spawnedBlocks-prefix)%cycle == 0 { - values[field.spawnedBlocks] = len(field.field) - field.getLineWithHighestRock() - } - currentJetMove++ - if currentJetMove == len(lines[0]) { - currentJetMove = 0 - } - } - cycleHeight := values[cycle+prefix] - values[prefix] - for i := prefix; i < 3*cycle+prefix; i += cycle { - if !(values[i+cycle]-values[i] == cycleHeight) { +func checkCycle(prefix, cycle int, heightForBlocks map[int]int) bool { + cycleHeight := heightForBlocks[cycle+prefix] - heightForBlocks[prefix] + for i := prefix; i < len(heightForBlocks); i += cycle { + if !(heightForBlocks[i+cycle]-heightForBlocks[i] == cycleHeight) { return false } } return true } -func part2(field Field, lines []string) { - prefix := 25 - cycle := 35 +func part2(prefix, cycle int, field Field, lines []string) { suffix := (1000000000000 - prefix) % cycle prefixHeight := 0 cycleHeight := 0 @@ -95,16 +81,22 @@ func part2(field Field, lines []string) { } func part1(field Field, lines []string) (heightForBlocks map[int]int) { + heightForBlocks = make(map[int]int) currentJetMove := 0 - for field.spawnedBlocks != 2023 { + solutionPart1 := 0 + for field.spawnedBlocks != 50000 { field.move(rune(lines[0][currentJetMove])) + if field.spawnedBlocks == 2023 { + solutionPart1 = len(field.field) - field.getLineWithHighestRock() + } heightForBlocks[field.spawnedBlocks] = len(field.field) - field.getLineWithHighestRock() currentJetMove++ if currentJetMove == len(lines[0]) { currentJetMove = 0 } } - fmt.Printf("%v : %v \n", field.spawnedBlocks, (len(field.field) - field.getLineWithHighestRock())) + fmt.Printf("%v : %v \n", field.spawnedBlocks, solutionPart1) + return } func getNewBlock(spawnedBlocks int) Block {