Compare commits
2 Commits
0992350e32
...
fb330b9b9f
| Author | SHA1 | Date | |
|---|---|---|---|
| fb330b9b9f | |||
| 1f3f322997 |
1
aoc-02/.gitignore
vendored
Normal file
1
aoc-02/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
||||
7
aoc-02/Cargo.lock
generated
Normal file
7
aoc-02/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 = "aoc-02"
|
||||
version = "0.1.0"
|
||||
8
aoc-02/Cargo.toml
Normal file
8
aoc-02/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "aoc-02"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
1000
aoc-02/input
Normal file
1000
aoc-02/input
Normal file
File diff suppressed because it is too large
Load Diff
87
aoc-02/src/main.rs
Normal file
87
aoc-02/src/main.rs
Normal file
@ -0,0 +1,87 @@
|
||||
use std::{fs::File, io::Read};
|
||||
|
||||
struct ReportList {
|
||||
reports: Vec<Vec<i64>>,
|
||||
}
|
||||
|
||||
impl ReportList {
|
||||
fn new() -> ReportList {
|
||||
ReportList { reports: vec![] }
|
||||
}
|
||||
|
||||
fn parse_data(&mut self, buf: String) {
|
||||
for entry in buf.split('\n') {
|
||||
let split_entries = entry.split(' ').collect::<Vec<&str>>();
|
||||
let mut tmp_vec = Vec::<i64>::new();
|
||||
for value in split_entries {
|
||||
tmp_vec.push(value.parse::<i64>().unwrap());
|
||||
}
|
||||
self.reports.push(tmp_vec);
|
||||
}
|
||||
}
|
||||
|
||||
fn solve(&mut self) {
|
||||
let mut safe = 0;
|
||||
// direction: 0 Ascending, 1 Descending
|
||||
let mut direction = 0;
|
||||
for report in &self.reports {
|
||||
let mut report_safe = true;
|
||||
// Skip the first iteration
|
||||
for (pos, entry) in report.iter().enumerate() {
|
||||
if pos == 0 {
|
||||
continue;
|
||||
}
|
||||
if pos == 1 {
|
||||
// Check first two values and see if we need to be ascending or decending
|
||||
if &report[pos - 1] < entry {
|
||||
// We are ascending
|
||||
direction = 0;
|
||||
} else {
|
||||
// We are descending
|
||||
direction = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Do the actual solution
|
||||
if direction == 0 {
|
||||
// Check all values ascending
|
||||
if (entry - &report[pos - 1]) > 3 {
|
||||
report_safe = false;
|
||||
}
|
||||
if (entry - &report[pos - 1]) <= 0 {
|
||||
report_safe = false
|
||||
}
|
||||
} else {
|
||||
// Check all values descending
|
||||
if (&report[pos - 1] - entry) > 3 {
|
||||
report_safe = false;
|
||||
}
|
||||
if (&report[pos - 1] - entry) <= 0 {
|
||||
report_safe = false
|
||||
}
|
||||
}
|
||||
}
|
||||
if report_safe {
|
||||
safe += 1;
|
||||
}
|
||||
}
|
||||
println!("{}", safe);
|
||||
}
|
||||
}
|
||||
|
||||
fn load_file(path: String) -> String {
|
||||
let mut buf = String::new();
|
||||
let mut fp = File::open(path).expect("Unable to open file");
|
||||
|
||||
fp.read_to_string(&mut buf).expect("Failed to read file");
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut reportlist = ReportList::new();
|
||||
let data = load_file(String::from("input"));
|
||||
|
||||
reportlist.parse_data(data);
|
||||
reportlist.solve();
|
||||
}
|
||||
7
aoc-02/test_input
Normal file
7
aoc-02/test_input
Normal file
@ -0,0 +1,7 @@
|
||||
7 6 4 2 1
|
||||
1 2 7 8 9
|
||||
9 7 6 2 1
|
||||
1 3 2 4 5
|
||||
8 6 4 4 1
|
||||
1 3 6 7 9
|
||||
9 7 6 3 1
|
||||
Loading…
x
Reference in New Issue
Block a user