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();
let mut current: HashSet<(usize,usize)> = seeds_raw.chunks(2)
.map(|arr| (arr[0], arr[0]+arr[1]-1))
let mut current: HashSet<(usize, usize)> = seeds_raw
.chunks(2)
.map(|arr| (arr[0], arr[0] + arr[1] - 1))
.collect();
let mut gen: HashSet<(usize,usize)>;
let mut gen: HashSet<(usize, usize)>;
for ranges in maps_ranges.iter() {
let next = ranges.clone();
gen = HashSet::<(usize,usize)>::new();
gen = HashSet::<(usize, usize)>::new();
let mut not_yet_mapped = current.clone();
for map in next {
let mut tmp = HashSet::<(usize,usize)>::new();
for (s,e) in not_yet_mapped.clone().iter() {
if map[1] < *s && map[1]+map[2] >= *s && map[1]+map[2] <= *e { // left
let len = map[1]+map[2] - *s;
let mut tmp = HashSet::<(usize, usize)>::new();
for (s, e) in not_yet_mapped.clone().iter() {
if map[1] < *s && map[1] + map[2] >= *s && map[1] + map[2] <= *e {
// left
let len = map[1] + map[2] - *s;
let dest = map[0] + (*s - map[1]);
gen.insert((dest, dest+len));
tmp.insert((*s+len, *e));
} else if map[1] >= *s && map[1] <= *e && map[1]+map[2] > *e { // right
gen.insert((dest, dest + len));
tmp.insert((*s + len, *e));
} else if map[1] >= *s && map[1] <= *e && map[1] + map[2] > *e {
// right
let len = *e - map[1];
let dest = map[0];
gen.insert((dest,dest+len));
tmp.insert((*s, *e-len));
} else if map[1] >= *s && map[1]+map[2] <= *e { // inner
gen.insert((dest, dest + len));
tmp.insert((*s, *e - len));
} else if map[1] >= *s && map[1] + map[2] <= *e {
// inner
let len = map[2];
let dest = map[0];
gen.insert((dest,dest+len));
gen.insert((dest, dest + len));
tmp.insert((*s, map[1]));
tmp.insert((map[1]+map[2], *e));
} else if map[1] < *s && map[1]+map[2] > *e { // outer
let len = *e-*s;
tmp.insert((map[1] + map[2], *e));
} else if map[1] < *s && map[1] + map[2] > *e {
// outer
let len = *e - *s;
let dest = map[0] + (*s - map[1]);
gen.insert((dest,dest+len));
gen.insert((dest, dest + len));
} else {
tmp.insert((*s,*e));
tmp.insert((*s, *e));
}
}
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;
}
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();
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();
if seeds[i] >= range_start && seeds[i] <= range_end {
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 = 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 records: Vec<usize> = contents.lines().skip(1).next().unwrap().split_whitespace().skip(1).map(|s| s.parse::<usize>().unwrap()).collect();
let times: Vec<usize> = contents
.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
.iter()
.map(|t| {
(0..*t).map(|load_t| {
load_t * (t-load_t)
})
.collect::<Vec::<usize>>()
(0..*t)
.map(|load_t| load_t * (t - load_t))
.collect::<Vec<usize>>()
})
.collect();
let result: usize = distances.iter()
let result: usize = distances
.iter()
.enumerate()
.map(|(i, dist)| {
dist.iter()
.filter(|d| **d > records[i])
.count()
})
.map(|(i, dist)| dist.iter().filter(|d| **d > records[i]).count())
.product();
println!("Result 1: {}", result);
let act_time: usize = times.iter()
let act_time: usize = times
.iter()
.map(|t| t.to_string())
.collect::<String>()
.parse()
.unwrap();
let act_record: usize = records.iter()
let act_record: usize = records
.iter()
.map(|t| t.to_string())
.collect::<String>()
.parse()
.unwrap();
let result: usize = (0..act_time).map(|load_t| {
load_t * (act_time-load_t)
})
let result: usize = (0..act_time)
.map(|load_t| load_t * (act_time - load_t))
.filter(|d| *d > act_record)
.count();