Last active
December 6, 2020 03:15
-
-
Save Trinitek/74878463d93117791f53d4dbf5cf4229 to your computer and use it in GitHub Desktop.
Advent of Code 2019
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Main() | |
| { | |
| var masses = Input.Split("\n", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Select(i => int.Parse(i)); | |
| masses.Sum(m => GetRequiredFuel(m)).Dump(); | |
| } | |
| int GetRequiredFuel(int mass) | |
| { | |
| int fuelMass = (mass / 3) - 2; | |
| if (fuelMass <= 0) | |
| { | |
| return 0; | |
| } | |
| else | |
| { | |
| return GetRequiredFuel(fuelMass) + fuelMass; | |
| } | |
| } | |
| public readonly string Input = @" | |
| 145866 | |
| 101641 | |
| 71590 | |
| 95922 | |
| 140188 | |
| 72376 | |
| 55476 | |
| 85697 | |
| 98456 | |
| 93928 | |
| 106896 | |
| 115715 | |
| 124364 | |
| 72957 | |
| 108532 | |
| 85330 | |
| 147386 | |
| 54323 | |
| 145384 | |
| 104665 | |
| 117539 | |
| 51151 | |
| 94139 | |
| 124784 | |
| 106624 | |
| 127034 | |
| 121847 | |
| 87388 | |
| 85778 | |
| 146850 | |
| 62744 | |
| 125351 | |
| 62382 | |
| 92694 | |
| 92848 | |
| 73291 | |
| 85971 | |
| 69358 | |
| 148674 | |
| 115957 | |
| 127865 | |
| 63695 | |
| 82372 | |
| 98268 | |
| 115743 | |
| 139867 | |
| 124701 | |
| 95280 | |
| 58252 | |
| 140192 | |
| 148478 | |
| 133129 | |
| 129392 | |
| 62828 | |
| 117987 | |
| 117070 | |
| 134493 | |
| 123419 | |
| 148890 | |
| 53183 | |
| 143135 | |
| 99892 | |
| 133565 | |
| 103335 | |
| 126562 | |
| 56527 | |
| 148819 | |
| 134626 | |
| 62805 | |
| 145167 | |
| 117147 | |
| 75263 | |
| 89470 | |
| 64792 | |
| 145233 | |
| 67654 | |
| 67642 | |
| 103778 | |
| 90355 | |
| 80176 | |
| 128655 | |
| 96818 | |
| 78409 | |
| 53704 | |
| 74910 | |
| 57051 | |
| 108317 | |
| 84280 | |
| 95293 | |
| 126315 | |
| 63765 | |
| 84776 | |
| 91836 | |
| 57134 | |
| 122127 | |
| 95625 | |
| 136598 | |
| 59997 | |
| 104865 | |
| 86457 | |
| "; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Main() | |
| { | |
| Program = Input.Trim().Split(",", StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).Select(i => int.Parse(i)).ToArray(); | |
| Program[12].Dump(); | |
| Program[2].Dump(); | |
| Program.Take(10).Dump(); | |
| Execute(56, 96, suppressInfo: true); | |
| //Program[0].Dump(); | |
| Program.Take(10).Dump(); | |
| (100 * 56 + 96).Dump(); | |
| } | |
| void Execute(int noun, int verb, bool suppressInfo) | |
| { | |
| Program[1] = noun; | |
| Program[2] = verb; | |
| void Info(string comment, int? xPos = default) { if (!suppressInfo) $"{(xPos ?? Pos):0000} : {Program[xPos ?? Pos]:00} | {comment}".Dump(); } | |
| bool running = true; | |
| while (running) | |
| { | |
| int opcode = Program[Pos]; | |
| switch (opcode) | |
| { | |
| case 1: | |
| { | |
| int xPos = Pos; | |
| int aSource = Program[++Pos]; | |
| int bSource = Program[++Pos]; | |
| int target = Program[++Pos]; | |
| Program[target] = Program[aSource] + Program[bSource]; | |
| Pos++; | |
| Info($"[{aSource}] + [{bSource}] => [{target}]", xPos); | |
| break; | |
| } | |
| case 2: | |
| { | |
| int xPos = Pos; | |
| int aSource = Program[++Pos]; | |
| int bSource = Program[++Pos]; | |
| int target = Program[++Pos]; | |
| Program[target] = Program[aSource] * Program[bSource]; | |
| Pos++; | |
| Info($"[{aSource}] + [{bSource}] => [{target}]", xPos); | |
| break; | |
| } | |
| case 99: | |
| running = false; | |
| Info("Halt"); | |
| break; | |
| default: | |
| running = false; | |
| Info("Unknown opcode"); | |
| break; | |
| } | |
| } | |
| } | |
| public int[] Program { get; set; } | |
| public int Pos { get; set; } | |
| public readonly string Input = @"1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,5,19,23,2,6,23,27,1,27,5,31,2,9,31,35,1,5,35,39,2,6,39,43,2,6,43,47,1,5,47,51,2,9,51,55,1,5,55,59,1,10,59,63,1,63,6,67,1,9,67,71,1,71,6,75,1,75,13,79,2,79,13,83,2,9,83,87,1,87,5,91,1,9,91,95,2,10,95,99,1,5,99,103,1,103,9,107,1,13,107,111,2,111,10,115,1,115,5,119,2,13,119,123,1,9,123,127,1,5,127,131,2,131,6,135,1,135,5,139,1,139,6,143,1,143,6,147,1,2,147,151,1,151,5,0,99,2,14,0,0"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Main() | |
| { | |
| var wires = Input.Trim().Split("\n", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Select(i => ParseNode(i)); | |
| //wires.First().ToLines().ToList().Dump(); | |
| //var l1 = new Line(new (4, 2), new (4, 9)); | |
| //var l2 = new Line(new (2, 3), new (12, 3)); | |
| // | |
| //l1.Dump(); | |
| //l2.Dump(); | |
| // | |
| //l1.Intersection(l1).Dump(); | |
| //l1.Intersection(l2).Dump(); | |
| // | |
| //var l3 = new Line(new (8, 9), new (8, 16)); | |
| // | |
| //l1.Intersection(l3).Dump(); | |
| //l2.Intersection(l3).Dump(); | |
| var wLines = wires.Select(w => w.ToLines()); | |
| //wLines.Dump(); | |
| var first = wLines.First().ToArray(); | |
| var second = wLines.Last().ToArray(); | |
| var intersections = Intersections(first, second) | |
| .OrderBy(i => i.ix.Distance) | |
| .Where(i => i.ix != Node.Origin) | |
| .Dump(); | |
| //first.TakeWhile(l => l != intersection.l1).Append(new (intersection.l1.A, intersection.ix)).Sum(l => l.Steps).Dump(); | |
| //second.TakeWhile(l => l != intersection.l2).Append(new (intersection.l2.A, intersection.ix)).Sum(l => l.Steps).Dump(); | |
| //second.TakeWhile(l => l != intersection.l2).Append(intersection.l2).Sum(l => l.Steps).Dump(); | |
| intersections.Select(i => | |
| first.TakeWhile(l => l != i.l1).Append(new (i.l1.A, i.ix)).Sum(l => l.Steps) | |
| + second.TakeWhile(l => l != i.l2).Append(new (i.l2.A, i.ix)).Sum(l => l.Steps)) | |
| .OrderBy(i => i) | |
| .Dump(); | |
| //new Line(new (2, 10), new (2, 40)).Dump(); | |
| //var ixFirst = intersections.First(); | |
| //first.TakeWhile(l => l != ixFirst.l1).Append(new (ixFirst.l1.A, ixFirst.ix)).Dump(); | |
| } | |
| IEnumerable<(Line l1, Line l2, Node ix)> Intersections(Line[] first, Line[] second) | |
| { | |
| for (int i1 = 0; i1 < first.Length; i1++) | |
| { | |
| for (int i2 = 0; i2 < second.Length; i2++) | |
| { | |
| var l1 = first[i1]; | |
| var l2 = second[i2]; | |
| if (l1.Intersection(l2) is Node n) | |
| { | |
| yield return (l1, l2, n); | |
| } | |
| } | |
| } | |
| } | |
| public static class NodeExt | |
| { | |
| public static IEnumerable<Line> ToLines(this IEnumerable<Node> nodes) | |
| { | |
| var e = nodes.GetEnumerator(); | |
| e.MoveNext(); | |
| Node prev = e.Current; | |
| while (e.MoveNext()) | |
| { | |
| yield return new Line(prev, e.Current); | |
| prev = e.Current; | |
| } | |
| } | |
| } | |
| public record Node(int X, int Y) | |
| { | |
| public static readonly Node Origin = new (0, 0); | |
| public int Distance => Math.Abs(X) + Math.Abs(Y); | |
| } | |
| public record Line(Node A, Node B) | |
| { | |
| public bool IsHoriz => A.Y == B.Y; | |
| public int Steps => IsHoriz | |
| ? Math.Max(A.X, B.X) - Math.Min(A.X, B.X) | |
| : Math.Max(A.Y, B.Y) - Math.Min(A.Y, B.Y); | |
| public Node Intersection(Line other) => Intersection(this, other); | |
| private static Node Intersection(Line l1, Line l2) | |
| { | |
| if (!(l1.IsHoriz ^ l2.IsHoriz)) | |
| { | |
| return null; | |
| } | |
| var horiz = l1.IsHoriz ? l1 : l2; | |
| var vert = l1.IsHoriz ? l2 : l1; | |
| int MaxX(Line l) => Math.Max(l.A.X, l.B.X); | |
| int MinX(Line l) => Math.Min(l.A.X, l.B.X); | |
| int MaxY(Line l) => Math.Max(l.A.Y, l.B.Y); | |
| int MinY(Line l) => Math.Min(l.A.Y, l.B.Y); | |
| if (vert.A.X < MinX(horiz) || vert.A.X > MaxX(horiz)) | |
| { | |
| return null; | |
| } | |
| if (horiz.A.Y < MinY(vert) || horiz.A.Y > MaxY(vert)) | |
| { | |
| return null; | |
| } | |
| int x = vert.A.X; | |
| int y = horiz.A.Y; | |
| return new(x, y); | |
| } | |
| } | |
| IEnumerable<Node> ParseNode(string raw) | |
| { | |
| var points = raw.Trim().Split(','); | |
| Node node = Node.Origin; | |
| yield return node; | |
| foreach (var p in points) | |
| { | |
| int steps = int.Parse(Regex.Match(p, @"\d+").Value); | |
| switch (p[0]) | |
| { | |
| case 'R': node = new (node.X + steps, node.Y); break; | |
| case 'L': node = new (node.X - steps, node.Y); break; | |
| case 'U': node = new (node.X, node.Y + steps); break; | |
| case 'D': node = new (node.X, node.Y - steps); break; | |
| default: | |
| throw new Exception($"Unknown direction {p[0]}"); | |
| } | |
| yield return node; | |
| } | |
| } | |
| public readonly string Input = @" | |
| R998,U502,R895,D288,R416,U107,R492,U303,R719,D601,R783,D154,L236,U913,R833,D329,R28,D759,L270,D549,L245,U653,L851,U676,L211,D949,R980,U314,L897,U764,R149,D214,L195,D907,R534,D446,R362,D6,L246,D851,L25,U925,L334,U673,L998,U581,R783,U912,R53,D694,L441,U411,L908,D756,R946,D522,L77,U468,R816,D555,L194,D707,R97,D622,R99,D265,L590,U573,R132,D183,L969,D207,L90,D331,R88,D606,L315,U343,R546,U460,L826,D427,L232,U117,R125,U309,R433,D53,R148,U116,L437,U339,L288,D879,L52,D630,R201,D517,L341,U178,R94,U636,L759,D598,L278,U332,R192,U463,L325,U850,L200,U810,L686,U249,L226,D297,R915,D117,R56,D59,R760,U445,R184,U918,R173,D903,R212,D868,L88,D798,L829,U835,L563,U19,R480,D989,R529,D834,R515,U964,L876,D294,R778,D551,L457,D458,R150,D698,R956,D781,L310,D948,R50,U56,R98,U348,L254,U614,L654,D359,R632,D994,L701,D615,R64,D507,R668,D583,L687,D902,L564,D214,R930,D331,L212,U943,R559,U886,R590,D805,R426,U669,L141,D233,L573,D682,L931,U267,R117,D900,L944,U667,L838,D374,L406,U856,R987,D870,R716,D593,R596,D654,R653,U120,L666,U145,R490,D629,R172,D881,L808,D324,R956,D532,L475,U165,L503,U361,R208,U323,R568,D876,R663,D11,L839,D67,R499,U75,L643,U954,R94,D418,R761,D842,L213,D616,L785,D42,R707,D343,L513,D480,L531,D890,L899,D2,L30,D188,R32,U588,R480,U33,R849,U443,L666,U117,L13,D974,L453,U93,R960,D369,R332,D61,L17,U557,R818,D744,L124,U916,L454,D572,R451,D29,R711,D134,R481,U366,L327,U132,L819,U839,R485,U941,R224,U531,R688,U561,R958,D899,L315,U824,L408,D941,R517,D163,L878,U28,R767,D798,R227 | |
| L1009,U399,R373,U980,L48,U638,R725,U775,R714,D530,L887,D576,L682,D940,L371,D621,L342,D482,R676,D445,R752,U119,L361,D444,L769,D854,L874,U259,R332,U218,R866,U28,L342,D233,L958,U649,R998,U262,L8,D863,L283,D449,L73,D438,L516,D54,R964,D981,R338,U332,L761,U704,L705,D468,L115,U834,R367,D156,R480,U27,R846,U73,R846,D720,R811,D466,L407,U928,R816,U50,R90,D893,L930,D833,L159,D972,L823,U868,R689,D558,L777,D13,R844,D8,L168,U956,L111,D462,L667,U559,L839,U503,R906,D838,R83,D323,L782,U588,R599,D233,L700,U679,L51,U779,L110,D260,L201,U992,L43,D557,L628,D875,L201,U535,L246,D976,L546,D22,R600,D301,L542,D41,R532,U316,L765,D310,L666,D369,R853,U684,L457,U816,L667,U758,R798,U959,R893,D185,L842,U168,R68,D348,R394,D296,R966,D511,L319,U717,L57,U129,R843,U439,L744,D870,L162,D991,R77,D565,R494,U601,L851,U748,L96,U124,L379,D446,L882,U371,R133,U820,L935,D704,L670,D911,L182,U138,R844,U926,L552,D716,L849,U624,R723,U117,R252,D737,L216,U796,R156,U322,R812,D390,L50,D493,L665,U314,L584,U798,L11,U524,R171,U837,R981,U32,L277,U650,L865,U28,R399,U908,R652,D543,L779,D406,L839,D198,L190,D319,L776,U752,R383,D884,R385,D682,R729,D163,R252,U533,L690,D767,R533,D147,R366,U716,R548,U171,R932,U720,L9,D39,R895,U850,L276,D988,L528,U551,L262,D480,L275,D567,R70,D599,L814,U876,R120,U93,L565,U795,L278,D41,R695,D693,R208,U272,L923,U498,R238,U268,L244,U278,R965,U395,R990,U329,L478,D245,R980,D473,L702,U396,R358,U636,R400,D919,R240,U780,L251,D633,L55,D723,L529,U319,L299,D89,L251,D557,L705,D705,L391,D58,R241 | |
| "; | |
| //public readonly string Input = @" | |
| //R75,D30,R83,U83,L12,D49,R71,U7,L72 | |
| //U62,R66,U55,R34,D71,R55,D58,R83 | |
| //"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| void Main() | |
| { | |
| var split = Input.Split('-'); | |
| int low = int.Parse(split[0]); | |
| int high = int.Parse(split[1]); | |
| var range = Enumerable.Range(low, high - low); | |
| //Digit(123456, 0).Dump(); | |
| //Digit(123456, 1).Dump(); | |
| //Digit(123456, 2).Dump(); | |
| //Digit(123456, 3).Dump(); | |
| //Digit(123456, 4).Dump(); | |
| //Digit(123456, 5).Dump(); | |
| //range = new int[] { 128899, 111111, 223450, 123789, 112233, 123444, 111122 }; | |
| range.Where(r => | |
| { | |
| for (int i = 0; i < 5; i++) | |
| { | |
| int d1 = Digit(r, i); | |
| int d2 = Digit(r, i + 1); | |
| if (d2 > d1) | |
| { | |
| return false; | |
| } | |
| } | |
| return true; | |
| }) | |
| .Where(r => | |
| { | |
| var pairs = r.ToString() | |
| .GroupBy(c => c) | |
| .Select(g => g.Count()) | |
| .Where(c => c > 1);//.Dump(); | |
| //.GroupBy(c => c) | |
| //.Select(g => g.Count()).Dump(); | |
| return pairs.Any(p => p == 2);//.Dump(); | |
| }) | |
| .ToList().Dump(); | |
| } | |
| int Digit(int value, int place) => value / ((int)Math.Pow(10, place)) % 10; | |
| static readonly string Input = @"128392-643281"; | |
| // You can define other methods, fields, classes and namespaces here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment