Skip to content

Commit 32c1b72

Browse files
committed
solved day 16 part 1 test but not input
1 parent bc8e9dc commit 32c1b72

File tree

2 files changed

+305
-0
lines changed

2 files changed

+305
-0
lines changed

2023/Day16/inputClean

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
...|../..................-.................\.....................\............-...........|.....|.|.........-|
97+
..............................-...........|..-/.................|................................../..........
98+
.../......../.............-./.............|...../......./...|...........\..............-..........-....|..-./.
99+
/..\...|...........|...........................|/................../......./......................\.....\.....
100+
..|.........-........|..|......-...................\.........|...........................-....................
101+
../...-.....-.............../...|\...\.......|.................|.\.........-...................-\.........-./.
102+
../..............................|.......|..............\...\......................\..........|...............
103+
............/....|../....../.............-............................./......./.................\............
104+
/.....\...../.........../.............../...-.../\.....|.....|............|....|..........\.....\..../........
105+
....\....-/.........................|............-...........\........\...\.....\\................\........-/.
106+
.....................-..-\...............././...........................\....-./......|..............\..-.....
107+
................./........|.......-.-....................\.-.||...-.......\./.|./.....|...-.../......./.......
108+
........-..-...../.......\.........................\.....././.\.../..|...../..................................
109+
.............|...............--.........../.|\/.....\.........................|..|.....................-......
110+
..........-...|.......\/...................|....../.-...-...|....................\..../.-...../..../..-.......

2023/Day16/solution.cs

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,212 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Linq;
25
using adventofcode;
36

47
namespace aoc2023;
58

