File tree Expand file tree Collapse file tree 1 file changed +77
-0
lines changed
Expand file tree Collapse file tree 1 file changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . IO ;
3+
4+ namespace adventofcode . Year2024 . Day02 ;
5+
6+ public class Solution : BaseSolution , ISolver
7+ {
8+ public void SolvePart1 ( )
9+ {
10+ //var inputFilePath = GetInputFilePath("test");
11+ var inputFilePath = GetInputFilePath ( ) ;
12+ Console . WriteLine ( inputFilePath ) ;
13+
14+ // Read input file
15+ string [ ] lines = File . ReadAllLines ( inputFilePath ) ;
16+
17+ int safeReports = 0 ;
18+
19+ foreach ( string report in lines )
20+ {
21+ string [ ] levels = report . Split ( ' ' ) ;
22+ long previousLevel = Convert . ToInt64 ( levels [ 0 ] ) ;
23+ long nextLevel = Convert . ToInt64 ( levels [ 1 ] ) ;
24+ bool wayUp = false ;
25+ bool safe = true ;
26+
27+ if ( previousLevel < nextLevel )
28+ wayUp = true ;
29+
30+ // if(previousLevel == nextLevel)
31+ // break;
32+
33+ for ( int i = 1 ; i < levels . Length ; i ++ )
34+ {
35+ long actualLevel = Convert . ToInt64 ( levels [ i ] ) ;
36+ long diff = 0 ;
37+
38+ if ( previousLevel < actualLevel && wayUp )
39+ {
40+ diff = actualLevel - previousLevel ;
41+ if ( diff == 0 || diff > 3 )
42+ {
43+ safe = false ;
44+ break ;
45+ }
46+ }
47+ else if ( previousLevel > actualLevel && ! wayUp )
48+ {
49+ diff = previousLevel - actualLevel ;
50+ if ( diff == 0 || diff > 3 )
51+ {
52+ safe = false ;
53+ break ;
54+ }
55+ }
56+ else
57+ {
58+ safe = false ;
59+ break ;
60+ }
61+ previousLevel = actualLevel ;
62+ }
63+
64+ if ( safe )
65+ {
66+ safeReports ++ ;
67+ }
68+ }
69+
70+ Console . WriteLine ( $ "Count of safe reports: { safeReports } ") ;
71+ }
72+
73+ public void SolvePart2 ( )
74+ {
75+ Console . WriteLine ( "Part 2" ) ;
76+ }
77+ }
You can’t perform that action at this time.
0 commit comments