Compare commits
No commits in common. "fb330b9b9fada3d482e0a180c1c35cf35b24de12" and "0992350e3267d732a2121925366634eeb70fc193" have entirely different histories.
fb330b9b9f
...
0992350e32
1
aoc-02/.gitignore
vendored
1
aoc-02/.gitignore
vendored
@ -1 +0,0 @@
|
|||||||
/target
|
|
||||||
7
aoc-02/Cargo.lock
generated
7
aoc-02/Cargo.lock
generated
@ -1,7 +0,0 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "aoc-02"
|
|
||||||
version = "0.1.0"
|
|
||||||
@ -1,8 +0,0 @@
|
|||||||
[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
1000
aoc-02/input
File diff suppressed because it is too large
Load Diff
@ -1,87 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
@ -1,7 +0,0 @@
|
|||||||
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