From 000e241684289cb0c23499b6dcf4fdfd1e65eb95 Mon Sep 17 00:00:00 2001 From: James Griffin Date: Mon, 13 Dec 2021 18:58:17 +0000 Subject: [PATCH] Day 13 solution --- README.md | 15 + main.go | 3 + thirteen/input.txt | 1030 +++++++++++++++++++++++++++++++++++++++ thirteen/main.go | 26 + thirteen/manual.go | 183 +++++++ thirteen/manual_test.go | 58 +++ thirteen/scatch | 31 ++ thirteen/test_input.txt | 21 + 8 files changed, 1367 insertions(+) create mode 100644 thirteen/input.txt create mode 100644 thirteen/main.go create mode 100644 thirteen/manual.go create mode 100644 thirteen/manual_test.go create mode 100644 thirteen/scatch create mode 100644 thirteen/test_input.txt diff --git a/README.md b/README.md index 652cb3b..d3c6fe9 100644 --- a/README.md +++ b/README.md @@ -129,3 +129,18 @@ The solution for "twelve" is: Found 5157 paths through the cave system Found 144309 paths through the cave system ``` + +### Day thirteen + +```sh +$ ./aoc2021 --thirteen +The solution for "thirteen" is: +There are 837 dots after 1 fold +#### ### #### ## # # ## # # # # +# # # # # # # # # # # # # # +### # # # # ## # #### # # +# ### # # ## # # # # # # # +# # # # # # # # # # # # # +#### # #### ### # # ## # # ## +This is the activation code +``` diff --git a/main.go b/main.go index ce9abe2..5e026bb 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "unsupervised.ca/aoc2021/seven" "unsupervised.ca/aoc2021/six" "unsupervised.ca/aoc2021/ten" + "unsupervised.ca/aoc2021/thirteen" "unsupervised.ca/aoc2021/three" "unsupervised.ca/aoc2021/twelve" "unsupervised.ca/aoc2021/two" @@ -58,6 +59,8 @@ func main() { day = eleven.Init("eleven/input.txt") case "twelve": day = twelve.Init("twelve/input.txt") + case "thirteen": + day = thirteen.Init("thirteen/input.txt") default: fmt.Printf("%q does not have a solution.\n", flagParts[1]) help() diff --git a/thirteen/input.txt b/thirteen/input.txt new file mode 100644 index 0000000..559932f --- /dev/null +++ b/thirteen/input.txt @@ -0,0 +1,1030 @@ +522,329 +174,466 +242,714 +785,686 +378,596 +335,353 +229,44 +1160,19 +5,318 +489,63 +484,275 +818,774 +214,882 +1128,519 +1143,432 +627,217 +669,544 +306,191 +878,199 +579,376 +649,121 +1004,191 +1017,838 +519,329 +62,228 +249,733 +1275,108 +569,173 +1297,686 +23,187 +5,576 +823,462 +730,395 +1049,309 +843,35 +164,364 +448,373 +50,372 +74,723 +970,539 +569,33 +579,605 +1126,750 +801,742 +525,726 +293,844 +334,686 +21,262 +514,617 +1303,541 +816,136 +211,284 +283,833 +887,119 +800,823 +559,131 +1077,695 +579,206 +764,350 +492,120 +1103,803 +229,850 +582,785 +1124,442 +676,863 +897,350 +45,32 +126,777 +1256,621 +853,862 +550,771 +1299,723 +328,728 +907,737 +296,310 +110,592 +780,850 +822,72 +1081,44 +964,649 +443,817 +681,8 +853,638 +796,617 +1148,728 +878,647 +371,385 +826,619 +820,728 +413,481 +136,145 +585,871 +549,187 +554,133 +112,166 +1092,621 +299,14 +1002,245 +1078,100 +1198,186 +104,535 +710,427 +197,549 +1067,47 +1144,136 +643,115 +361,56 +811,68 +452,486 +146,700 +822,408 +641,462 +346,801 +1232,24 +966,749 +160,584 +1151,462 +853,222 +1241,751 +457,704 +378,298 +109,341 +160,310 +324,310 +1051,651 +502,413 +103,856 +997,649 +1061,544 +515,483 +1183,359 +537,558 +401,801 +579,768 +827,93 +63,406 +0,838 +1084,245 +1001,70 +1275,862 +328,812 +984,728 +1285,438 +167,208 +1268,374 +392,609 +145,51 +793,771 +1069,798 +186,442 +716,469 +604,371 +904,439 +217,887 +661,859 +284,374 +542,855 +465,347 +393,596 +607,670 +1210,627 +887,327 +1272,856 +682,117 +832,436 +552,421 +472,519 +1299,733 +939,385 +1054,19 +560,772 +1283,238 +248,551 +1260,820 +907,157 +341,21 +853,32 +728,109 +127,550 +768,186 +129,799 +1166,381 +440,858 +1198,166 +1215,543 +509,742 +783,359 +981,696 +0,306 +1141,564 +616,40 +653,738 +1086,535 +110,32 +492,225 +1093,887 +214,12 +1237,488 +155,765 +1310,52 +764,670 +847,551 +488,150 +378,227 +1247,406 +45,823 +1113,856 +187,483 +344,749 +1303,801 +1210,425 +42,822 +60,546 +811,765 +648,686 +542,75 +222,199 +912,467 +841,780 +243,399 +1263,670 +30,758 +1048,609 +115,593 +731,518 +50,591 +345,70 +723,651 +92,770 +17,371 +957,792 +350,135 +656,56 +465,858 +378,746 +174,428 +689,56 +895,289 +485,240 +567,498 +1165,152 +497,16 +1049,522 +1198,523 +232,497 +915,686 +50,599 +791,329 +589,309 +616,380 +1198,728 +190,19 +237,68 +47,670 +621,681 +422,322 +373,303 +288,745 +1200,592 +545,679 +1088,199 +1208,85 +335,93 +326,728 +743,396 +981,472 +1144,758 +110,750 +932,390 +1119,791 +964,351 +1193,784 +503,588 +180,28 +706,295 +253,241 +853,670 +756,627 +1034,876 +20,253 +633,296 +432,199 +970,114 +584,845 +1078,397 +293,96 +288,518 +740,287 +691,456 +344,642 +194,5 +713,75 +828,855 +72,221 +845,347 +169,635 +528,49 +1113,336 +447,539 +102,546 +1146,166 +1121,771 +306,703 +1226,852 +575,495 +527,135 +1181,224 +703,170 +1108,38 +428,649 +982,364 +1159,759 +512,302 +189,795 +641,544 +853,220 +494,856 +740,735 +1121,547 +413,413 +108,350 +87,371 +314,117 +659,278 +534,179 +144,890 +768,819 +872,862 +1081,421 +257,31 +964,693 +171,801 +766,123 +478,684 +669,208 +1071,137 +1290,880 +207,61 +594,298 +758,546 +870,690 +371,539 +1057,401 +845,858 +888,497 +913,397 +776,379 +1017,112 +129,560 +1150,584 +65,137 +52,183 +378,876 +880,535 +331,432 +870,280 +447,61 +102,473 +585,845 +1168,592 +1149,383 +679,707 +731,352 +1195,560 +1046,537 +1193,558 +904,455 +803,137 +982,504 +1277,56 +600,273 +69,845 +460,889 +89,577 +3,761 +5,240 +470,359 +986,584 +813,228 +269,411 +311,828 +497,635 +726,875 +771,301 +639,757 +224,535 +197,336 +591,182 +144,101 +748,49 +1280,115 +201,453 +1265,71 +909,801 +1238,225 +299,105 +1153,635 +17,523 +69,703 +85,686 +440,12 +361,504 +57,728 +621,838 +785,168 +403,157 +395,686 +624,136 +594,596 +1078,845 +197,793 +836,364 +218,672 +710,273 +490,550 +1111,851 +674,767 +846,875 +574,849 +1181,799 +254,486 +843,187 +838,4 +325,248 +1163,533 +405,96 +288,149 +627,117 +497,666 +325,24 +65,148 +21,150 +377,388 +910,486 +1201,553 +870,217 +1225,413 +1164,82 +850,889 +1158,481 +484,843 +120,179 +1165,483 +647,801 +499,826 +1004,703 +117,558 +341,686 +795,411 +1104,155 +176,427 +826,843 +662,14 +676,686 +855,550 +68,322 +206,494 +1299,171 +969,798 +363,704 +728,673 +1245,157 +1245,605 +1086,516 +1289,184 +815,392 +870,858 +1163,361 +1130,866 +436,96 +587,651 +127,359 +1277,325 +908,686 +1226,159 +550,571 +1185,161 +206,739 +54,621 +36,497 +977,325 +174,876 +949,350 +248,870 +1253,280 +870,815 +1250,544 +253,493 +947,704 +361,95 +585,703 +820,550 +710,467 +129,480 +1017,262 +683,217 +346,127 +992,716 +820,812 +582,221 +1265,519 +1022,380 +604,36 +1017,56 +7,93 +151,759 +1240,19 +1056,486 +1299,609 +760,571 +182,65 +483,93 +464,19 +1265,319 +1039,233 +960,135 +84,735 +142,592 +979,392 +607,170 +1250,425 +484,619 +5,688 +629,8 +676,31 +1251,528 +917,737 +1292,435 +1151,840 +1139,801 +1158,635 +152,413 +1134,436 +174,18 +1251,814 +189,771 +832,684 +184,144 +1049,585 +872,592 +60,21 +969,21 +838,890 +997,432 +78,870 +525,215 +1136,466 +233,616 +1078,794 +428,245 +495,392 +1265,480 +1049,421 +801,152 +1011,789 +919,264 +1049,697 +1113,38 +176,436 +561,891 +663,577 +488,584 +378,675 +989,325 +853,856 +1210,133 +1000,866 +408,805 +689,838 +1293,371 +197,215 +949,504 +574,89 +229,137 +313,432 +1260,303 +986,310 +716,298 +1014,584 +628,117 +1029,787 +1121,562 +211,560 +701,414 +1202,350 +847,343 +1265,862 +21,184 +1225,481 +654,56 +525,168 +472,228 +366,402 +969,686 +482,407 +145,499 +448,43 +818,120 +78,714 +949,544 +725,871 +525,110 +764,224 +760,459 +575,362 +840,359 +850,558 +654,838 +917,585 +621,280 +853,674 +176,210 +582,225 +1166,513 +647,317 +816,310 +611,763 +1263,224 +750,217 +985,248 +803,316 +65,309 +634,863 +932,18 +75,542 +780,44 +492,774 +1124,452 +962,852 +75,518 +467,35 +162,669 +266,574 +1210,469 +100,425 +962,583 +50,522 +129,663 +161,86 +306,852 +515,411 +110,526 +1181,543 +897,413 +1099,112 +499,68 +992,178 +189,123 +348,486 +190,427 +1061,96 +1268,624 +478,236 +677,296 +1164,700 +821,63 +796,890 +509,152 +798,592 +746,780 +510,101 +1069,544 +354,525 +514,4 +120,379 +904,551 +1125,780 +964,245 +276,667 +579,654 +785,320 +656,52 +1155,765 +309,70 +1084,627 +1215,95 +27,432 +146,252 +114,164 +109,789 +306,759 +706,523 +1200,526 +766,750 +1009,120 +440,79 +1307,761 +1220,780 +333,325 +164,812 +1019,332 +960,311 +328,504 +38,38 +800,549 +35,414 +1002,543 +73,499 +237,317 +1154,798 +945,453 +348,529 +1215,763 +826,499 +364,497 +498,843 +1029,107 +713,166 +276,504 +388,574 +190,145 +1017,504 +795,483 +795,35 +385,838 +785,768 +825,575 +478,458 +393,585 +710,621 +554,469 +698,270 +1093,7 +651,278 +1123,859 +100,627 +321,325 +713,523 +1184,117 +1232,714 +559,95 +1004,759 +1010,369 +306,255 +710,691 +373,367 +1200,32 +823,432 +42,744 +1096,79 +782,49 +631,483 +600,691 +628,777 +485,575 +160,136 +539,558 +783,135 +224,208 +1094,32 +232,546 +79,495 +1103,733 +820,166 +1113,32 +1241,255 +985,696 +915,651 +997,245 +253,353 +843,707 +110,302 +991,353 +648,880 +1136,876 +187,35 +45,71 +808,481 +364,397 +452,402 +423,119 +42,374 +766,144 +636,127 +726,845 +30,115 +661,35 +229,473 +691,480 +917,596 +1223,742 +731,376 +801,51 +843,411 +1245,869 +494,310 +569,861 +243,847 +514,890 +820,225 +1113,793 +776,627 +822,632 +11,723 +182,345 +863,803 +1139,689 +1084,649 +989,857 +467,483 +889,891 +1206,359 +395,880 +27,677 +713,411 +465,410 +701,527 +1057,849 +169,319 +669,96 +1128,345 +949,462 +966,642 +1183,135 +432,247 +915,880 +1277,838 +448,739 +467,187 +832,210 +472,381 +785,374 +564,780 +402,686 +1155,129 +62,666 +557,186 +668,220 +436,798 +393,309 +184,141 +465,123 +783,759 +619,414 +361,544 +1155,541 +1069,721 +530,44 +910,109 +924,871 +460,558 +440,751 +621,351 +191,791 +85,693 +749,278 +164,728 +822,744 +226,627 +1292,693 +256,427 +447,355 +982,812 +1260,522 +346,245 +129,112 +1119,551 +1250,21 +1241,143 +955,441 +1272,38 +525,231 +326,364 +1181,768 +276,743 +1052,453 +169,778 +1268,822 +1277,880 +299,432 +1285,786 +472,4 +129,208 +45,862 +874,544 +933,388 +760,435 +209,336 +627,656 +214,751 +585,639 +1195,593 +1126,141 +1181,208 +773,784 +233,423 +113,388 +23,259 +1126,359 +1235,799 +1136,428 +1245,137 +33,632 +257,143 +662,238 +152,635 +1057,541 +607,224 +557,708 +1238,673 +25,438 +1067,399 +351,411 +385,411 +539,294 +964,543 +940,821 +1081,473 +493,383 +1141,857 +155,516 +982,728 +129,224 +1198,763 +816,856 +959,411 +1084,693 +1053,143 +723,243 +1292,106 +731,654 +115,301 +760,771 +171,541 +569,693 +145,395 +197,668 +554,425 +771,558 +528,117 +653,47 +59,80 +567,396 +1303,353 +782,777 +147,361 +109,679 +42,72 +7,801 +348,408 +994,578 +12,562 +701,476 +115,560 +455,43 +329,696 +1166,890 +145,843 +619,862 +721,309 +1073,68 +1181,480 +338,316 +1004,135 +72,109 +880,473 +842,442 +1280,779 +546,497 +395,651 +1053,79 +1168,190 +436,126 +519,565 +84,852 +222,834 +60,544 +728,225 +933,170 +330,366 +1064,805 +457,856 +277,388 +124,520 +1260,599 +171,93 +1068,831 +59,528 +1195,301 +155,541 +508,194 +1305,576 +1207,38 +691,414 +262,609 +962,529 +54,19 +796,4 +365,441 +913,79 +100,469 +95,763 +1053,563 +390,515 +1168,302 +142,190 +802,252 +1113,215 +490,344 +1253,483 +552,546 +517,562 +550,359 +326,166 +1166,823 +542,708 +500,850 +512,592 +817,383 +166,758 +1141,654 +166,408 +768,131 +316,316 +589,148 +490,812 +1010,777 + +fold along x=655 +fold along y=447 +fold along x=327 +fold along y=223 +fold along x=163 +fold along y=111 +fold along x=81 +fold along y=55 +fold along x=40 +fold along y=27 +fold along y=13 +fold along y=6 \ No newline at end of file diff --git a/thirteen/main.go b/thirteen/main.go new file mode 100644 index 0000000..8a124e0 --- /dev/null +++ b/thirteen/main.go @@ -0,0 +1,26 @@ +package thirteen + +import "fmt" + +type Thirteen struct { + manual manual +} + +func Init(filepath string) *Thirteen { + thirteen := &Thirteen{ + manual: manual{}, + } + + thirteen.manual.load(filepath) + return thirteen +} + +func (d *Thirteen) Answer() string { + _, dots := foldGrid(d.manual.folds[0], d.manual.dots) + return fmt.Sprintf("There are %d dots after 1 fold", dots) +} + +func (d *Thirteen) FollowUp() string { + d.manual.fold() + return "This is the activation code" +} diff --git a/thirteen/manual.go b/thirteen/manual.go new file mode 100644 index 0000000..e8a52f4 --- /dev/null +++ b/thirteen/manual.go @@ -0,0 +1,183 @@ +package thirteen + +import ( + "bufio" + "fmt" + "os" +) + +type axis rune + +const ( + x axis = 'x' + y axis = 'y' +) + +type fold struct { + axis axis + index int +} + +type manual struct { + dots map[int]map[int]bool + folds []fold +} + +func (m *manual) load(filename string) error { + file, err := os.Open(filename) + if err != nil { + return err + } + defer file.Close() + scanner := bufio.NewScanner(file) + + m.dots = map[int]map[int]bool{} + m.folds = []fold{} + scanningDots := true + for scanner.Scan() { + if scanner.Text() == "" { + scanningDots = false + continue + } + + if scanningDots { + var dotX, dotY int + if count, err := fmt.Sscanf(scanner.Text(), "%d,%d", &dotX, &dotY); count != 2 || err != nil { + return fmt.Errorf("expected 2 values, found %d: %w", count, err) + } + + if m.dots[dotX] == nil { + m.dots[dotX] = map[int]bool{} + } + + m.dots[dotX][dotY] = true + } else { + var foldAxis axis + var foldIndex int + + if count, err := fmt.Sscanf(scanner.Text(), "fold along %c=%d", &foldAxis, &foldIndex); count != 2 || err != nil { + return fmt.Errorf("scanning %q, expected 2 values, found %d: %w", scanner.Text(), count, err) + } + + m.folds = append(m.folds, fold{ + axis: foldAxis, + index: foldIndex, + }) + } + } + return nil +} + +func gridSize(grid map[int]map[int]bool) (xSize, ySize int) { + for x, col := range grid { + if x > xSize { + xSize = x + } + for y := range col { + if y > ySize { + ySize = y + } + } + } + + // // Increment Size to reflect 0 indexed arrays + xSize++ + ySize++ + + return xSize, ySize +} + +func countDots(grid map[int]map[int]bool) int { + dots := 0 + for _, col := range grid { + for _, v := range col { + if v { + dots++ + } + } + } + + return dots +} + +func (m *manual) fold() { + grid := m.dots + + for _, inst := range m.folds { + grid, _ = foldGrid(inst, grid) + } + + printGrid(grid) +} + +func foldGrid(inst fold, grid map[int]map[int]bool) (map[int]map[int]bool, int) { + newGrid := map[int]map[int]bool{} + + maxX, maxY := gridSize(grid) + if inst.axis == x { + // Copy the first rows + for copyX := 0; copyX < inst.index; copyX++ { + if newGrid[copyX] == nil { + newGrid[copyX] = map[int]bool{} + } + + for copyY := 0; copyY < maxY; copyY++ { + newGrid[copyX][copyY] = grid[copyX][copyY] + } + } + + // Copy the folded values + for foldX := inst.index + 1; foldX < maxX; foldX++ { + targetX := inst.index - (foldX - inst.index) + for targetY := 0; targetY < maxY; targetY++ { + if grid[foldX][targetY] { + newGrid[targetX][targetY] = grid[foldX][targetY] + } + } + } + } else { + // initialize all X maps + for k := range grid { + newGrid[k] = map[int]bool{} + } + + // Copy the first rows + for copyY := 0; copyY < inst.index; copyY++ { + for copyX := 0; copyX < maxX; copyX++ { + if newGrid[copyX] == nil { + newGrid[copyX] = map[int]bool{} + } + + newGrid[copyX][copyY] = grid[copyX][copyY] + } + } + + // Copy the folded values + for foldY := inst.index + 1; foldY < maxY; foldY++ { + targetY := inst.index - (foldY - inst.index) + for targetX := 0; targetX < maxX; targetX++ { + if grid[targetX][foldY] { + newGrid[targetX][targetY] = grid[targetX][foldY] + } + } + } + } + + return newGrid, countDots(newGrid) +} + +func printGrid(grid map[int]map[int]bool) { + maxX, maxY := gridSize(grid) + + for y := 0; y < maxY; y++ { + row := "" + for x := 0; x < maxX; x++ { + if grid[x][y] { + row += "#" + } else { + row += " " + } + } + fmt.Println(row) + } +} diff --git a/thirteen/manual_test.go b/thirteen/manual_test.go new file mode 100644 index 0000000..9ec6956 --- /dev/null +++ b/thirteen/manual_test.go @@ -0,0 +1,58 @@ +package thirteen + +import ( + "fmt" + "testing" +) + +func Test_read(t *testing.T) { + m := manual{} + if err := m.load("test_input.txt"); err != nil { + t.Log(err) + t.FailNow() + } + + dots := countDots(m.dots) + folds := len(m.folds) + + if dots != 18 { + t.Logf("Expected 18 dots, found %d", dots) + t.Fail() + } + + if folds != 2 { + t.Logf("Expected 2 folds, found %d", folds) + t.Fail() + } + + x, y := gridSize(m.dots) + + if x != 11 || y != 15 { + t.Logf("Expected 11x15, found %dx%d", x, y) + t.Fail() + } +} + +func Test_fold(t *testing.T) { + m := manual{} + m.load("test_input.txt") + + printGrid(m.dots) + fmt.Println("") + grid, dots := foldGrid(m.folds[0], m.dots) + + printGrid(grid) + if dots != 17 { + t.Logf("Expected 17 dots after fold, found %d", dots) + t.Fail() + } + + grid, dots = foldGrid(m.folds[1], grid) + + fmt.Println("") + printGrid(grid) + if dots != 16 { + t.Logf("Expected 16 dots after fold, found %d", dots) + t.Fail() + } +} diff --git a/thirteen/scatch b/thirteen/scatch new file mode 100644 index 0000000..9bb02ca --- /dev/null +++ b/thirteen/scatch @@ -0,0 +1,31 @@ +#.##..#..#. +#...#...... +......#...# +#...#...... +.#.#..#.### +........... +........... + +#..#..#..#. +....#.#...# +....#...... +##....#.##. +...#....#.# +........... +........... + +...#..#..#. +....#...... +........... +#.......... +...#....#.# +........... +........... +........... +........... +........... +.#....#.##. +....#...... +......#...# +#.......... +#.#........ \ No newline at end of file diff --git a/thirteen/test_input.txt b/thirteen/test_input.txt new file mode 100644 index 0000000..32a8563 --- /dev/null +++ b/thirteen/test_input.txt @@ -0,0 +1,21 @@ +6,10 +0,14 +9,10 +0,3 +10,4 +4,11 +6,0 +6,12 +4,1 +0,13 +10,12 +3,4 +3,0 +8,4 +1,10 +2,14 +8,10 +9,0 + +fold along y=7 +fold along x=5 \ No newline at end of file