Skip to content

Commit b13689b

Browse files
committed
Day 5 part 2
1 parent 14478b5 commit b13689b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

day-5/src/main.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,37 @@ fn part_1() {
4141

4242
fn part_2() {
4343
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);
4477
}

0 commit comments

Comments
 (0)