Add helper

This commit is contained in:
kageru 2020-12-16 16:12:41 +01:00
parent 60e63a667d
commit a2aa8a353f
3 changed files with 12 additions and 15 deletions

View File

@ -1,6 +1,7 @@
#![feature(test)]
extern crate test;
use std::collections::HashMap;
use aoc2020::common::*;
type Parsed = Vec<usize>;
@ -8,8 +9,9 @@ fn read_input() -> String {
String::from("15,12,0,14,3,1")
}
#[inline]
fn parse_input(raw: &str) -> Parsed {
raw.split(',').filter_map(|x| x.parse().ok()).collect()
parse_nums(raw)
}
#[rustfmt::skip]

View File

@ -45,23 +45,13 @@ fn parse_input(raw: &str) -> Parsed {
let (rules, my_ticket, nearby) = raw.split("\n\n").next_tuple().unwrap();
Parsed {
rules: rules.lines().map_into().collect(),
my_ticket: my_ticket
.split_once('\n')
.unwrap()
.1
.split(',')
.map(|n| n.parse().unwrap())
.collect(),
nearby_tickets: nearby
.lines()
.skip(1)
.map(|l| l.split(',').map(|n| n.parse().unwrap()).collect())
.collect(),
my_ticket: parse_nums(my_ticket.split_once('\n').unwrap().1),
nearby_tickets: nearby.lines().skip(1).map(parse_nums).collect(),
}
}
// Could be optimized by folding the ranges first to only have one lower and one upper (or even
// just a single one if they’re continuous), but meh.
// Could be optimized by merging overlapping ranges into one before checking all the tickets.
// … or so I thought until I tried, benchmarked, and saw that that takes twice as long.
fn part1(parsed: &Parsed) -> usize {
parsed
.nearby_tickets

View File

@ -9,3 +9,8 @@ pub fn read_file(day: usize) -> String {
)
.unwrap()
}
#[inline]
pub fn parse_nums(l: &str) -> Vec<usize> {
l.split(',').filter_map(|n| n.parse().ok()).collect()
}