Skip to content

Commit 219b226

Browse files
committed
24 05
1 parent c30d46b commit 219b226

File tree

6 files changed

+207
-2
lines changed

6 files changed

+207
-2
lines changed

solutions/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,7 @@ required-features = ["regex"]
167167
[[bin]]
168168
name = "2024_04"
169169
path = "src/p2024_04.rs"
170+
171+
[[bin]]
172+
name = "2024_05"
173+
path = "src/p2024_05.rs"

solutions/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
let now = chrono::Utc::now().date_naive();
88
let (year, day) = (now.year(), now.day());
99

10-
let day = 04;
10+
let day = 05;
1111

1212
let input_folder = format!("input");
1313
let date_folder = format!("{:04}_{:02}", year, day);

solutions/input/2024_05/test_01

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

solutions/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ pub mod p2024_02;
4646
#[cfg(feature = "regex")]
4747
pub mod p2024_03;
4848
pub mod p2024_04;
49+
pub mod p2024_05;

solutions/src/p2024_05.rs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#![allow(dead_code, unreachable_code, unused)]
2+
3+
use utils::*;
4+
5+
pub type Data = (HashMap<(usize, usize), (usize, usize)>, Vec<Vec<usize>>);
6+
7+
pub fn parse_data(input: utils::Input) -> Data {
8+
let Some((a, b)) = input.split_once("\n\n") else {
9+
panic!()
10+
};
11+
12+
let mut rules = HashMap::new();
13+
for (k, v) in a
14+
.lines()
15+
.map(|line| line.trim().split_once('|').unwrap())
16+
.map(|(a, b)| (a.parse().unwrap(), b.parse().unwrap()))
17+
{
18+
rules.insert((k, v), (k, v));
19+
rules.insert((v, k), (k, v));
20+
}
21+
22+
let updates = b
23+
.lines()
24+
.map(|line| line.trim().split(',').map(|n| n.parse().unwrap()).collect())
25+
.collect();
26+
27+
(rules, updates)
28+
}
29+
30+
pub fn make_ok(job: &mut Vec<usize>, map: &HashMap<usize, Vec<usize>>, printed: &mut Vec<usize>) {
31+
printed.clear();
32+
33+
loop {
34+
let mut ok = true;
35+
36+
'job: for i in 0..job.len() {
37+
let c = job[i];
38+
let Some(e) = map.get(&c) else {
39+
printed.push(c);
40+
continue;
41+
};
42+
43+
for &j in e {
44+
if printed.contains(&j) {
45+
let c = job.remove(i);
46+
job.insert(0, c);
47+
ok = false;
48+
printed.clear();
49+
break 'job;
50+
}
51+
}
52+
53+
printed.push(c);
54+
}
55+
56+
if ok {
57+
break;
58+
}
59+
}
60+
}
61+
62+
pub fn is_ok(
63+
job: &[usize],
64+
rules: &HashMap<(usize, usize), (usize, usize)>,
65+
map: &mut HashMap<usize, Vec<usize>>,
66+
printed: &mut Vec<usize>,
67+
) -> bool {
68+
printed.clear();
69+
map.clear();
70+
71+
for i in 0..job.len() {
72+
let a = job[i];
73+
for j in i + 1..job.len() {
74+
let b = job[j];
75+
76+
if let Some((a, b)) = rules.get(&(a, b)).copied() {
77+
let e = map.entry(a).or_insert_with(|| vec![]);
78+
e.push(b);
79+
}
80+
}
81+
}
82+
83+
let mut ok = true;
84+
'job: for i in 0..job.len() {
85+
let c = job[i];
86+
let Some(e) = map.get(&c) else {
87+
printed.push(c);
88+
continue;
89+
};
90+
91+
for &j in e {
92+
if printed.contains(&j) {
93+
ok = false;
94+
break 'job;
95+
}
96+
}
97+
98+
printed.push(c);
99+
}
100+
101+
ok
102+
}
103+
104+
pub fn part_1(input: utils::Input) -> String {
105+
let (rules, updates) = parse_data(input);
106+
107+
let mut result = 0;
108+
109+
let mut printed = vec![];
110+
let mut map = HashMap::default();
111+
'jobs: for job in updates {
112+
let ok = is_ok(&job, &rules, &mut map, &mut printed);
113+
114+
if ok {
115+
result += job[job.len() / 2];
116+
}
117+
}
118+
119+
format!("{}", result)
120+
}
121+
122+
pub fn part_2(input: utils::Input) -> String {
123+
let (rules, updates) = parse_data(input);
124+
125+
let mut result = 0;
126+
127+
let mut printed = vec![];
128+
let mut map = HashMap::default();
129+
'jobs: for mut job in updates {
130+
let ok = is_ok(&job, &rules, &mut map, &mut printed);
131+
if !ok {
132+
make_ok(&mut job, &map, &mut printed);
133+
result += job[job.len() / 2];
134+
}
135+
}
136+
137+
format!("{}", result)
138+
}
139+
140+
pub fn run_1(date: utils::Date) {
141+
#[rustfmt::skip]
142+
let tests_1 = [
143+
(1, Some("143")),
144+
];
145+
146+
let all_correct_1 = utils::test(part_1, date, 1, &tests_1);
147+
if all_correct_1 {
148+
let answer = utils::run(part_1, 1, date);
149+
}
150+
}
151+
152+
pub fn run_2(date: utils::Date) {
153+
#[rustfmt::skip]
154+
let tests_2 = [
155+
(1, Some("123")),
156+
];
157+
158+
let all_correct_2 = utils::test(part_2, date, 2, &tests_2);
159+
if all_correct_2 {
160+
let answer = utils::run(part_2, 2, date);
161+
}
162+
}
163+
164+
pub fn date() -> utils::Date {
165+
utils::date_from_file_name(file!())
166+
}
167+
168+
fn main() {
169+
let date = date();
170+
run_1(date);
171+
run_2(date);
172+
}

utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
pub use std::{
2-
collections::{BinaryHeap, HashMap, HashSet, VecDeque},
2+
collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque},
33
fs::{read_to_string, File},
44
io::{BufRead, BufReader},
55
};

0 commit comments

Comments
 (0)