File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -41,4 +41,37 @@ fn part_1() {
41
41
42
42
fn part_2 ( ) {
43
43
let input = fs:: read_to_string ( "./day-5/input.txt" ) . unwrap ( ) ;
44
+ let result = input. lines ( ) . fold ( 0 , |acc, elem| {
45
+ let mut has_valid_pairs = false ;
46
+ let mut history: Vec < String > = Vec :: new ( ) ;
47
+ let mut last_char = ' ' ;
48
+ let mut antepenultimate_char = ' ' ;
49
+ let mut allowed = false ;
50
+
51
+ for c in elem. chars ( ) {
52
+ let pair = format ! ( "{}{}" , last_char, c) ;
53
+ if history. last ( ) . unwrap_or ( & String :: new ( ) ) != & pair {
54
+ if history. contains ( & pair) {
55
+ has_valid_pairs = true ;
56
+ } else {
57
+ history. push ( pair) ;
58
+ }
59
+ }
60
+
61
+ if antepenultimate_char == c && last_char != c {
62
+ allowed = true ;
63
+ }
64
+
65
+ antepenultimate_char = last_char;
66
+ last_char = c;
67
+ }
68
+
69
+ if allowed && has_valid_pairs {
70
+ acc + 1
71
+ } else {
72
+ acc
73
+ }
74
+ } ) ;
75
+
76
+ println ! ( "Part 2 result: {}" , result) ;
44
77
}
You can’t perform that action at this time.
0 commit comments