Skip to content

Commit 73a773c

Browse files
committed
day 15
1 parent c6e92b0 commit 73a773c

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

day15/Program.cs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System.Numerics;
2+
3+
class Sensor
4+
{
5+
public int X { get; set; }
6+
public int Y { get; set; }
7+
public int BeaconX { get; set; }
8+
public int BeaconY { get; set; }
9+
10+
public int DeltaX { get; set; }
11+
12+
public Sensor(int x, int y, int beaconX, int beaconY)
13+
{
14+
X = x;
15+
Y = y;
16+
BeaconX = beaconX;
17+
BeaconY = beaconY;
18+
19+
DeltaX = Math.Abs(x - beaconX) + Math.Abs(y - beaconY);
20+
}
21+
22+
public int MinXAtY(int y) => X - DeltaX + Math.Abs(Y - y);
23+
public int MaxXAtY(int y) => X + DeltaX - Math.Abs(Y - y);
24+
}
25+
26+
class Program
27+
{
28+
static List<Sensor> sensors = new List<Sensor>();
29+
30+
static void Solve1(int row)
31+
{
32+
var minX = sensors.Min(s => s.X - s.DeltaX);
33+
var maxX = sensors.Max(s => s.X + s.DeltaX);
34+
35+
var score = 0;
36+
37+
for (int i = minX; i <= maxX; i++)
38+
{
39+
var isBeacon = false;
40+
41+
foreach (var s in sensors)
42+
{
43+
if (s.BeaconX == i && s.BeaconY == row)
44+
{
45+
isBeacon = true;
46+
break;
47+
}
48+
}
49+
50+
if (isBeacon)
51+
continue;
52+
53+
foreach (var s in sensors)
54+
{
55+
if (i >= s.MinXAtY(row) && i <= s.MaxXAtY(row))
56+
{
57+
score++;
58+
break;
59+
}
60+
}
61+
}
62+
63+
Console.WriteLine(score);
64+
}
65+
66+
static void Solve2(int maxBound)
67+
{
68+
for (var y = 0; y <= maxBound; y++)
69+
{
70+
var bounds = sensors.Select(s => new int[] {Math.Max(s.MinXAtY(y), 0), Math.Min(s.MaxXAtY(y), maxBound)})
71+
.Where(e => e[0] <= e[1]).ToList();
72+
73+
bounds.Sort((a, b) => a[0].CompareTo(b[0]));
74+
75+
var isMerged = true;
76+
77+
while (isMerged && bounds.Count > 1)
78+
{
79+
isMerged = false;
80+
81+
if (bounds[0][0] <= bounds[1][0] && bounds[0][1] >= bounds[1][0])
82+
{
83+
bounds[0][1] = Math.Max(bounds[0][1], bounds[1][1]);
84+
bounds.RemoveAt(1);
85+
isMerged = true;
86+
}
87+
}
88+
89+
if (!isMerged || bounds[0][0] != 0 || bounds[0][1] != maxBound)
90+
{
91+
Console.WriteLine(((BigInteger) (bounds[0][1] + 1)) * 4000000 + y);
92+
break;
93+
}
94+
}
95+
}
96+
97+
static void Main()
98+
{
99+
File.ReadAllLines("/Users/tpetrovic/Projects/aoc2022/day15/input.txt").ToList().ForEach(line =>
100+
{
101+
var parts = line.Replace("Sensor at x=", "").Replace(", y=", ",").Replace(": closest beacon is at x=", ",")
102+
.Replace(", y=", ",").Split(",");
103+
104+
sensors.Add(new Sensor(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]), int.Parse(parts[3])));
105+
});
106+
107+
Solve1(2000000);
108+
Solve2(4000000);
109+
}
110+
}

day15/day15.csproj

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>

day15/input.txt

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Sensor at x=1555825, y=18926: closest beacon is at x=1498426, y=-85030
2+
Sensor at x=697941, y=3552290: closest beacon is at x=595451, y=3788543
3+
Sensor at x=3997971, y=2461001: closest beacon is at x=3951198, y=2418718
4+
Sensor at x=3818312, y=282332: closest beacon is at x=4823751, y=1061753
5+
Sensor at x=2874142, y=3053631: closest beacon is at x=3074353, y=3516891
6+
Sensor at x=1704479, y=2132468: closest beacon is at x=1749091, y=2000000
7+
Sensor at x=3904910, y=2080560: closest beacon is at x=3951198, y=2418718
8+
Sensor at x=657061, y=3898803: closest beacon is at x=595451, y=3788543
9+
Sensor at x=3084398, y=3751092: closest beacon is at x=3074353, y=3516891
10+
Sensor at x=2582061, y=972407: closest beacon is at x=1749091, y=2000000
11+
Sensor at x=2886721, y=3971936: closest beacon is at x=3074353, y=3516891
12+
Sensor at x=303399, y=548513: closest beacon is at x=-1010425, y=986825
13+
Sensor at x=3426950, y=2290126: closest beacon is at x=3951198, y=2418718
14+
Sensor at x=3132858, y=3592272: closest beacon is at x=3074353, y=3516891
15+
Sensor at x=3773724, y=3751243: closest beacon is at x=3568452, y=3274758
16+
Sensor at x=3987212, y=2416515: closest beacon is at x=3951198, y=2418718
17+
Sensor at x=61559, y=3806326: closest beacon is at x=595451, y=3788543
18+
Sensor at x=2693503, y=2291389: closest beacon is at x=2269881, y=2661753
19+
Sensor at x=3953437, y=2669220: closest beacon is at x=3951198, y=2418718
20+
Sensor at x=763035, y=3997568: closest beacon is at x=595451, y=3788543
21+
Sensor at x=3999814, y=2370103: closest beacon is at x=3951198, y=2418718
22+
Sensor at x=1290383, y=1696257: closest beacon is at x=1749091, y=2000000
23+
Sensor at x=3505508, y=2805537: closest beacon is at x=3568452, y=3274758
24+
Sensor at x=3276207, y=3323122: closest beacon is at x=3568452, y=3274758
25+
Sensor at x=2244609, y=3517499: closest beacon is at x=3074353, y=3516891
26+
Sensor at x=1370860, y=3436382: closest beacon is at x=595451, y=3788543
27+
Sensor at x=3831063, y=3042662: closest beacon is at x=3568452, y=3274758
28+
Sensor at x=551202, y=3971545: closest beacon is at x=595451, y=3788543
29+
Sensor at x=336629, y=2519780: closest beacon is at x=595451, y=3788543
30+
Sensor at x=2033602, y=2882628: closest beacon is at x=2269881, y=2661753
31+
Sensor at x=3939808, y=2478271: closest beacon is at x=3951198, y=2418718
32+
Sensor at x=1958708, y=2370822: closest beacon is at x=1749091, y=2000000
33+
Sensor at x=3039958, y=3574483: closest beacon is at x=3074353, y=3516891

0 commit comments

Comments
 (0)