rust: Optimisations

This commit is contained in:
Sijmen 2020-12-19 16:04:05 +01:00
parent 8efa00bd48
commit 834c86e09a
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48

View file

@ -592,7 +592,7 @@ mod day16 {
.next() .next()
.unwrap() .unwrap()
.split(",") .split(",")
.flat_map(str::parse) .map(|s| s.parse().unwrap())
.collect(); .collect();
lines.next(); // "" lines.next(); // ""
@ -600,7 +600,7 @@ mod day16 {
let mut nearby_tickets: Vec<Ticket> = Vec::new(); let mut nearby_tickets: Vec<Ticket> = Vec::new();
while let Some(line) = lines.next() { while let Some(line) = lines.next() {
nearby_tickets.push(line.split(",").flat_map(str::parse).collect()); nearby_tickets.push(line.split(",").map(|s| s.parse().unwrap()).collect());
} }
let mut part1 = 0; let mut part1 = 0;
@ -629,20 +629,13 @@ mod day16 {
.collect(); .collect();
for ticket in valid_tickets { for ticket in valid_tickets {
for (field, possibilities) in ticket.iter().zip(field_possibilities.iter_mut()) { for (&field, possibilities) in ticket.iter().zip(field_possibilities.iter_mut()) {
for rule_i in possibilities.clone().iter() {
let rule = &rules[*rule_i];
if !((field >= &rule.ranges.0.from && field <= &rule.ranges.0.to)
|| (field >= &rule.ranges.1.from && field <= &rule.ranges.1.to))
{
possibilities possibilities
.iter() .iter()
.position(|i| i == rule_i) .position(|&rule_i| !rules[rule_i].validate(field))
.map(|i| possibilities.remove(i)); .map(|i| possibilities.remove(i));
} }
} }
}
}
let mut part2 = 1; let mut part2 = 1;
loop { loop {
@ -663,7 +656,7 @@ mod day16 {
} }
let rule = &rules[possibility].name; let rule = &rules[possibility].name;
if rule.starts_with("departure") { if rule.starts_with("de") {
part2 *= your_ticket[i]; part2 *= your_ticket[i];
} }
} }