-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay7.cs
49 lines (37 loc) · 1.49 KB
/
Day7.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace cs
{
class Day7
{
public static void Part1()
{
List<int> positions = File.ReadLines(@"../../../../../data/day7.txt").First().Split(',').Select(num_s => int.Parse(num_s)).ToList();
int min_cost = int.MaxValue;
foreach (int end_position in new HashSet<int>(positions))
{
int position_cost = 0;
foreach (int crab_position in positions)
position_cost += Math.Abs(crab_position - end_position);
min_cost = Math.Min(min_cost, position_cost);
}
Console.WriteLine("Day: 7 | Part: 1 | Result: " + min_cost);
}
public static void Part2()
{
List<int> positions = File.ReadLines(@"../../../../../data/day7.txt").First().Split(',').Select(num_s => int.Parse(num_s)).ToList();
int min_cost = int.MaxValue;
for (int end_position = 1; end_position < positions.Max(); end_position++)
{
int position_cost = 0;
foreach (int crab_position in positions)
for (int step = 1; step <= Math.Abs(end_position - crab_position); step++)
position_cost += step;
min_cost = Math.Min(min_cost, position_cost);
}
Console.WriteLine("Day: 7 | Part: 2 | Result: " + min_cost);
}
}
}