initial commit
This commit is contained in:
commit
cf7d87c04d
34 changed files with 4282 additions and 0 deletions
1
day5/.gitignore
vendored
Normal file
1
day5/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target
|
||||
7
day5/Cargo.lock
generated
Normal file
7
day5/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
||||
6
day5/Cargo.toml
Normal file
6
day5/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
1387
day5/input
Normal file
1387
day5/input
Normal file
File diff suppressed because it is too large
Load diff
59
day5/src/main.rs
Normal file
59
day5/src/main.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
fn main() {
|
||||
let file = File::open("input").unwrap();
|
||||
|
||||
let mut orderings: Vec<(i64, i64)> = Vec::new();
|
||||
let mut updates: Vec<Vec<i64>> = Vec::new();
|
||||
|
||||
for line_result in io::BufReader::new(file).lines() {
|
||||
let line = match line_result {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
eprintln!("Error reading line: {}", e);
|
||||
continue;
|
||||
}
|
||||
};
|
||||
if line.contains("|") {
|
||||
let pages: Vec<_> = line.split("|").collect();
|
||||
orderings.push(
|
||||
(
|
||||
pages[0].parse::<i64>().unwrap(),
|
||||
pages[1].parse::<i64>().unwrap()
|
||||
)
|
||||
)
|
||||
}
|
||||
else if line.contains(",") {
|
||||
updates.push(line.split(",").map(|p| p.parse::<i64>().unwrap()).collect());
|
||||
}
|
||||
else {
|
||||
println!("unexpected input: '{}'", line)
|
||||
}
|
||||
}
|
||||
|
||||
let mut sum_correct: i64 = 0;
|
||||
let mut sum_reordered: i64 = 0;
|
||||
for update in updates {
|
||||
let mut is_correct = true;
|
||||
'update: for i in 1..update.len() {
|
||||
for (p1, p2) in orderings.clone().into_iter() {
|
||||
if update[i] == p1 && update[i-1] == p2 {
|
||||
is_correct = false;
|
||||
break 'update
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_correct {
|
||||
sum_correct += update[(update.len()-1)/2];
|
||||
}
|
||||
else {
|
||||
|
||||
sum_reordered += update[(update.len()-1)/2];
|
||||
}
|
||||
}
|
||||
|
||||
println!("Sum (correct): {}", sum_correct);
|
||||
println!("Sum (reordered): {}", sum_reordered);
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue