-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
111 lines (93 loc) · 4.38 KB
/
Solution.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using AdventOfCode.Common;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode2020.Day12
{
internal class Solution
{
private static readonly IntVector HStep = new IntVector(1, 0);
private static readonly IntVector VStep = new IntVector(0, 1);
private readonly IReadOnlyCollection<(char, int)> _instructions;
public Solution(IEnumerable<string> input)
{
_instructions = input.Select(line => (line[0], int.Parse(line.Substring(1)))).ToList();
}
public int PartOne()
{
var ship = _instructions.Aggregate(ShipState.Initial, (state, instruction) =>
{
var (action, value) = instruction;
switch (action)
{
case 'N':
case 'F' when state.Direction == Direction.North:
return state.WithPosition(state.Position + VStep * value);
case 'E':
case 'F' when state.Direction == Direction.East:
return state.WithPosition(state.Position + HStep * value);
case 'S':
case 'F' when state.Direction == Direction.South:
return state.WithPosition(state.Position - VStep * value);
case 'W':
case 'F' when state.Direction == Direction.West:
return state.WithPosition(state.Position - HStep * value);
case 'L':
return state.WithDirection(state.Direction.TurnLeft(times: value / 90));
case 'R':
return state.WithDirection(state.Direction.TurnRight(times: value / 90));
default:
throw new ArgumentException();
}
});
return ship.Position.ManhattanDistance;
}
public int PartTwo()
{
var ship = _instructions.Aggregate(ShipWaypointState.Initial, (state, instruction) =>
{
var (action, value) = instruction;
return action switch
{
'F' => state.WithShipPosition(state.ShipPosition + state.WaypointPosition * value),
'N' => state.WithWaypointPosition(state.WaypointPosition + VStep * value),
'E' => state.WithWaypointPosition(state.WaypointPosition + HStep * value),
'S' => state.WithWaypointPosition(state.WaypointPosition - VStep * value),
'W' => state.WithWaypointPosition(state.WaypointPosition - HStep * value),
'L' => state.WithWaypointPosition(state.WaypointPosition.Rotate(value)),
'R' => state.WithWaypointPosition(state.WaypointPosition.Rotate(-value)),
_ => throw new ArgumentException()
};
});
return ship.ShipPosition.ManhattanDistance;
}
private class ShipState
{
public ShipState(IntVector position, Direction direction)
{
Position = position;
Direction = direction;
}
public IntVector Position { get; }
public Direction Direction { get; }
public ShipState WithPosition(IntVector position) => new ShipState(position, Direction);
public ShipState WithDirection(Direction direction) => new ShipState(Position, direction);
public static ShipState Initial => new ShipState(new IntVector(0, 0), Direction.East);
}
private class ShipWaypointState
{
public ShipWaypointState(IntVector shipPosition, IntVector waypointPosition)
{
ShipPosition = shipPosition;
WaypointPosition = waypointPosition;
}
public IntVector ShipPosition { get; }
public IntVector WaypointPosition { get; }
public ShipWaypointState WithShipPosition(IntVector position) =>
new ShipWaypointState(position, WaypointPosition);
public ShipWaypointState WithWaypointPosition(IntVector position) =>
new ShipWaypointState(ShipPosition, position);
public static ShipWaypointState Initial => new ShipWaypointState(new IntVector(0, 0), new IntVector(10, 1));
}
}
}