add until day05-1
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
use std::collections::HashSet;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
const FILENAME: &str = "example.txt";
|
||||
#[cfg(not(debug_assertions))]
|
||||
const FILENAME: &str = "input.txt";
|
||||
|
||||
fn main() {
|
||||
let data = std::fs::read_to_string(FILENAME).unwrap();
|
||||
|
||||
part_1(&data);
|
||||
part_2(&data);
|
||||
}
|
||||
|
||||
fn part_2(data: &str) {
|
||||
let mut hs = HashSet::new();
|
||||
|
||||
let mut xs: isize = 0;
|
||||
let mut ys: isize = 0;
|
||||
|
||||
let mut xr: isize = 0;
|
||||
let mut yr: isize = 0;
|
||||
|
||||
hs.insert((xs, ys));
|
||||
|
||||
for (idx, c) in data.trim().chars().enumerate() {
|
||||
let x;
|
||||
let y;
|
||||
if idx % 2 == 0 {
|
||||
x = &mut xs;
|
||||
y = &mut ys;
|
||||
} else {
|
||||
x = &mut xr;
|
||||
y = &mut yr;
|
||||
}
|
||||
match c {
|
||||
'^' => *y += 1,
|
||||
'>' => *x += 1,
|
||||
'<' => *x -= 1,
|
||||
'v' => *y -= 1,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
hs.insert((*x, *y));
|
||||
}
|
||||
|
||||
println!("{}", hs.len());
|
||||
}
|
||||
|
||||
fn part_1(data: &str) {
|
||||
let mut hs = HashSet::new();
|
||||
|
||||
let mut x: isize = 0;
|
||||
let mut y: isize = 0;
|
||||
|
||||
hs.insert((x, y));
|
||||
|
||||
for c in data.trim().chars() {
|
||||
match c {
|
||||
'^' => y += 1,
|
||||
'>' => x += 1,
|
||||
'<' => x -= 1,
|
||||
'v' => y -= 1,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
hs.insert((x, y));
|
||||
}
|
||||
|
||||
println!("{}", hs.len());
|
||||
}
|
||||
Reference in New Issue
Block a user