<jupyter_start><jupyter_text>[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/oddrationale/AdventOfCode2020CSharp/main?urlpath=lab%2Ftree%2FDay12.ipynb)  --- Day 12: Rain Risk ---<jupyter_code>using System.IO;
var navInstructions = File.ReadAllLines(@"input/12.txt").ToArray();
enum Orientation
{
    N,
    E,
    S,
    W,
};
class Ship1
{
    private int _x = 0;
    private int _y = 0;
    
    public int X { get => _x; }
    public int Y { get => _y; }    
    public int Distance { get => Math.Abs(_x) + Math.Abs(_y); }
    public Orientation Facing { get; set; } = Orientation.E;
    
    private void Move(string instruction)
    {
        var direction = instruction[0] == 'F' ? Facing : Enum.Parse<Orientation>(instruction[0].ToString());
        
        switch (direction)
        {
            case Orientation.N:
                _y -= Convert.ToInt32(instruction[1..]);
                break;
            case Orientation.E:
                _x += Convert.ToInt32(instruction[1..]);
                break;
            case Orientation.S:
                _y += Convert.ToInt32(instruction[1..]);
                break;
            case Orientation.W:
                _x -= Convert.ToInt32(instruction[1..]);
                break;
        }
    }
    
    private void Turn(string instruction)
    {
        var degrees = Convert.ToInt32(instruction[1..]);
        switch (instruction[0])
        {
            case 'L':
                Facing = (Orientation)(((int)Facing + 4 - (degrees / 90)) % 4);
                break;
            case 'R':
                Facing = (Orientation)(((int)Facing + (degrees / 90)) % 4);
                break;
        }
    }
    
    public void ReadNavInstruction(string instruction)
    {
        switch (instruction[0])
        {
            case 'N':
            case 'E':
            case 'S':
            case 'W':
            case 'F':
                Move(instruction);
                break;
            case 'L':
            case 'R':
                Turn(instruction);
                break;
        }
    }
}
var ship1 = new Ship1();
foreach (var i in navInstructions)
{
    ship1.ReadNavInstruction(i);
}
ship1.Distance<jupyter_output><empty_output><jupyter_text>--- Part Two ---<jupyter_code>class Ship2
{
    private int _x = 0;
    private int _y = 0;
    private int _dX = 10;
    private int _dY = -1;
    
    public int X { get => _x; }
    public int Y { get => _y; }
    public int WaypointX { get => _x + _dX; }
    public int WaypointY { get => _y + _dY; }
    public int Distance { get => Math.Abs(_x) + Math.Abs(_y); }
    
    private void Move(string instruction)
    {
        if (instruction[0] == 'F')
        {
            for (int i = 0; i < Convert.ToInt32(instruction[1..]); i++)
            {
                _x = WaypointX;
                _y = WaypointY;
            }
            return;
        }
        
        switch (Enum.Parse<Orientation>(instruction[0].ToString()))
        {
            case Orientation.N:
                _dY -= Convert.ToInt32(instruction[1..]);
                break;
            case Orientation.E:
                _dX += Convert.ToInt32(instruction[1..]);
                break;
            case Orientation.S:
                _dY += Convert.ToInt32(instruction[1..]);
                break;
            case Orientation.W:
                _dX -= Convert.ToInt32(instruction[1..]);
                break;
        }
    }
    
    private void Turn(string instruction)
    {
        var degrees = Convert.ToInt32(instruction[1..]);
        switch (instruction[0])
        {
            case 'L':
                for (int i = 0; i < degrees / 90; i++)
                {
                    (_dX, _dY) = (_dY, -_dX);
                }
                break;
            case 'R':
                for (int i = 0; i < degrees / 90; i++)
                {
                    (_dX, _dY) = (-_dY, _dX);
                }
                break;
        }
    }
    
    public void ReadNavInstruction(string instruction)
    {
        switch (instruction[0])
        {
            case 'N':
            case 'E':
            case 'S':
            case 'W':
            case 'F':
                Move(instruction);
                break;
            case 'L':
            case 'R':
                Turn(instruction);
                break;
        }
    }
}
var ship2 = new Ship2();
foreach (var i in navInstructions)
{
    ship2.ReadNavInstruction(i);
}
ship2.Distance<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12629490852986555, 'fraction_numerical': 0.01873484681507604, 'mean_word_length': 1.3403816400206292, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '496139', 'license': 'MIT', 'n_tokens_mistral': 1470, 'n_tokens_neox': 1352, 'n_words': 304, 'path': 'Day12.ipynb', 'repo_name': 'oddrationale/AdventOfCode2020CSharp', 'size': '4537'}