-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.rs
66 lines (54 loc) · 1.63 KB
/
part1.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// adventofcode - day 3
// part 1
use std::io::prelude::*;
use std::fs::File;
use std::collections::HashMap;
#[derive(Hash, Eq, PartialEq, Debug)]
struct Point {
x: i32,
y: i32,
}
impl Point {
fn new(x: i32, y: i32) -> Point {
Point { x: x, y: y }
}
}
fn main(){
println!("Advent of Code - day 3 | part 1");
// import data
let steps = import_data();
// we'll use a HasMap to keep track of the points (used as keys in the map)
// we already visited -> Every point can only be stored once. Therefore, at
// the end we can simply read out the number of stored locations
let mut visited = HashMap::new();
let mut santa = Point::new(0,0);
// save the initial point
visited.insert( Point::new(0, 0), true );
for step in steps.chars(){
match step {
'>' => santa.x += 1,
'<' => santa.x += -1,
'^' => santa.y += 1,
'v' => santa.y += -1,
_ => {},
}
// save Santas current position
// Note: We have to create a new point because of Rust's move & borrow
// concepts
visited.insert( Point::new(santa.x, santa.y), true);
}
println!("Santa visited {} unique houses", visited.len() );
}
// This function simply imports the data set from a file called input.txt
fn import_data() -> String {
let mut file = match File::open("../../inputs/03.txt") {
Ok(f) => f,
Err(e) => panic!("file error: {}", e),
};
let mut data = String::new();
match file.read_to_string(&mut data){
Ok(_) => {},
Err(e) => panic!("file error: {}", e),
};
data
}