示例#1
0
        static int CalcJolts(string input)
        {
            var prevJolt = 0;
            var jump1    = 0;
            var jump3    = 1; // Include the final 3 jolt jump up front

            foreach (var jolt in FileIterator.Lines <int>(input).OrderBy((v) => v))
            {
                switch (jolt - prevJolt)
                {
                case 1:
                    jump1++;
                    break;

                case 2:
                    break;

                case 3:
                    jump3++;
                    break;

                default:
                    Oh.Bugger();
                    break;
                }
                prevJolt = jolt;
            }

            return(jump1 * jump3);
        }
示例#2
0
            public void Execute(Instruction[] prog, bool part2)
            {
                foreach (var instuction in prog)
                {
                    switch (instuction.Op)
                    {
                    case Operation.Mem:
                        if (part2)
                        {
                            SetMemRecursive(0, 0, instuction.OperandA, instuction.OperandB);
                        }
                        else
                        {
                            var value = instuction.OperandB;
                            value &= AndMask;
                            value |= OrMask;
                            Mem[instuction.OperandA] = value;
                        }
                        break;

                    case Operation.Mask:
                        AndMask = instuction.OperandA;
                        OrMask  = instuction.OperandB;
                        Mask    = instuction.Mask;
                        break;

                    default:
                        Oh.Bugger();
                        break;
                    }
                }
            }
示例#3
0
            public GuardEvent(string details)
            {
                var matches = details.Match(@"\[(.+)\] (.+)");

                if (matches.Groups.Count != 3)
                {
                    Oh.Bugger();
                }

                Time = DateTime.ParseExact(matches.Groups[1].Value, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
                var desription = matches.Groups[2].Value;

                if (desription == "falls asleep")
                {
                    Type = GuardEventType.Asleep;
                    Id   = -1;
                }
                else if (desription == "wakes up")
                {
                    Type = GuardEventType.Awake;
                    Id   = -1;
                }
                else
                {
                    Type = GuardEventType.BeginShift;
                    Id   = int.Parse(desription.Match(@"#(\d+)").Groups[1].Value);
                }
            }
示例#4
0
        CellState[,] LoadEnvironment(string inputFile)
        {
            var lines       = FileIterator.LoadLines <string>(inputFile);
            var environment = new CellState[lines[0].Length, lines.Length];

            foreach (var(x, y) in environment.Rectangle())
            {
                switch (lines[y][x])
                {
                case '.':
                    environment[x, y] = CellState.Clear;
                    break;

                case '|':
                    environment[x, y] = CellState.Tree;
                    break;

                case '#':
                    environment[x, y] = CellState.LumberYard;
                    break;

                default:
                    Oh.Bugger();
                    break;
                }
            }

            return(environment);
        }
示例#5
0
        public void Problem1(string input, long expected)
        {
            var vmState = new VmState();
            var prog    = LoadProgram(input);

            try
            {
                ExecuteUntilLoop(vmState, prog);
                Oh.Bugger();
            }
            catch (Utils.VM.Halt)
            {
                vmState.Acc.Should().Be(expected);
            }
        }
示例#6
0
        void ParseRule(GridRules rules, string rule)
        {
            var fromTo    = rule.Replace(" => ", "|").Split('|');
            var rotations = CreateRotations(fromTo[0]);
            var writer    = CreateWriter(fromTo[1]);

            foreach (var rotation in rotations)
            {
                if (rules.ContainsKey(rotation))
                {
                    Oh.Bugger();
                }
                rules[rotation] = writer;
            }
        }
示例#7
0
 void ChangeDirection(Map map, int x, int y, ref int dX, ref int dY)
 {
     if (dX == 0)
     {
         var left  = map.GetCell(x - 1, y);
         var right = map.GetCell(x + 1, y);
         if (left == '-' || IsLetter(left))
         {
             dX = -1;
             dY = 0;
         }
         else if (right == '-' || IsLetter(right))
         {
             dX = 1;
             dY = 0;
         }
         else
         {
             Oh.WhatTheFuck();
         }
     }
     else
     {
         var up   = map.GetCell(x, y - 1);
         var down = map.GetCell(x, y + 1);
         if (up == '|' || IsLetter(up))
         {
             dX = 0;
             dY = -1;
         }
         else if (down == '|' || IsLetter(down))
         {
             dX = 0;
             dY = 1;
         }
         else
         {
             Oh.Bugger();
         }
     }
 }
示例#8
0
            public Request(string request)
            {
                var split = request.Split('@', ',', ':', 'x');

                if (split.Length != 5)
                {
                    Oh.Bugger();
                }

                Id  = int.Parse(split[0].Substring(1));
                Pos = new int[]
                {
                    int.Parse(split[1]),
                    int.Parse(split[2])
                };
                Size = new int[]
                {
                    int.Parse(split[3]),
                    int.Parse(split[4])
                };
            }
示例#9
0
            public Stroke(string input)
            {
                var match = input.Match(@"(.)=(\d+), .=(\d+)\.\.(\d+)");

                if (match.Groups.Count != 5)
                {
                    Oh.Bugger();
                }

                Horizontal = match.Groups[1].Value == "y";
                if (Horizontal)
                {
                    StartY = int.Parse(match.Groups[2].Value);
                    StartX = int.Parse(match.Groups[3].Value);
                }
                else
                {
                    StartX = int.Parse(match.Groups[2].Value);
                    StartY = int.Parse(match.Groups[3].Value);
                }
                Extent = int.Parse(match.Groups[4].Value);
            }
示例#10
0
            public Cart(int x, int y, char initialDirection)
            {
                this.x = x;
                this.y = y;
                switch (initialDirection)
                {
                case '<':
                    dX = -1;
                    dY = 0;
                    break;

                case '>':
                    dX = 1;
                    dY = 0;
                    break;

                case '^':
                    dX = 0;
                    dY = -1;
                    break;

                case 'v':
                case 'V':
                    dX = 0;
                    dY = 1;
                    break;

                default:
                    Oh.Bugger();
                    break;
                }

                crossingAction = new Action[]
                {
                    TurnLeft,
                    () => { },
                    TurnRight
                };
            }