69
public class solutionDay16 : ISolver
710
{
11+
public static char[,] Map { get; set; }
12+
public static int WidthX { get; set; }
13+
public static int HeightY { get; set; }
14+
15+
public static HashSet<Beam> Beams { get; set; } = new HashSet<Beam>();
16+
public static HashSet<VisitedPlace> VisitedPlaces { get; set; } = new HashSet<VisitedPlace>();
17+
818
public void SolvePart1()
919
{
1020
Console.WriteLine("Day 16, Part 1");
21+
22+
solutionBase solutionBase = new solutionBase();
23+
var input = solutionBase.getInputLines($"2023/Day16/test").ToArray();
24+
//var input = solutionBase.getInputLines($"2023/Day16/inputClean").ToArray();
25+
26+
WidthX = input[0].Length;
27+
HeightY = input.Length;
28+
Map = new char[WidthX, HeightY];
29+
30+
for (int y = 0; y < HeightY; y++)
31+
{
32+
for (int x = 0; x < WidthX; x++)
33+
{
34+
Map[x, y] = input[y][x];
35+
}
36+
}
37+
38+
//PrintMap();
39+
40+
var beam = new Beam(0, 0, Direction.Right);
41+
beam.AddBeam2Lists();
42+
43+
while (Beams.Any(b => b.Moving))
44+
{
45+
var beamsToMove = Beams.Where(b => b.Moving).ToList();
46+
foreach (var b in beamsToMove)
47+
{
48+
b.Move();
49+
}
50+
}
51+
52+
foreach (var vp in VisitedPlaces)
53+
{
54+
Map[vp.X, vp.Y] = '#';
55+
}
56+
57+
PrintMap();
58+
59+
Console.WriteLine($"Result visitedPlaces: {VisitedPlaces.Count}");
60+
61+
HashSet<Point> hash = new HashSet<Point>();
62+
foreach (var vp in VisitedPlaces)
63+
{
64+
hash.Add(new Point(vp.X, vp.Y));
65+
}
66+
67+
Console.WriteLine($"Result points: {hash.Count}");
68+
1169
}
1270

1371
public void SolvePart2()
1472
{
1573
Console.WriteLine("Day 16, Part 2");
1674
}
75+
76+
public class Beam()
77+
{
78+
public int X { get; private set; }
79+
public int Y { get; private set; }
80+
public Direction Direction { get; private set; }
81+
public bool Moving { get; set; } = true;
82+
83+
public Beam(int x, int y, Direction direction) : this()
84+
{
85+
X = x;
86+
Y = y;
87+
Direction = direction;
88+
}
89+
90+
public void Move()
91+
{
92+
switch (Direction)
93+
{
94+
case Direction.Up:
95+
Y++;
96+
break;
97+
case Direction.Down:
98+
Y--;
99+
break;
100+
case Direction.Left:
101+
X--;
102+
break;
103+
case Direction.Right:
104+
X++;
105+
break;
106+
}
107+
108+
if(X < 0 || X >= WidthX || Y < 0 || Y >= HeightY)
109+
{
110+
Moving = false;
111+
return;
112+
}
113+
114+
var nextPlace = Map[X, Y];
115+
116+
if (VisitedPlaces.Any(vp => vp.X == X && vp.Y == Y && vp.Direction == Direction))
117+
{
118+
Moving = false;
119+
return;
120+
}
121+
122+
if (nextPlace == '|' && (Direction == Direction.Left || Direction == Direction.Right))
123+
{
124+
Moving = false;
125+
var newBeamUp = new Beam(X, Y, Direction.Up);
126+
var newBeamDown = new Beam(X, Y, Direction.Down);
127+
newBeamUp.AddBeam2Lists();
128+
newBeamDown.AddBeam2Lists();
129+
}
130+
else if (nextPlace == '-' && (Direction == Direction.Up || Direction == Direction.Down))
131+
{
132+
Moving = false;
133+
var newBeamLeft = new Beam(X, Y, Direction.Left);
134+
var newBeamRight = new Beam(X, Y, Direction.Right);
135+
newBeamLeft.AddBeam2Lists();
136+
newBeamRight.AddBeam2Lists();
137+
}
138+
else if (nextPlace == '/')
139+
{
140+
if (Direction == Direction.Up)
141+
{
142+
Direction = Direction.Left;
143+
}
144+
else if (Direction == Direction.Down)
145+
{
146+
Direction = Direction.Right;
147+
}
148+
else if (Direction == Direction.Left)
149+
{
150+
Direction = Direction.Up;
151+
}
152+
else if (Direction == Direction.Right)
153+
{
154+
Direction = Direction.Down;
155+
}
156+
}
157+
else if (nextPlace == '\\')
158+
{
159+
if (Direction == Direction.Up)
160+
{
161+
Direction = Direction.Right;
162+
}
163+
else if (Direction == Direction.Down)
164+
{
165+
Direction = Direction.Left;
166+
}
167+
else if (Direction == Direction.Left)
168+
{
169+
Direction = Direction.Down;
170+
}
171+
else if (Direction == Direction.Right)
172+
{
173+
Direction = Direction.Up;
174+
}
175+
}
176+
VisitedPlaces.Add(new VisitedPlace(this.X, this.Y, this.Direction));
177+
}
178+
179+
public void AddBeam2Lists()
180+
{
181+
Beams.Add(this);
182+
VisitedPlaces.Add(new VisitedPlace(this.X, this.Y, this.Direction));
183+
}
184+
}
185+
186+
public enum Direction
187+
{
188+
Up,
189+
Down,
190+
Left,
191+
Right
192+
}
193+
194+
public class VisitedPlace(int x, int y, Direction direction)
195+
{
196+
public int X { get; } = x;
197+
public int Y { get; } = y;
198+
public Direction Direction { get; } = direction;
199+
}
200+
201+
public static void PrintMap()
202+
{
203+
for (int y = 0; y < HeightY; y++)
204+
{
205+
for (int x = 0; x < WidthX; x++)
206+
{
207+
Console.Write(Map[x, y]);
208+
}
209+
Console.WriteLine();
210+
}
211+
}
17212
}

0 commit comments

Comments
 (0)