This commit is contained in:
JonOfUs 2023-12-06 13:27:33 +01:00
parent 64429bfaf2
commit 2c3e222620
2 changed files with 64 additions and 41 deletions

View file

@ -45,55 +45,66 @@ pub fn solve() {
}) })
.collect(); .collect();
let mut current: HashSet<(usize,usize)> = seeds_raw.chunks(2) let mut current: HashSet<(usize, usize)> = seeds_raw
.map(|arr| (arr[0], arr[0]+arr[1]-1)) .chunks(2)
.map(|arr| (arr[0], arr[0] + arr[1] - 1))
.collect(); .collect();
let mut gen: HashSet<(usize,usize)>; let mut gen: HashSet<(usize, usize)>;
for ranges in maps_ranges.iter() { for ranges in maps_ranges.iter() {
let next = ranges.clone(); let next = ranges.clone();
gen = HashSet::<(usize,usize)>::new(); gen = HashSet::<(usize, usize)>::new();
let mut not_yet_mapped = current.clone(); let mut not_yet_mapped = current.clone();
for map in next { for map in next {
let mut tmp = HashSet::<(usize,usize)>::new(); let mut tmp = HashSet::<(usize, usize)>::new();
for (s,e) in not_yet_mapped.clone().iter() { for (s, e) in not_yet_mapped.clone().iter() {
if map[1] < *s && map[1]+map[2] >= *s && map[1]+map[2] <= *e { // left if map[1] < *s && map[1] + map[2] >= *s && map[1] + map[2] <= *e {
let len = map[1]+map[2] - *s; // left
let len = map[1] + map[2] - *s;
let dest = map[0] + (*s - map[1]); let dest = map[0] + (*s - map[1]);
gen.insert((dest, dest+len)); gen.insert((dest, dest + len));
tmp.insert((*s+len, *e)); tmp.insert((*s + len, *e));
} else if map[1] >= *s && map[1] <= *e && map[1]+map[2] > *e { // right } else if map[1] >= *s && map[1] <= *e && map[1] + map[2] > *e {
// right
let len = *e - map[1]; let len = *e - map[1];
let dest = map[0]; let dest = map[0];
gen.insert((dest,dest+len)); gen.insert((dest, dest + len));
tmp.insert((*s, *e-len)); tmp.insert((*s, *e - len));
} else if map[1] >= *s && map[1]+map[2] <= *e { // inner } else if map[1] >= *s && map[1] + map[2] <= *e {
// inner
let len = map[2]; let len = map[2];
let dest = map[0]; let dest = map[0];
gen.insert((dest,dest+len)); gen.insert((dest, dest + len));
tmp.insert((*s, map[1])); tmp.insert((*s, map[1]));
tmp.insert((map[1]+map[2], *e)); tmp.insert((map[1] + map[2], *e));
} else if map[1] < *s && map[1]+map[2] > *e { // outer } else if map[1] < *s && map[1] + map[2] > *e {
let len = *e-*s; // outer
let len = *e - *s;
let dest = map[0] + (*s - map[1]); let dest = map[0] + (*s - map[1]);
gen.insert((dest,dest+len)); gen.insert((dest, dest + len));
} else { } else {
tmp.insert((*s,*e)); tmp.insert((*s, *e));
} }
} }
not_yet_mapped = tmp; not_yet_mapped = tmp;
} }
not_yet_mapped.iter().for_each(|v| {gen.insert(*v);}); not_yet_mapped.iter().for_each(|v| {
gen.insert(*v);
});
current = gen; current = gen;
} }
let mut arr = current.iter().filter(|(s,e)| *s != *e).map(|(s,_)| *s).collect::<Vec::<usize>>(); let mut arr = current
.iter()
.filter(|(s, e)| *s != *e)
.map(|(s, _)| *s)
.collect::<Vec<usize>>();
arr.sort(); arr.sort();
println!("Result 2: {}", arr[0]); println!("Result 2: {}", arr[0]);
@ -118,7 +129,7 @@ fn parse_maps(seeds_arc: &Arc<Mutex<Vec<usize>>>, seeds_len: usize, contents: &V
let mut seeds = seeds_arc.lock().unwrap(); let mut seeds = seeds_arc.lock().unwrap();
if seeds[i] >= range_start && seeds[i] <= range_end { if seeds[i] >= range_start && seeds[i] <= range_end {
seeds[i] = ranges[j][0] + (seeds[i] - range_start); seeds[i] = ranges[j][0] + (seeds[i] - range_start);
break break;
} }
} }
}) })

View file

@ -6,44 +6,56 @@ pub fn solve() {
let contents = fs::read_to_string(path).expect("I/O error, wrong path?"); let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
//let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?")); //let contents = BufReader::new(fs::File::open(path).expect("I/O error, wrong path?"));
let times: Vec<usize> = contents.lines().next().unwrap().split_whitespace().skip(1).map(|s| s.parse::<usize>().unwrap()).collect(); let times: Vec<usize> = contents
let records: Vec<usize> = contents.lines().skip(1).next().unwrap().split_whitespace().skip(1).map(|s| s.parse::<usize>().unwrap()).collect(); .lines()
.next()
.unwrap()
.split_whitespace()
.skip(1)
.map(|s| s.parse::<usize>().unwrap())
.collect();
let records: Vec<usize> = contents
.lines()
.skip(1)
.next()
.unwrap()
.split_whitespace()
.skip(1)
.map(|s| s.parse::<usize>().unwrap())
.collect();
let distances: Vec<Vec<usize>> = times let distances: Vec<Vec<usize>> = times
.iter() .iter()
.map(|t| { .map(|t| {
(0..*t).map(|load_t| { (0..*t)
load_t * (t-load_t) .map(|load_t| load_t * (t - load_t))
}) .collect::<Vec<usize>>()
.collect::<Vec::<usize>>()
}) })
.collect(); .collect();
let result: usize = distances.iter() let result: usize = distances
.iter()
.enumerate() .enumerate()
.map(|(i, dist)| { .map(|(i, dist)| dist.iter().filter(|d| **d > records[i]).count())
dist.iter()
.filter(|d| **d > records[i])
.count()
})
.product(); .product();
println!("Result 1: {}", result); println!("Result 1: {}", result);
let act_time: usize = times.iter() let act_time: usize = times
.iter()
.map(|t| t.to_string()) .map(|t| t.to_string())
.collect::<String>() .collect::<String>()
.parse() .parse()
.unwrap(); .unwrap();
let act_record: usize = records.iter() let act_record: usize = records
.iter()
.map(|t| t.to_string()) .map(|t| t.to_string())
.collect::<String>() .collect::<String>()
.parse() .parse()
.unwrap(); .unwrap();
let result: usize = (0..act_time).map(|load_t| { let result: usize = (0..act_time)
load_t * (act_time-load_t) .map(|load_t| load_t * (act_time - load_t))
})
.filter(|d| *d > act_record) .filter(|d| *d > act_record)
.count(); .count();