From 96b4a4badef420971a17a7b1cd736e73b74a5d86 Mon Sep 17 00:00:00 2001 From: Markus Witt Date: Wed, 14 Dec 2022 21:23:42 +0100 Subject: [PATCH 1/2] feat: solve 14 1 --- 14.go | 185 +++++++++++++++++++++++++++++++++++++++++++++++++++ 14_test.go | 23 +++++++ input/202214 | 129 +++++++++++++++++++++++++++++++++++ main.go | 1 + 4 files changed, 338 insertions(+) create mode 100644 14.go create mode 100644 14_test.go create mode 100644 input/202214 diff --git a/14.go b/14.go new file mode 100644 index 0000000..5fb5e77 --- /dev/null +++ b/14.go @@ -0,0 +1,185 @@ +package main + +import ( + "fmt" + "log" + "strconv" + "strings" +) + +type MapPoint int + +const ( + Air MapPoint = iota + Rock + Sand +) + +type AOC202214SandMap struct { + // Point[y][x] + Point map[int]map[int]MapPoint + MaxY int + MinX int + MaxX int +} + +func (sm *AOC202214SandMap) DrawMap() string { + rtn := []string{} + for y := 0; y <= sm.MaxY; y++ { + line := "" + for x := sm.MinX; x <= sm.MaxX; x++ { + if x == 500 && y == 0 { + line += "+" + continue + } + pt := sm.GetPoint(x, y) + if pt == Air { + line += "." + } else if pt == Rock { + line += "#" + } else if pt == Sand { + line += "o" + } + } + rtn = append(rtn, line) + } + + return strings.Join(rtn, "\n") +} + +func (sm *AOC202214SandMap) SetPoint(x, y int, pt MapPoint) { + if sm.Point[y] == nil { + sm.Point[y] = make(map[int]MapPoint, 0) + } + + sm.Point[y][x] = pt +} + +func (sm *AOC202214SandMap) GetPoint(x, y int) MapPoint { + if sm.Point[y] == nil { + sm.Point[y] = make(map[int]MapPoint) + return Air + } + + elem, ok := sm.Point[y][x] + if !ok { + return Air + } + return elem +} + +func (sm *AOC202214SandMap) AddSand() bool { + xPos, yPos := 500, 0 + for { + // check if it fell off + if yPos > sm.MaxY { + return false + } + + // check below + if sm.GetPoint(xPos, yPos+1) == Air { + yPos += 1 + continue + } + + // check below & left + if sm.GetPoint(xPos-1, yPos+1) == Air { + xPos -= 1 + yPos += 1 + continue + } + + if sm.GetPoint(xPos+1, yPos+1) == Air { + xPos += 1 + yPos += 1 + continue + } + + sm.SetPoint(xPos, yPos, Sand) + return true + } +} + +func (sm *AOC202214SandMap) ParseRocks(input string) error { + for _, line := range strings.Split(input, "\n") { + points := [][]int{} + for _, part := range strings.Split(line, " -> ") { + coord := strings.Split(part, ",") + coordX, err := strconv.Atoi(coord[0]) + if err != nil { + return fmt.Errorf("can't parse coordinate %s: %v", part, err) + } + coordY, err := strconv.Atoi(coord[1]) + if err != nil { + return fmt.Errorf("can't parse coordinate %s: %v", part, err) + } + if coordY > sm.MaxY { + sm.MaxY = coordY + } + if coordX < sm.MinX || sm.MinX == 0 { + sm.MinX = coordX + } + if coordX > sm.MaxX { + sm.MaxX = coordX + } + + points = append(points, []int{coordX, coordY}) + } + + for i := 0; i < len(points)-1; i++ { + startX, startY := points[i][0], points[i][1] + endX, endY := points[i+1][0], points[i+1][1] + + if startX == endX { + if startY > endY { + startY, endY = endY, startY + } + for y := startY; y <= endY; y++ { + sm.SetPoint(startX, y, Rock) + } + } else { + if startX > endX { + startX, endX = endX, startX + } + for x := startX; x <= endX; x++ { + sm.SetPoint(x, startY, Rock) + } + } + } + } + + return nil +} + +func AOC2022141Helper(input string) (int, error) { + sandMap := *&AOC202214SandMap{ + Point: make(map[int]map[int]MapPoint), + } + + err := sandMap.ParseRocks(input) + if err != nil { + return 0, fmt.Errorf("can't parse rocks: %v", err) + } + + log.Printf("Map before:\n%s", sandMap.DrawMap()) + + grains := 0 + for { + if !sandMap.AddSand() { + break + } + grains += 1 + } + + log.Printf("Map after:\n%s", sandMap.DrawMap()) + + return grains, nil +} + +func AOC2022141(input string) (string, error) { + grains, err := AOC2022141Helper(input) + if err != nil { + return "", err + } + return fmt.Sprintf("%d", grains), nil +} diff --git a/14_test.go b/14_test.go new file mode 100644 index 0000000..bfa002b --- /dev/null +++ b/14_test.go @@ -0,0 +1,23 @@ +package main + +import "testing" + +func TestAOC202214Part1(t *testing.T) { + test := struct { + input string + output int + }{ + input: `498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9`, + output: 24, + } + + got, err := AOC2022141Helper(test.input) + if err != nil { + t.Fatalf("AOC202214Helper() err: %v", err) + } + + if got != test.output { + t.Errorf("AOC202214Helper() missmatch:\nwant: %d\ngot: %d", test.output, got) + } +} diff --git a/input/202214 b/input/202214 new file mode 100644 index 0000000..d87dd0b --- /dev/null +++ b/input/202214 @@ -0,0 +1,129 @@ +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +484,132 -> 489,132 +501,15 -> 506,15 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +503,43 -> 514,43 -> 514,42 +497,13 -> 502,13 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +498,17 -> 503,17 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +467,81 -> 467,82 -> 487,82 -> 487,81 +481,135 -> 486,135 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +470,122 -> 470,123 -> 485,123 -> 485,122 +491,132 -> 496,132 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +491,17 -> 496,17 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +497,26 -> 501,26 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +491,22 -> 495,22 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +480,129 -> 485,129 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +477,132 -> 482,132 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +488,20 -> 492,20 +492,48 -> 506,48 -> 506,47 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +474,135 -> 479,135 +479,64 -> 484,64 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +486,64 -> 491,64 +490,66 -> 495,66 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +494,24 -> 498,24 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +503,43 -> 514,43 -> 514,42 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +488,135 -> 493,135 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +467,81 -> 467,82 -> 487,82 -> 487,81 +483,126 -> 488,126 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +483,66 -> 488,66 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +476,66 -> 481,66 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +494,15 -> 499,15 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +485,22 -> 489,22 +487,129 -> 492,129 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +488,24 -> 492,24 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +477,148 -> 477,138 -> 477,148 -> 479,148 -> 479,144 -> 479,148 -> 481,148 -> 481,146 -> 481,148 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +495,135 -> 500,135 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +482,24 -> 486,24 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +491,26 -> 495,26 +470,122 -> 470,123 -> 485,123 -> 485,122 +505,17 -> 510,17 +470,122 -> 470,123 -> 485,123 -> 485,122 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +485,26 -> 489,26 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +471,109 -> 471,112 -> 467,112 -> 467,120 -> 477,120 -> 477,112 -> 475,112 -> 475,109 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +485,85 -> 485,88 -> 482,88 -> 482,93 -> 499,93 -> 499,88 -> 491,88 -> 491,85 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +472,79 -> 472,73 -> 472,79 -> 474,79 -> 474,69 -> 474,79 -> 476,79 -> 476,77 -> 476,79 +510,29 -> 510,33 -> 506,33 -> 506,39 -> 517,39 -> 517,33 -> 512,33 -> 512,29 +467,81 -> 467,82 -> 487,82 -> 487,81 +479,26 -> 483,26 +492,48 -> 506,48 -> 506,47 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +482,62 -> 487,62 +476,161 -> 476,156 -> 476,161 -> 478,161 -> 478,160 -> 478,161 -> 480,161 -> 480,154 -> 480,161 -> 482,161 -> 482,155 -> 482,161 -> 484,161 -> 484,158 -> 484,161 -> 486,161 -> 486,159 -> 486,161 +490,51 -> 490,54 -> 486,54 -> 486,59 -> 499,59 -> 499,54 -> 492,54 -> 492,51 +457,106 -> 457,99 -> 457,106 -> 459,106 -> 459,98 -> 459,106 -> 461,106 -> 461,97 -> 461,106 -> 463,106 -> 463,105 -> 463,106 -> 465,106 -> 465,99 -> 465,106 -> 467,106 -> 467,103 -> 467,106 -> 469,106 -> 469,101 -> 469,106 -> 471,106 -> 471,103 -> 471,106 -> 473,106 -> 473,100 -> 473,106 \ No newline at end of file diff --git a/main.go b/main.go index adea626..05a0a85 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ func main() { 9: {AOC2022091, AOC2022092}, 10: {AOC2022101, AOC2022102}, 12: {AOC2022121, AOC2022122}, + 14: {AOC2022141}, } days := []int{} for day := range tasks { From c833365c6cc81bf555fe19c5f52b677f07f3beaa Mon Sep 17 00:00:00 2001 From: Markus Witt Date: Wed, 14 Dec 2022 22:14:22 +0100 Subject: [PATCH 2/2] feat: solve 14 2 --- 14.go | 70 ++++++++++++++++++++++++++++++++++++++++++++---------- 14_test.go | 20 ++++++++++++++++ main.go | 2 +- 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/14.go b/14.go index 5fb5e77..f454ccb 100644 --- a/14.go +++ b/14.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "log" "strconv" "strings" ) @@ -17,15 +16,20 @@ const ( type AOC202214SandMap struct { // Point[y][x] - Point map[int]map[int]MapPoint - MaxY int - MinX int - MaxX int + Point map[int]map[int]MapPoint + MaxY int + MinX int + MaxX int + Infinite bool } func (sm *AOC202214SandMap) DrawMap() string { rtn := []string{} - for y := 0; y <= sm.MaxY; y++ { + yMax := sm.MaxY + if !sm.Infinite { + yMax += 2 + } + for y := 0; y <= yMax; y++ { line := "" for x := sm.MinX; x <= sm.MaxX; x++ { if x == 500 && y == 0 { @@ -53,9 +57,18 @@ func (sm *AOC202214SandMap) SetPoint(x, y int, pt MapPoint) { } sm.Point[y][x] = pt + if x > sm.MaxX { + sm.MaxX = x + } else if x < sm.MinX { + sm.MinX = x + } } func (sm *AOC202214SandMap) GetPoint(x, y int) MapPoint { + if !sm.Infinite && y == sm.MaxY+2 { + return Rock + } + if sm.Point[y] == nil { sm.Point[y] = make(map[int]MapPoint) return Air @@ -71,9 +84,11 @@ func (sm *AOC202214SandMap) GetPoint(x, y int) MapPoint { func (sm *AOC202214SandMap) AddSand() bool { xPos, yPos := 500, 0 for { - // check if it fell off - if yPos > sm.MaxY { - return false + if sm.Infinite { + // check if it fell off + if yPos > sm.MaxY { + return false + } } // check below @@ -96,6 +111,9 @@ func (sm *AOC202214SandMap) AddSand() bool { } sm.SetPoint(xPos, yPos, Sand) + if xPos == 500 && yPos == 0 { + return false + } return true } } @@ -153,7 +171,8 @@ func (sm *AOC202214SandMap) ParseRocks(input string) error { func AOC2022141Helper(input string) (int, error) { sandMap := *&AOC202214SandMap{ - Point: make(map[int]map[int]MapPoint), + Point: make(map[int]map[int]MapPoint), + Infinite: true, } err := sandMap.ParseRocks(input) @@ -161,8 +180,6 @@ func AOC2022141Helper(input string) (int, error) { return 0, fmt.Errorf("can't parse rocks: %v", err) } - log.Printf("Map before:\n%s", sandMap.DrawMap()) - grains := 0 for { if !sandMap.AddSand() { @@ -171,7 +188,26 @@ func AOC2022141Helper(input string) (int, error) { grains += 1 } - log.Printf("Map after:\n%s", sandMap.DrawMap()) + return grains, nil +} + +func AOC2022142Helper(input string) (int, error) { + sandMap := *&AOC202214SandMap{ + Point: make(map[int]map[int]MapPoint), + } + + err := sandMap.ParseRocks(input) + if err != nil { + return 0, fmt.Errorf("can't parse rocks: %v", err) + } + + grains := 0 + for { + grains += 1 + if !sandMap.AddSand() { + break + } + } return grains, nil } @@ -183,3 +219,11 @@ func AOC2022141(input string) (string, error) { } return fmt.Sprintf("%d", grains), nil } + +func AOC2022142(input string) (string, error) { + grains, err := AOC2022142Helper(input) + if err != nil { + return "", err + } + return fmt.Sprintf("%d", grains), nil +} diff --git a/14_test.go b/14_test.go index bfa002b..1122259 100644 --- a/14_test.go +++ b/14_test.go @@ -21,3 +21,23 @@ func TestAOC202214Part1(t *testing.T) { t.Errorf("AOC202214Helper() missmatch:\nwant: %d\ngot: %d", test.output, got) } } + +func TestAOC202214Part2(t *testing.T) { + test := struct { + input string + output int + }{ + input: `498,4 -> 498,6 -> 496,6 +503,4 -> 502,4 -> 502,9 -> 494,9`, + output: 93, + } + + got, err := AOC2022142Helper(test.input) + if err != nil { + t.Fatalf("AOC202214Helper() err: %v", err) + } + + if got != test.output { + t.Errorf("AOC202214Helper() missmatch:\nwant: %d\ngot: %d", test.output, got) + } +} diff --git a/main.go b/main.go index 05a0a85..62eb7a0 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,7 @@ func main() { 9: {AOC2022091, AOC2022092}, 10: {AOC2022101, AOC2022102}, 12: {AOC2022121, AOC2022122}, - 14: {AOC2022141}, + 14: {AOC2022141, AOC2022142}, } days := []int{} for day := range tasks {