์ค ์ด๋ฐ ๊ฒ์ด ์์๊ตฐ์.
1๋ฒ ๋ถํฐ 25๋ฒ๊น์ง ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๊ฐ ์ด๋ฆฌ๋ฉฐ ์ง๊ธ์ 14๋ฒ์งธ ๋ฌธ์ ๊ฐ ๊ฒ์๋์ด ์์ต๋๋ค.
ANDREA ANGELLA๋์ด C#์ ์ด์ฉํด Advent of Code 2022
๋ฌธ์ ๋ฅผ ํ๊ณ ์์ต๋๋ค!
์ค ์ด๋ฐ ๊ฒ์ด ์์๊ตฐ์.
1๋ฒ ๋ถํฐ 25๋ฒ๊น์ง ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๊ฐ ์ด๋ฆฌ๋ฉฐ ์ง๊ธ์ 14๋ฒ์งธ ๋ฌธ์ ๊ฐ ๊ฒ์๋์ด ์์ต๋๋ค.
ANDREA ANGELLA๋์ด C#์ ์ด์ฉํด Advent of Code 2022
๋ฌธ์ ๋ฅผ ํ๊ณ ์์ต๋๋ค!
์ ์ Day1 ๋ฌธ์ ํ์ ๋๋ค. ๋ฌธ์ ๋ฅผ ์์ฉํ ์ ์๋ ๋ฌธ์ ๊ฐ ์๋ก ์ด๋ฆฝ๋๋ค. ๊ณ ์ ์ ์ผ๋ก ํ์์ต๋๋ค.
var lines = File.ReadAllLines("day1_input.txt");
var (max, sum) = (0, 0);
foreach (var line in lines)
{
if (line is "")
{
if (sum > max)
max = sum;
sum = 0;
continue;
}
sum += int.Parse(line);
}
Console.WriteLine(max);
Day1์ ์ฒซ ๋ฒ์งธ ๋ฌธ์ ๋ฅผ ํ๋ฉด ์์ฉํ๋ ๋ ๋ฒ์งธ ๋ฌธ์ ๊ฐ ์ด๋ฆฌ๋๋ฐ ์์ 3๊ฐ์ ํฉ์ ๊ตฌํ๋ ๊ฒ์ ๋๋ค. ์ฌ์ฌ LINQ๋ก ํ ๊ป ์ฝ๊ฐ์ ํํ๋ฅผ ํ๊ฒ ๋๋ค์ ^^;
์์ ์ฝ๋๋ ๋ค์์ฒ๋ผ LINQ๋ก๋ ํํ ๊ฐ๋ฅํฉ๋๋ค.
var max = File.ReadAllText("day1_input.txt")
.Split(Environment.NewLine + Environment.NewLine)
.Select(x =>
x.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)
.Sum(y => int.Parse(y))
)
.Max();
Console.WriteLine(max);
๊ฐ๋ ์ฑ์? ๊ธ์์โฆ ์ฒซ ๋ฒ์งธ ์ฝ๋๊ฐ ์ข์ ๋ณด์ ๋๋ค.
Day1์ ๋๋ฒ์งธ ๋ฌธ์ ์ ํ์ด์ ๋๋ค. LINQ๋ฅผ ์ฌ์ฉํ์ผ๋ฏ๋ก ์ ๋ ฌ์ ํ ํ ์์ 3๊ฐ์ ํฉ์ ๊ตฌํ๋ ๊ฒ์ผ๋ก ํ ์ ์์ต๋๋ค.
var max = File.ReadAllText("day1_input.txt")
.Split(Environment.NewLine + Environment.NewLine)
.Select(x =>
x.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)
.Sum(y => int.Parse(y))
)
.OrderByDescending(x => x)
.Take(3)
.Sum();
Console.WriteLine(max);
Day2 ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์์ต๋๋ค.
์ดํดํ๋ฉด ๊ฐ๋จํ ๋ฌธ์ ์ธ๋ฐ ์์ด๋ผ ์ฒ์ ์ดํดํ๊ธฐ๊ฐ ์ด๋ ค์ ๋ค์.
๋ค์์ ํ์ด์ ๋๋ค.
internal class Day2
{
/// <summary>
/// ๊ฐ์๋ฐ์๋ณด ๊ฒ์์์ ์น๋ฆฌํ์
/// </summary>
public static void Solve1()
{
var lines = File.ReadAllLines("day2_input.txt");
var sum = 0;
foreach (var line in lines)
{
var temp = line.Split();
var y = temp[0] switch { "A" => Kind.๋ฐ์, "B" => Kind.๋ณด, "C" => Kind.๊ฐ์, _ => throw new InvalidOperationException() };
var i = temp[1] switch { "X" => Kind.๋ฐ์, "Y" => Kind.๋ณด, "Z" => Kind.๊ฐ์, _ => throw new InvalidOperationException() };
var score = (int)i;
// ์ด๊ธด ๊ฒฝ์ฐ
if ((i, y) is (Kind.๋ฐ์, Kind.๊ฐ์) ||
(i, y) is (Kind.๋ณด, Kind.๋ฐ์) ||
(i, y) is (Kind.๊ฐ์, Kind.๋ณด))
score += 6;
// ๋น๊ธด ๊ฒฝ์ฐ
else if (i == y)
score += 3;
sum += score;
}
Console.WriteLine(sum);
}
}
internal enum Kind
{
๋ฐ์ = 1,
๋ณด = 2,
๊ฐ์ = 3
}
๋ง๋ค๊ณ ๋๋ ์ ๋ง์์ ๋ค์ง ์์ต๋๋ค.
์ด๊ธด ๊ฒฝ์ฐ์ ๋ํ ํจํด์ ์ข ๋ ๋ถ์ํ ํ ๋ค์์ ์ฝ๋๋ฅผ ์์ฑํ์์ต๋๋ค.
var lines = File.ReadAllLines("day2_input.txt");
var sum = 0;
foreach (var line in lines)
{
var y = line[0] - 'A';
var i = line[2] - 'X';
var score = 0;
// ๋น๊ธด๊ฒฝ์ฐ
if (i == y)
score = 3;
// ์ด๊ธด๊ฒฝ์ฐ
else if (i == (y + 1) % 3)
score = 6;
sum += score + i + 1;
}
Console.WriteLine(sum);
Day2์ ์ฌํ ๋ฌธ์ ๋ ๋น์ทํ ํจํด์ผ๋ก ํ ์ ์์ต๋๋ค.
var lines = File.ReadAllLines("day2_input.txt");
var sum = 0;
foreach (var line in lines)
{
var y = line[0] - 'A'; // 0:๋ฐ์, 1:๋ณด, 2:๊ฐ์
var r = line[2] - 'X'; // 0:์ง, 1:๋น๊น, 2:์ด๊น
// 0, 0 : ๋ฐ์, i = 2
// 0, 1 : ๋ฐ์, i = 0
// 0, 2 : ๋ฐ์, i = 1
// 1, 0 : ๋ณด, i = 0
// 1, 1 : ๋ณด, i = 1
// 1, 2 : ๋ณด, i = 2
// 2, 0 : ๊ฐ์, i = 1
// 2, 1 : ๊ฐ์, i = 2
// 2, 2 : ๊ฐ์, i = 0
var i = (y + r + 2) % 3;
sum += r * 3 + i + 1;
}
Console.WriteLine(sum);
๊ทธ๋ฐ๋ฐ ์ฝ๋๋ ๊ฐ๊ฒฐํด ์ก์ง๋ง ๊ฐ๋ ์ฑ์ ๋จ์ด์ง๋๊ตฐ์.
Day3 ๋ฌธ์ ๋ฅผ ํ์ด๋ดค์ต๋๋ค.
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
๊ฐ ๋ฌธ์์ด์ ์ ๋ฐ์ผ๋ก ์๋ฅธ ํ ์ผ์ชฝ ๋ฌธ์์ด๊ณผ ์ค๋ฅธ์ชฝ ๋ฌธ์์ด ์ค ๊ฐ์ ์ฒซ ๋ฌธ์๋ฅผ ์ฐ์ ์์ ๊ฐ์ผ๋ก ์นํํด์ ํฉ๊ณ๋ฅผ ๊ตฌํ๋ฉด ํ ์ ์์ต๋๋ค.
var lines = File.ReadAllLines("day3_input.txt");
//var lines = new[]
//{
// "vJrwpWtwJgWrhcsFMMfFFhFp",
// "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL",
// "PmmdzqPrVvPwwTWBwg",
// "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn",
// "ttgJtRGJQctTZtZT",
// "CrZsJsPPZsGzwwsLwLmpwMDw",
//};
var sum = 0;
foreach (var line in lines)
{
var rucksackSize = line.Length / 2;
var aList = line[..rucksackSize];
var bList = line[rucksackSize..];
//var result = from a in aList
// join b in bList
// on a equals b
// select a;
//var c = result.FirstOrDefault();
var c = aList
.Join(bList, x => x, y => y, (x, y) => x)
.FirstOrDefault();
var priority = GetPriorityNumber(c);
sum += priority;
}
Console.WriteLine(sum);
static int GetPriorityNumber(char c) => c switch
{
>= 'a' and <= 'z' => c - 'a' + 1,
>= 'A' and <= 'Z' => c - 'A' + 27,
_ => 0
};
LINQ๋ฅผ ์ฌ์ฉํ๋ฉด ์ฝ๊ฒ ํ๋ฆฌ๋ ๋ฌธ์ ๋ค์.
Join()
๋์ Intersect()
๋ก ์ข ๋ ๊ฐ๋จํ ํํํ ์ ์์ต๋๋ค.
var c = aList
.Intersect(bList)
.FirstOrDefault();
Day3์ ์ฌํ ๋ฌธ์ ์ ๋๋ค. ์ฌํ ๋ฌธ์ ๋ 3์ค์ ๊ณตํต์ผ๋ก ๋ค์ด๊ฐ ๋ฌธ์๋ฅผ ์ฐพ์ ์ฐ์ ์์ ๊ฐ์ผ๋ก ๋ณํํ ํ ๊ทธ ํฉ๊ณ๋ฅผ ๊ตฌํ๋ ๊ฒ์ ๋๋ค.
์ด๋ฐ ๋ฌธ์ ๋ LINQ๋ฅผ ์ด์ฉํ๋ฉด ์ฝ๊ฒ ํ๋ฆฝ๋๋ค.
var lines = File.ReadAllLines("day3_input.txt");
//var lines = new[]
//{
// "vJrwpWtwJgWrhcsFMMfFFhFp",
// "jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL",
// "PmmdzqPrVvPwwTWBwg",
// "wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn",
// "ttgJtRGJQctTZtZT",
// "CrZsJsPPZsGzwwsLwLmpwMDw",
//};
var sum = lines.Chunk(3)
.Select(x => x[0].Intersect(x[1]).Intersect(x[2]).FirstOrDefault())
.Sum(x => GetPriorityNumber(x));
Console.WriteLine(sum);
static int GetPriorityNumber(char c) => c switch
{
>= 'a' and <= 'z' => c - 'a' + 1,
>= 'A' and <= 'Z' => c - 'A' + 27,
_ => 0
};
Day4์ ๋ฌธ์ ๋ ๋ ๊ฐ์ ๋ฒ์ ์ค ํฌํจ๋๋ ๋ฒ์์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ๊ฒ์ ๋๋ค.
public static void Solve1()
{
var lines = File.ReadAllLines("day4_input.txt");
//var lines = new[]
//{
// "2-4,6-8",
// "2-3,4-5",
// "5-7,7-9",
// "2-8,3-7",
// "6-6,4-6",
// "2-6,4-8",
//};
var count = lines.Select(x => x.Split(','))
.Select(x => new
{
A = x[0].Split('-').Select(y => int.Parse(y)).ToArray(),
B = x[1].Split('-').Select(y => int.Parse(y)).ToArray()
})
.Count(x => IsInclude(x.A, x.B));
Console.WriteLine(count);
static bool IsInclude(int[] r1, int[] r2) => (r1, r2) switch
{
_ when r1[0] <= r2[0] && r1[1] >= r2[1] => true,
_ when r2[0] <= r1[0] && r2[1] >= r1[1] => true,
_ => false
};
๊ฒฐ๊ณผ๊ฐ ๋์ค๋ฉด ํ๊ท ์ ํ๋ ๊ฒ์ด๊ฒ ์ง๋ง ANDREA ANGELLA๋์ ํ์ด๋ ๊ถ๊ธํด์ง๋๊ตฐ์.
var assignments = File.ReadAllLines("Input.txt")
.Select(pair => pair.Split(","))
.Select(x => (Assignment.Parse(x[0]), Assignment.Parse(x[1])));
var fullyContainedAssignments = assignments.Where(a => a.First.FullyContains(a.Second) || a.Second.FullyContains(a.First));
Assert.That(fullyContainedAssignments.Count(), Is.EqualTo(518));
private record Assignment(int Start, int End)
{
public bool Overlap(Assignment a) => End < a.Start || Start > a.End;
public bool FullyContains(Assignment a) => Start <= a.Start && End >= a.End;
public static Assignment Parse(string a) => new(int.Parse(a.Split("-")[0]), int.Parse(a.Split("-")[1]));
}
๊น๋ํ๊ตฐ์.
Day4์ ์์ฉ๋ฌธ์ ๋ ๋๊ฐ์ ๋ฒ์๊ฐ ๊ฒน์น ๊ฒฝ์ฐ๋ฅผ ์นด์ดํธ ํ๋ ๊ฒ์ ๋๋ค. ๋ค์ํ ํ์ด๊ฐ ์๊ฒ ์ง๋ง ์ด๋ฒ์๋ ์ข ๋ ๋ฌด์ํ๊ฒ ์ง๋ดค์ต๋๋ค.
public static void Solve2()
{
var lines = File.ReadAllLines("day4_input.txt");
//var lines = new[]
//{
// "2-4,6-8",
// "2-3,4-5",
// "5-7,7-9",
// "2-8,3-7",
// "6-6,4-6",
// "2-6,4-8",
//};
var count = lines.Select(x => x.Split(','))
.Select(x => new
{
A = x[0].Split('-').Select(y => int.Parse(y)).ToArray(),
B = x[1].Split('-').Select(y => int.Parse(y)).ToArray()
})
.Count(x => IsOverlap(x.A, x.B));
Console.WriteLine(count);
static bool IsOverlap(int[] r1, int[] r2)
{
var r1List = Enumerable.Range(r1[0], r1[1] - r1[0] + 1);
var r2List = Enumerable.Range(r2[0], r2[1] - r2[0] + 1);
return r1List.Intersect(r2List).Any();
}
}
Day5 ๋ฌธ์ ๋ ํ ์คํธ ์ ๋ณด์์ ์คํ ๋ชฉ๋ก์ ๋ง๋ค๊ณ ์ด๋ ๋ช ๋ น์ ํด์ํด์ ์คํ์ ์กฐ์ ํ ๋ค ์คํ์ ์์ธ ๊ฐ์ฅ ์์ ๋ฌผ๊ฑด์ ์ป๋ ๋ฌธ์ ์ ๋๋ค.
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2
์ต๋ํ LINQ๋ก ํ๋ ค๊ณ ๋ ธ๋ ฅํด๋ดค๊ณ ์๋ LINQ ๋ช ๋ น์ด ๋ง์ง ์๋ค๋ ๊ฒ์ ๋๊ผ์ต๋๋ค. ์ ์๊ฒ AoC ๋ฌธ์ ๋ LINQ์ ์ข ๋ ์ต์ํด์ง ์ ์๋ ๊ฒฝํ์ด ๋๊ณ๊ตฐ์.
public static void Solve1()
{
var lines = File.ReadAllText("day5_input.txt");
//var lines = """
// [D]
// [N] [C]
// [Z] [M] [P]
// 1 2 3
// move 1 from 2 to 1
// move 3 from 1 to 3
// move 2 from 2 to 1
// move 1 from 1 to 2
// """;
// ์คํ๋ถ์ ๋ช
๋ น๋ถ ๋ผ์ธ ์ป์
var (stacksLines, commandLines) = lines
.Split(Environment.NewLine + Environment.NewLine)
.Chunk(2)
.Select(x =>
(x[0].Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries),
x[1].Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)))
.First();
// ์คํ๋ถ์์ ๋ฐ์ค ์ ๋ณด ์ป์
var stackBoxes = stacksLines
.Select(x => x
.Chunk(4)
.Select((y, i) => (Number: i + 1, Box: y[1]))
.Where(x => x.Box is not ' ') // ๋น ์ฌ๋กฏ ์ ์ธ
)
.SkipLast(1) // ๋ง์ง๋ง ์ซ์๋ถ ์ ์ธ
.SelectMany(x => x)
.Reverse(); // ์๋๋ถํฐ ๋ด์์ผ ํ๋ฏ๋ก ๋ฐ์
var stackCount = stackBoxes.Max(x => x.Number);
// ๋ฐ์ค๋ฅผ ์คํ์ ๋ด์
var stacks = new Stack<char>[stackCount];
foreach (var stackBox in stackBoxes)
{
ref var stack = ref stacks[stackBox.Number - 1];
if (stack is null)
stack = new Stack<char>();
stack.Push(stackBox.Box);
}
// ๋ช
๋ น๋ถ ์ฒ๋ฆฌ
foreach (var commandLine in commandLines)
{
var (moves, from, to) = commandLine.Split(' ')
.Chunk(6)
.Select(x => (int.Parse(x[1]), int.Parse(x[3]), int.Parse(x[5])))
.First();
foreach (var count in Enumerable.Range(1, moves))
{
var stack = stacks[from - 1].Pop();
stacks[to - 1].Push(stack);
}
}
var result = string.Concat(stacks.Select(x => x.First().ToString()));
Console.WriteLine(result);
}
๋ฌธ์ ๋ฅผ ๊ณ์ ํ์ด๊ฐ๋ค ๋ณด๋ฉด ๋ฌธ์ ํด๊ฒฐ์ ์ํด ๋ฌธ์ ๋ฅผ ์ฐ์๋ ์ ๋ณด - Stream
์ผ๋ก ๋ณด๊ณ ํ์ด๊ฐ๋ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ํด ์ข ๋ ์ต์ํด์ง์ง ์์๊น ์๊ฐํด๋ด
๋๋ค.
Day5์ ์ฌํ ๋ฌธ์ ์ ๋๋ค. ์ด์ ๋ ์์๋ฅผ ํ๋์ฉ ์ฎ๊ธฐ๋ ๊ฒ์ด ์๋๋ผ ์ฎ๊ฒจ์ผ ํ๋ ์์๋ฅผ ๋์์ ์ฎ๊ฒผ์ ๋ ์ต์ข ๊ตฌ์ฑ๋ ์คํ์์ ์๋จ์ ๋์ง ๋ฐ์ค๋ค์ ๊ตฌํ๋ ๋ฌธ์ ์ ๋๋ค.
๋์ด์ ์คํ์ด ํ์ ์์ผ๋ฏ๋ก ์ฌ๋ฌ๊ฐ๋ฅผ ์ฎ๊ธธ ์ ์๋ List<T>
๋ก ์ปฌ๋ ์
์ ๋ณ๊ฒฝํ๊ณ ๋ช
๋ น ์ฒ๋ฆฌ ํ ์ต์ข
์คํ ์๋จ์ ๋ฐ์ค๋ฅผ ๊ฒฐํฉํ๋ ๊ฒ์ string.Concat()
๋์ LINQ Aggregate()
๋ฅผ ์ฌ์ฉํ์์ต๋๋ค.
public static void Solve2()
{
var lines = File.ReadAllText("day5_input.txt");
//var lines = """
// [D]
// [N] [C]
// [Z] [M] [P]
// 1 2 3
// move 1 from 2 to 1
// move 3 from 1 to 3
// move 2 from 2 to 1
// move 1 from 1 to 2
// """;
// ์คํ๋ถ์ ๋ช
๋ น๋ถ ๋ผ์ธ ์ป์
var (stacksLines, commandLines) = lines
.Split(Environment.NewLine + Environment.NewLine)
.Chunk(2)
.Select(x =>
(x[0].Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries),
x[1].Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries)))
.First();
// ์คํ๋ถ์์ ๋ฐ์ค ์ ๋ณด ์ป์
var stackBoxes = stacksLines
.Select(x => x
.Chunk(4)
.Select((y, i) => (Number: i + 1, Box: y[1]))
.Where(x => x.Box is not ' ') // ๋น ์ฌ๋กฏ ์ ์ธ
)
.SkipLast(1) // ๋ง์ง๋ง ์ซ์๋ถ ์ ์ธ
.SelectMany(x => x);
var stackCount = stackBoxes.Max(x => x.Number);
// ๋ฐ์ค๋ฅผ ์คํ์ ๋ด์
var stacks = new List<char>[stackCount];
foreach (var stackBox in stackBoxes)
{
ref var stack = ref stacks[stackBox.Number - 1];
if (stack is null)
stack = new List<char>();
stack.Add(stackBox.Box);
}
// ๋ช
๋ น๋ถ ์ฒ๋ฆฌ
foreach (var commandLine in commandLines)
{
var (moves, from, to) = commandLine.Split(' ')
.Chunk(6)
.Select(x => (int.Parse(x[1]), int.Parse(x[3]), int.Parse(x[5])))
.First();
var fromStacks = stacks[from - 1];
var toStacks = stacks[to - 1];
var movingBoxes = fromStacks.GetRange(0, moves);
fromStacks.RemoveRange(0, moves);
toStacks.InsertRange(0, movingBoxes);
}
//var result = string.Concat(stacks.Select(x => x.First().ToString()));
var result = stacks.Aggregate("", (x, stack) => x + stack.First());
Console.WriteLine(result);
}
๋ค์ ๋ฌธ์ ๋ถํฐ๋ ๋ฌธ์ ๊ฐ ์ ๋ํ๋ ์ทจ์ง์ ๋ง๊ฒ ์ ๋ ํ ์คํธ๋ก ๊ตฌ์ฑํด์ ํด๋ด์ผ๊ฒ ์ต๋๋ค.