mirror of
https://github.com/tomru/adventofcode.git
synced 2026-03-03 14:37:22 +01:00
task 2 day03 - feels still noob'y
This commit is contained in:
12
day03/data/sample
Normal file
12
day03/data/sample
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
00100
|
||||||
|
11110
|
||||||
|
10110
|
||||||
|
10111
|
||||||
|
10101
|
||||||
|
01111
|
||||||
|
00111
|
||||||
|
11100
|
||||||
|
10000
|
||||||
|
11001
|
||||||
|
00010
|
||||||
|
01010
|
||||||
@@ -12,9 +12,57 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn task2(data: &Vec<Vec<bool>>) {
|
fn task2(data: &Vec<Vec<bool>>) {
|
||||||
let a = filter_by_first_bit_n(data, true, 0);
|
println!("oxygen generator rating");
|
||||||
|
let oxygen_generator_rating = get_rating(data, 0, true);
|
||||||
|
println!("co2 scrubber");
|
||||||
|
let co2_scrubber_rating = get_rating(data, 0, false);
|
||||||
|
println!("live support rating {:?}", to_dec(&co2_scrubber_rating) * to_dec(&oxygen_generator_rating));
|
||||||
|
}
|
||||||
|
|
||||||
println!("{:?}", a);
|
fn get_rating(data: &Vec<Vec<bool>>, index: usize, oxygen_flag: bool) -> Vec<bool> {
|
||||||
|
let filter_value = most_common_at_index(data, index, oxygen_flag);
|
||||||
|
println!("filter value {:?}", filter_value);
|
||||||
|
let result: Vec<Vec<bool>> = data
|
||||||
|
.iter()
|
||||||
|
.filter(|v| v[index] == filter_value)
|
||||||
|
.cloned()
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if index > data[0].len() {
|
||||||
|
panic!("index out of bounds for first vector")
|
||||||
|
}
|
||||||
|
|
||||||
|
if result.len() == 1 {
|
||||||
|
return result[0].clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
return get_rating(&result, index + 1, oxygen_flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn most_common_at_index(data: &Vec<Vec<bool>>, index: usize, oxygen_flag: bool) -> bool {
|
||||||
|
let half = data.len() as f32 / 2 as f32;
|
||||||
|
let true_count = data.into_iter().fold(0, |accum, item| {
|
||||||
|
if *item.get(index).unwrap() {
|
||||||
|
accum + 1
|
||||||
|
} else {
|
||||||
|
accum
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return if oxygen_flag { true_count as f32 >= half} else {half > true_count as f32};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_dec(measurement: &Vec<bool>) -> u32 {
|
||||||
|
return measurement
|
||||||
|
.into_iter()
|
||||||
|
.enumerate()
|
||||||
|
.fold(0, |accum, (pos, item)| {
|
||||||
|
let exp: u32 = (MAX_BITS - pos - 1) as u32;
|
||||||
|
if *item {
|
||||||
|
accum + (2 as u32).pow(exp)
|
||||||
|
} else {
|
||||||
|
accum
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn task1(data: &Vec<Vec<bool>>) {
|
fn task1(data: &Vec<Vec<bool>>) {
|
||||||
@@ -33,20 +81,11 @@ fn task1(data: &Vec<Vec<bool>>) {
|
|||||||
println!("power consumption {}", epsilon * gamma);
|
println!("power consumption {}", epsilon * gamma);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn filter_by_first_bit_n(data: &Vec<Vec<bool>>, value: bool, n: usize) -> Vec<bool> {
|
|
||||||
let result: Vec<Vec<bool>> = data.iter().filter(|v| v[n] == value).cloned().collect();
|
|
||||||
|
|
||||||
if result.len() == 1 {
|
|
||||||
return result[0].clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
return filter_by_first_bit_n(&result, value, n+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse(data: &String) -> Vec<Vec<bool>> {
|
fn parse(data: &String) -> Vec<Vec<bool>> {
|
||||||
let mut result: Vec<Vec<bool>> = vec![];
|
let mut result: Vec<Vec<bool>> = vec![];
|
||||||
for line in data.lines() {
|
for line in data.lines() {
|
||||||
let thing: Vec<bool> = line.chars()
|
let thing: Vec<bool> = line
|
||||||
|
.chars()
|
||||||
.map(|v| if v == '1' { return true } else { return false })
|
.map(|v| if v == '1' { return true } else { return false })
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@@ -60,10 +99,11 @@ fn count(data: &Vec<Vec<bool>>, value: bool) -> Vec<u16> {
|
|||||||
let mut result = vec![0; data[0].len()];
|
let mut result = vec![0; data[0].len()];
|
||||||
for item in data.iter() {
|
for item in data.iter() {
|
||||||
for (pos, v) in item.iter().enumerate() {
|
for (pos, v) in item.iter().enumerate() {
|
||||||
if *v == value { result[pos] += 1 }
|
if *v == value {
|
||||||
|
result[pos] += 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user