Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::env;
- use std::io::{self};
- use rayon::prelude::*;
- extern crate regex;
- use regex::Regex;
- extern crate itertools;
- use itertools::Itertools;
- struct Mapping {
- dest_range_start: usize,
- source_range_start: usize,
- range_length: usize,
- }
- impl Mapping {
- pub fn in_source_range(&self, number: usize) -> bool {
- number >= self.source_range_start && number <= self.source_range_start + self.range_length - 1
- }
- }
- impl From<&str> for Mapping {
- fn from(s: &str) -> Self {
- let re = Regex::new(r"(\d+) (\d+) (\d+)").unwrap();
- let matches = re.captures(&s).unwrap();
- Self {
- dest_range_start: matches[1].parse().unwrap(),
- source_range_start: matches[2].parse().unwrap(),
- range_length: matches[3].parse().unwrap(),
- }
- }
- }
- struct Day5Map {
- mappings: Vec<Mapping>,
- }
- impl Day5Map {
- fn load_mappings(lines: &str) -> Self {
- let mappings = lines.split("\n").skip(1).map(Mapping::from).collect();
- Self { mappings: mappings }
- }
- pub fn destination(&self, from: usize) -> usize {
- for map in &self.mappings {
- if map.in_source_range(from) {
- return map.dest_range_start + (from - map.source_range_start);
- }
- }
- from
- }
- }
- fn read_seeds(s: &str) -> Vec<usize> {
- let re = Regex::new(r"(\d+)").unwrap();
- let matches: Vec<_> = re
- .find_iter(s)
- .map(|x| x.as_str().parse::<usize>().unwrap())
- .collect();
- matches
- }
- fn solve(input: &str) -> io::Result<()> {
- // Input
- let input_str = std::fs::read_to_string(input).unwrap();
- let input_str = input_str.trim();
- let input: Vec<_> = input_str.split("\n\n").collect();
- // Seeds & mappings
- let seeds = read_seeds(input[0]);
- let seed_to_soil_map = Day5Map::load_mappings(input[1]);
- let soil_to_fertilizer_map = Day5Map::load_mappings(input[2]);
- let fertilizer_to_water_map = Day5Map::load_mappings(input[3]);
- let water_to_light_map = Day5Map::load_mappings(input[4]);
- let light_to_temperature_map = Day5Map::load_mappings(input[5]);
- let temperature_to_humidity_map = Day5Map::load_mappings(input[6]);
- let humidity_to_location_map = Day5Map::load_mappings(input[7]);
- // Part 1
- let part1 = seeds
- .iter()
- .map(|x| seed_to_soil_map .destination(*x))
- .map(|x| soil_to_fertilizer_map .destination( x))
- .map(|x| fertilizer_to_water_map .destination( x))
- .map(|x| water_to_light_map .destination( x))
- .map(|x| light_to_temperature_map .destination( x))
- .map(|x| temperature_to_humidity_map.destination( x))
- .map(|x| humidity_to_location_map .destination( x))
- .min()
- .unwrap();
- println!("Part 1: {part1}"); // 579439039
- let mut part2 = usize::max_value();
- for (start,range) in seeds.iter().tuples() {
- let new = (*start..(*start + *range))
- .into_par_iter()
- .map(|x| seed_to_soil_map .destination( x))
- .map(|x| soil_to_fertilizer_map .destination( x))
- .map(|x| fertilizer_to_water_map .destination( x))
- .map(|x| water_to_light_map .destination( x))
- .map(|x| light_to_temperature_map .destination( x))
- .map(|x| temperature_to_humidity_map.destination( x))
- .map(|x| humidity_to_location_map .destination( x))
- .min()
- .unwrap();
- part2 = std::cmp::min(part2,new);
- }
- println!("Part 2: {part2}"); // 7873084
- Ok(())
- }
- fn main() {
- let args: Vec<String> = env::args().collect();
- let filename = &args[1];
- solve(&filename).unwrap();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement