-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.rs
174 lines (148 loc) · 4.57 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// adventofcode - day 16
// part 1
use std::io::prelude::*;
use std::fs::File;
use std::str::FromStr;
struct Aunt {
id: i32,
children: i32,
cats: i32,
samoyeds: i32,
pomeranians: i32,
akitas: i32,
vizslas: i32,
goldfish: i32,
trees: i32,
cars: i32,
perfumes: i32,
}
impl Aunt {
fn new(id: i32) -> Aunt {
Aunt{
id: id,
children: -1,
cats: -1,
samoyeds: -1,
pomeranians: -1,
akitas: -1,
vizslas: -1,
goldfish: -1,
trees: -1,
cars: -1,
perfumes: -1,
}
}
fn matches(&self, other: &Aunt) -> bool {
if (self.children == -1 || self.children == other.children)
&& (self.cats == -1 || self.cats == other.cats)
&& (self.samoyeds == -1 || self.samoyeds == other.samoyeds)
&& (self.pomeranians == -1 || self.pomeranians == other.pomeranians)
&& (self.akitas == -1 || self.akitas == other.akitas)
&& (self.vizslas == -1 || self.vizslas == other.vizslas)
&& (self.goldfish == -1 || self.goldfish == other.goldfish)
&& (self.trees == -1 || self.trees == other.trees)
&& (self.cars == -1 || self.cars == other.cars)
&& (self.perfumes == -1 || self.perfumes == other.perfumes) {
return true;
}
false
}
fn print(&self) {
print!("Sue {}: ", self.id);
print!("{} children, ", self.children);
print!("{} cats, ", self.cats);
print!("{} samoyeds, ", self.samoyeds);
print!("{} pomeranians, ", self.pomeranians);
print!("{} akitas, ", self.akitas);
print!("{} vizslas, ", self.vizslas);
print!("{} goldfish, ", self.goldfish);
print!("{} trees, ", self.trees);
print!("{} cars, ", self.cars);
println!("{} perfumes", self.perfumes);
}
}
fn main(){
println!("Advent of Code - day 16 | part 1");
// import data
let data = import_data();
let mut aunts = Vec::new();
let giver = Aunt{
id: 0,
children: 3,
cats: 7,
samoyeds: 2,
pomeranians: 3,
akitas: 0,
vizslas: 0,
goldfish: 5,
trees: 3,
cars: 2,
perfumes: 1,
};
let mut linecount = 1i32;
for mut line in data.lines(){
let aunt = parse_line(&mut line, linecount);
if aunt.matches(&giver) {
aunts.push( aunt );
}
linecount += 1;
}
let matches = aunts.len();
for aunt in aunts {
aunt.print();
}
println!("Found {} matching aunts.", matches);
}
fn parse_line(line: &mut &str, number: i32) -> Aunt {
let mut x = "Sue ".to_string();
x.push_str( &number.to_string() );
x.push_str(": ");
eat(line, &x);
let mut aunt = Aunt::new(number);
let values = line.split(", ").collect::<Vec<_>>();
for mut value in values {
if eat(&mut value, "children: ") {
aunt.children = i32::from_str(value).unwrap();
} else if eat(&mut value, "cats: ") {
aunt.cats = i32::from_str(value).unwrap();
} else if eat(&mut value, "samoyeds: ") {
aunt.samoyeds = i32::from_str(value).unwrap();
} else if eat(&mut value, "pomeranians: ") {
aunt.pomeranians = i32::from_str(value).unwrap();
} else if eat(&mut value, "akitas: ") {
aunt.akitas = i32::from_str(value).unwrap();
} else if eat(&mut value, "vizslas: ") {
aunt.vizslas = i32::from_str(value).unwrap();
} else if eat(&mut value, "goldfish: ") {
aunt.goldfish = i32::from_str(value).unwrap();
} else if eat(&mut value, "trees: ") {
aunt.trees = i32::from_str(value).unwrap();
} else if eat(&mut value, "cars: ") {
aunt.cars = i32::from_str(value).unwrap();
} else if eat(&mut value, "perfumes: ") {
aunt.perfumes = i32::from_str(value).unwrap();
}
}
aunt
}
fn eat(s: &mut &str, expect: &str) -> bool {
if s.starts_with(expect) {
*s = &s[expect.len()..];
true
} else {
false
}
}
// This function simply imports the data set from a file called input.txt
fn import_data() -> String {
let mut file = match File::open("../../inputs/16.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
}