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

Commit6bd237d

Browse files
committed
day3
1 parentc26109e commit6bd237d

File tree

2 files changed

+138
-5
lines changed

2 files changed

+138
-5
lines changed

‎advent-of-code/2021/.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ target/
1111
**/*.rs.bk
1212

1313
# MSVC Windows builds of rustc generate these, which store debugging information
14-
*.pdb
14+
*.pdb
15+
16+
# Others
17+
Advent-Of-Code-2021
18+
AdventOfCode2021
19+
adventofcode-2021

‎advent-of-code/2021/day03/src/main.rs

Lines changed: 132 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,141 @@ fn part_1(input: &Vec<&str>) {
3636
println!("part_1={}",usize::from_str_radix(&gamma_rate.to_string(),2).unwrap()*usize::from_str_radix(&epsilon_rate.to_string(),2).unwrap());
3737
}
3838

39-
fnpart_2(input:&Vec<&str>){
40-
// TODO
41-
println!("part_2={}",1);
39+
40+
typeMatrix<T> =Vec<Vec<T>>;
41+
42+
pubfngenerator(input:&str) ->Matrix<char>{
43+
input
44+
.lines()
45+
.map(|line| line.trim())
46+
.map(|line| line.chars().collect())
47+
.collect()
48+
}
49+
50+
51+
fnpart_2(input:&[Vec<char>]){
52+
// let pos_len = input[0].len();
53+
// let mut oxygen_input = input;
54+
// let mut oxygen_raw = vec![[0, 0]; pos_len];
55+
// let mut oxygen_gen = 0;
56+
57+
// let mut co2_input = oxygen_input.clone();
58+
// let mut co2_raw = oxygen_raw.clone();
59+
// let mut co2_scrub = 0;
60+
61+
// for i in 0..pos_len {
62+
// // Filter oxygen
63+
// let i_max = get_i_max(&oxygen_raw, i);
64+
// oxygen_raw = gen_raw(oxygen_raw, pos_len, &oxygen_input);
65+
// oxygen_input = oxygen_input
66+
// .iter()
67+
// .filter(|x| x.chars().into_iter().nth(i).unwrap() == i_max)
68+
// .copied()
69+
// .collect();
70+
71+
// // Filter Co2
72+
// let i_max = get_i_max(&co2_raw, i);
73+
// co2_raw = gen_raw(co2_raw, pos_len, &co2_input);
74+
// co2_input = co2_input
75+
// .iter()
76+
// .filter(|x| x.chars().into_iter().nth(i).unwrap() != i_max)
77+
// .copied()
78+
// .collect();
79+
80+
// if oxygen_input.len() == 1 {
81+
// oxygen_gen = isize::from_str_radix(oxygen_input.first().unwrap(), 2).unwrap();
82+
// }
83+
84+
// if co2_input.len() == 1 {
85+
// co2_scrub = isize::from_str_radix(co2_input.first().unwrap(), 2).unwrap();
86+
// }
87+
// }
88+
89+
let num_length = input[0].len();// yeah, again, super unsafe
90+
letmut o2_reports = input.to_vec();
91+
letmut co2_reports = input.to_vec();
92+
93+
for iin0..num_length{
94+
let majority_digit_count = o2_reports
95+
.iter()
96+
.map(|line| line[i])
97+
.fold((0,0), |acc, d|match d{
98+
'0' =>(acc.0 +1, acc.1),
99+
'1' =>(acc.0, acc.1 +1),
100+
_ =>unreachable!(),
101+
});
102+
103+
let majority_digit =if majority_digit_count.0 > majority_digit_count.1{
104+
'0'
105+
}else{
106+
'1'
107+
};
108+
109+
o2_reports.retain(|line| line[i] == majority_digit);
110+
if o2_reports.len() ==1{
111+
break;
112+
}
113+
}
114+
115+
for iin0..num_length{
116+
let digit_count = co2_reports
117+
.iter()
118+
.map(|line| line[i])
119+
.fold((0,0), |acc, d|match d{
120+
'0' =>(acc.0 +1, acc.1),
121+
'1' =>(acc.0, acc.1 +1),
122+
_ =>unreachable!(),
123+
});
124+
125+
let minority_digit =if digit_count.1 < digit_count.0{
126+
'1'
127+
}else{
128+
'0'
129+
};
130+
131+
co2_reports.retain(|line| line[i] == minority_digit);
132+
if co2_reports.len() ==1{
133+
break;
134+
}
135+
}
136+
137+
let o2_rating_string:String = o2_reports[0].iter().collect();
138+
let co2_rating_string:String = co2_reports[0].iter().collect();
139+
140+
let o2_rating = u32::from_str_radix(&o2_rating_string,2).unwrap();
141+
let co2_rating = u32::from_str_radix(&co2_rating_string,2).unwrap();
142+
143+
println!("part_2={}", o2_rating* co2_rating);
144+
//println!("part_2={}, {}", oxygen_gen, co2_scrub);
145+
}
146+
147+
// Helpers
148+
fnget_i_max(raw:&[[u32;2]],i:usize) ->char{
149+
if raw[i][0] > raw[i][1]{
150+
return'0';
151+
}
152+
return'1';
153+
}
154+
155+
fngen_raw(mutold:Vec<[u32;2]>,pos_len:usize,input:&[&str]) ->Vec<[u32;2]>{
156+
for iin0..pos_len{
157+
letmut z =0;
158+
letmut o =0;
159+
for jin input{
160+
match j.chars().nth(i).unwrap(){
161+
'0' => z +=1,
162+
'1' => o +=1,
163+
_ =>{}
164+
}
165+
}
166+
*old.get_mut(i).unwrap() =[z, o];
167+
}
168+
169+
old
42170
}
43171

44172
fnmain(){
45173
let input =parse_input(include_str!("../input.txt"));
46174
part_1(&input);
47-
part_2(&input);
175+
part_2(&generator(include_str!("../input.txt")));
48176
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp