Skip to content

Commit cd0db2b

Browse files
committed
Add day 5 part 1 and 2
1 parent 7e3c046 commit cd0db2b

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed

input_examples/day05.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
47|53
2+
97|13
3+
97|61
4+
97|47
5+
75|29
6+
61|13
7+
75|53
8+
29|13
9+
97|29
10+
53|29
11+
61|53
12+
97|53
13+
61|29
14+
47|13
15+
75|47
16+
97|75
17+
47|61
18+
75|61
19+
47|29
20+
75|13
21+
53|13
22+
23+
75,47,61,53,29
24+
97,61,53,29,13
25+
75,29,13
26+
75,97,47,61,53
27+
61,13,29
28+
97,13,75,29,47

src/days.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod day01;
33
mod day02;
44
mod day03;
55
mod day04;
6+
mod day05;
67

78
use common::generic_day::GenericDay;
89

@@ -17,6 +18,7 @@ pub fn run_day(day: u8, input_folder: String) {
1718
2 => run_my_day(day02::Day02::new(input_folder)),
1819
3 => run_my_day(day03::Day03::new(input_folder)),
1920
4 => run_my_day(day04::Day04::new(input_folder)),
21+
5 => run_my_day(day05::Day05::new(input_folder)),
2022
_ => panic!("Day not found!"),
2123
}
2224
}

src/days/day05.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
use std::collections::HashMap;
2+
use std::fs::File;
3+
use std::io::BufRead;
4+
use std::io::BufReader;
5+
6+
use crate::days::common::generic_day;
7+
8+
pub struct Day05 {
9+
input_file: String,
10+
page_ordering_rules: HashMap<i64, Vec<i64>>,
11+
updates: Vec<Vec<i64>>,
12+
}
13+
14+
impl Day05 {
15+
pub fn new(input_folder: String) -> Day05 {
16+
let mut day05: Day05 = Day05 {
17+
input_file: format!("{}/day05.txt", input_folder),
18+
page_ordering_rules: HashMap::new(),
19+
updates: Vec::new(),
20+
};
21+
day05.parse_input();
22+
day05
23+
}
24+
25+
fn process_page_ordering_rules(&mut self, line: String) {
26+
let pages = line
27+
.split("|")
28+
.map(|x| x.parse::<i64>().unwrap())
29+
.collect::<Vec<_>>();
30+
self.page_ordering_rules
31+
.entry(pages[0])
32+
.and_modify(|x| x.push(pages[1]))
33+
.or_insert(vec![pages[1]]);
34+
}
35+
36+
fn process_update(&mut self, line: String) {
37+
self.updates.push(
38+
line.split(",")
39+
.map(|x| x.parse::<i64>().unwrap())
40+
.collect::<Vec<_>>(),
41+
);
42+
}
43+
fn parse_input(&mut self) {
44+
let lines = BufReader::new(File::open(&self.input_file).unwrap())
45+
.lines()
46+
.map(|x| x.unwrap())
47+
.collect::<Vec<_>>();
48+
49+
let mut all_page_ordering_rules_processed: bool = false;
50+
for line in lines {
51+
if line == "" {
52+
all_page_ordering_rules_processed = true;
53+
} else if !all_page_ordering_rules_processed {
54+
self.process_page_ordering_rules(line);
55+
} else {
56+
self.process_update(line);
57+
}
58+
}
59+
}
60+
fn is_update_correctly_ordered(&self, update: &Vec<i64>) -> bool {
61+
let mut result = true;
62+
63+
for (position, page) in update.iter().enumerate() {
64+
if self.page_ordering_rules.contains_key(page) {
65+
for not_allowed_previous_page in &self.page_ordering_rules[page] {
66+
if update[0..position].contains(&not_allowed_previous_page) {
67+
result = false;
68+
break;
69+
}
70+
if !result {
71+
break;
72+
}
73+
}
74+
}
75+
}
76+
result
77+
}
78+
79+
fn correct_update(&self, update: &Vec<i64>) -> Vec<i64> {
80+
let mut corrected_update = update.clone();
81+
82+
while !self.is_update_correctly_ordered(&corrected_update) {
83+
let mut positions_to_swap = vec![0, 0];
84+
for (position, page) in corrected_update.iter().enumerate() {
85+
if self.page_ordering_rules.contains_key(page) {
86+
for not_allowed_previous_page in &self.page_ordering_rules[page] {
87+
if corrected_update[0..position].contains(&not_allowed_previous_page) {
88+
let position_to_swap_with = corrected_update
89+
.iter()
90+
.position(|p| p == not_allowed_previous_page)
91+
.unwrap();
92+
positions_to_swap[0]=position;
93+
positions_to_swap[1]=position_to_swap_with;
94+
break;
95+
}
96+
}
97+
}
98+
}
99+
corrected_update.swap(positions_to_swap[0], positions_to_swap[1]);
100+
}
101+
corrected_update
102+
}
103+
}
104+
105+
impl generic_day::GenericDay for Day05 {
106+
fn part1(&self) -> i64 {
107+
let mut result = 0;
108+
for update in &self.updates {
109+
if self.is_update_correctly_ordered(update) {
110+
result += update[update.len() / 2];
111+
}
112+
}
113+
result
114+
}
115+
116+
fn part2(&self) -> i64 {
117+
let mut result = 0;
118+
for update in &self.updates {
119+
if !self.is_update_correctly_ordered(update) {
120+
let corrected_update = self.correct_update(update);
121+
result += corrected_update[corrected_update.len() / 2];
122+
}
123+
}
124+
result
125+
}
126+
}
127+
128+
#[cfg(test)]
129+
mod tests {
130+
use super::*;
131+
use crate::days::GenericDay;
132+
133+
#[test]
134+
fn result_part1() {
135+
let day05: Day05 = Day05::new(String::from("input_examples"));
136+
assert_eq!(day05.part1(), 143);
137+
}
138+
139+
#[test]
140+
fn result_part2() {
141+
let day05: Day05 = Day05::new(String::from("input_examples"));
142+
assert_eq!(day05.part2(), 123);
143+
}
144+
}

0 commit comments

Comments
 (0)