diff --git a/README.md b/README.md index be20c90..9033054 100644 --- a/README.md +++ b/README.md @@ -22,5 +22,5 @@ _in which I attempt to use a different language every day_ 8. Noulith 9. Picat 10. Hy -11. ?? +11. GNU Octave 12. ?? \ No newline at end of file diff --git a/d11/README.md b/d11/README.md index 71484ff..1fe3156 100644 --- a/d11/README.md +++ b/d11/README.md @@ -2,10 +2,14 @@ | | | |-------------------|-------------------| -| Language | ?? | -| Input Method | ?? | -| Output Method | ?? | +| Language | GNU Octave | +| Input Method | File | +| Output Method | stdout | ## Running +```sh +octave-cli day11.m +``` -## Notes \ No newline at end of file +## Notes +This is slower than it should be, but it's 2am and I need to go to bed \ No newline at end of file diff --git a/d11/day11.m b/d11/day11.m new file mode 100644 index 0000000..5e89f39 --- /dev/null +++ b/d11/day11.m @@ -0,0 +1,82 @@ +file = char (argv ()(1)); + +f = fopen (file, "r"); +graph = containers.Map (); +nz = 0; +nodes = {}; +while ((line = fgetl (f)) != -1) + split = strsplit (line, {":", " "}); + device = char (split(1)); + connections = split(2:end); + graph(device) = connections; + nz += length (connections); + nodes = union (nodes, split); +endwhile +fclose (f); + +dim = length (nodes); +global adjMat; +adjMat = spalloc (dim, dim, nz); +for i = 1:dim + node = char (nodes(i)); + if (!isKey(graph, node)) + continue + endif + + adjNodes = graph(node); + for adj = adjNodes + j = find (ismember (nodes, char (adj))); + adjMat(i, j) = 1; + endfor +endfor + +youKey = find (ismember (nodes, "you")); +outKey = find (ismember (nodes, "out")); +svrKey = find (ismember (nodes, "svr")); + +global fftKey; +global dacKey; +fftKey = find (ismember (nodes, "fft")); +dacKey = find (ismember (nodes, "dac")); + +function pathCount = numPaths (start, goal) + global adjMat; + persistent numPathsMemo = memoize (@numPaths); + pOneMemo.CacheSize = 1e6; + + pathCount = 1; + if (start != goal) + pathCount = 0; + adjNodes = find (adjMat(start,:)); + for node = adjNodes + pathCount += numPathsMemo (node, goal); + endfor + endif +endfunction + +function pathCount = partTwo (start, goal, foundDac, foundFft) + global adjMat; + global fftKey; + global dacKey; + persistent pTwoMemo = memoize (@partTwo); + pTwoMemo.CacheSize = 1e6; + + pathCount = foundDac && foundFft; + if (start != goal) + if (start == fftKey) + foundFft = 1; + elseif (start == dacKey) + foundDac = 1; + endif + + pathCount = 0; + adjNodes = find (adjMat(start,:)); + for node = adjNodes + pathCount += pTwoMemo (node, goal, foundDac, foundFft); + endfor + endif +endfunction + +partOneAnswer = numPaths (youKey, outKey); +partTwoAnswer = partTwo (svrKey, outKey, 0, 0); +printf ("%d\n%d\n", partOneAnswer, partTwoAnswer);