Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace AdventOfCodeDay1
- {
- class Solution
- {
- public object PartOne(string input) =>
- Solve(input, @"\d");
- public object PartTwo(string input) =>
- Solve(input, @"\d|one|two|three|four|five|six|seven|eight|nine");
- int Solve(string input, string rx) => (
- from line in input.Split("\n")
- let first = Regex.Match(line, rx)
- let last = Regex.Match(line, rx, RegexOptions.RightToLeft)
- select ParseMatch(first.Value) * 10 + ParseMatch(last.Value)
- ).Sum();
- int ParseMatch(string st) => st switch
- {
- "one" => 1,
- "two" => 2,
- "three" => 3,
- "four" => 4,
- "five" => 5,
- "six" => 6,
- "seven" => 7,
- "eight" => 8,
- "nine" => 9,
- var d => int.Parse(d)
- };
- }
- class Program
- {
- static void Main(string[] args)
- {
- string filePath = "input.txt";
- if (!File.Exists(filePath))
- {
- Console.WriteLine("Файлът input.txt не е намерен!");
- return;
- }
- string input = File.ReadAllText(filePath);
- var solution = new Solution();
- Console.WriteLine($"Part One: {solution.PartOne(input)}");
- Console.WriteLine($"Part Two: {solution.PartTwo(input)}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement