-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay5.cs
96 lines (80 loc) · 2.51 KB
/
Day5.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
namespace advent_of_code_2020
{
public class Day5
{
private List<string> _inputFile;
private readonly int[] _startRowRange = Enumerable.Range(0, 128).ToArray();
private readonly int[] _startSeatRange = Enumerable.Range(0, 8).ToArray();
private List<int> _seatIds = new List<int>();
public Day5()
{
_inputFile = Utilities.LoadFile("Data/Day5_Input.txt");
ProcessPasses();
}
private int[] SplitRange(char splitType, int[] currentRange)
{
var rangeLength = currentRange.Length / 2;
var newRange = splitType switch
{
'F' => currentRange.Take(rangeLength),
'L' => currentRange.Take(rangeLength),
'B' => currentRange.Skip(rangeLength),
'R' => currentRange.Skip(rangeLength),
_ => currentRange
};
return newRange.ToArray();
}
private int ProcessBoardingPass(string pass)
{
var rowCode = pass[..7];
var seatCode = pass[7..];
var rowResult = _startRowRange;
var seatResult = _startSeatRange;
foreach (var split in rowCode)
{
rowResult = SplitRange(split, rowResult);
}
var row = rowResult[0];
foreach (var split in seatCode)
{
seatResult = SplitRange(split, seatResult);
}
var seat = seatResult[0];
return GetSeatId(row, seat);
}
private int GetSeatId(int row, int seat)
{
return row * 8 + seat;
}
public void ProcessPasses()
{
foreach (var pass in _inputFile)
{
var seatId = ProcessBoardingPass(pass);
_seatIds.Add(seatId);
}
}
public int FindHighestSeatId()
{
return _seatIds.Max();
}
public int FindMySeat()
{
for (int i = 1; i < _seatIds.Count; i++)
{
var thisSeat = _seatIds[i];
var prevSeat = thisSeat - 1;
var nextSeat = thisSeat + 1;
if (!_seatIds.Contains(nextSeat))
{
return nextSeat;
}
}
return 0;
}
}
}