Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit94e7086

Browse files
committed
day24 part 2
1 parent822a792 commit94e7086

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

‎day24/src/main.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ use std::io::prelude::*;
44
use std::fs::File;
55
use itertools::Itertools;
66

7+
fncalculate_qe(weights:&Vec<u16>,groups_num:u16,start_from:usize) ->u64{
8+
let sum:u16 = weights.iter().fold(0, |sum,&w| sum + w);
9+
let group_weight = sum / groups_num;
10+
let qe:u64 =(start_from..weights.len())
11+
.map(|w|{
12+
let groups:Vec<Vec<&u16>> = weights.iter()
13+
.combinations_n(w)
14+
.filter(|w_list:&Vec<&u16>| w_list.iter().fold(0, |sum,&w| sum + w) == group_weight)
15+
.collect();
16+
match groups.iter()
17+
.map(|w_list:&Vec<&u16>| w_list.iter().fold(1, |pro,&&w| pro* wasu64))
18+
.min(){
19+
Some(m) => m,
20+
_ =>0,
21+
}
22+
})
23+
.skip_while(|&x| x ==0)
24+
.take(1)
25+
.collect::<Vec<u64>>()[0];
26+
qe
27+
28+
}
29+
730
fnmain(){
831

932
letmut content =String::new();
@@ -17,27 +40,9 @@ fn main() {
1740
typeScheme =Vec<u16>;
1841

1942
let weights:Vec<u16> = content.trim().split("\n").map(|w| w.parse().unwrap()).collect();
20-
let sum:u16 = weights.iter().fold(0, |sum,&w| sum + w);
21-
let group_weight = sum /3;
22-
let qe:u64 =(5..weights.len())
23-
.map(|w|{
24-
let groups:Vec<Vec<&u16>> = weights.iter()
25-
.combinations_n(w)
26-
.filter(|w_list:&Vec<&u16>| w_list.iter().fold(0, |sum,&w| sum + w) == group_weight)
27-
.collect();
28-
match groups.len() >0{
29-
true =>{
30-
groups.iter()
31-
.map(|w_list:&Vec<&u16>| w_list.iter().fold(1, |pro,&&w| pro* wasu64))
32-
.min()
33-
.unwrap()
34-
}
35-
false =>0,
36-
}
37-
})
38-
.skip_while(|&x| x ==0)
39-
.take(1)
40-
.collect::<Vec<u64>>()[0];
43+
let qe:u64 =calculate_qe(&weights,3,5);
44+
println!("{}", qe);
45+
let qe:u64 =calculate_qe(&weights,4,4);
4146
println!("{}", qe);
4247

4348
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp