1
0
Fork 0
aoc2021/src/main.rs

87 lines
1.9 KiB
Rust
Raw Normal View History

2021-12-01 05:51:55 +00:00
#![feature(test)]
extern crate test;
2021-12-03 05:38:45 +00:00
const INPUTS: [&'static str; 2] = [
include_str!("../inputs/day3.sample.txt"),
include_str!("../inputs/day3.txt"),
];
2021-12-02 05:14:07 +00:00
2021-12-03 05:38:45 +00:00
fn cmds(input: &'static str) -> Vec<u64> {
input
.lines()
.filter(|&x| !x.is_empty())
.map(|x| u64::from_str_radix(x, 2).unwrap())
.collect()
2021-12-01 05:16:08 +00:00
}
2021-12-03 05:38:45 +00:00
fn solution<const MAX_LINE_LENGTH: usize>(levels: &[u64]) -> i64 {
let dominant_mask = count_bits::<MAX_LINE_LENGTH>(&levels, true);
2021-12-01 05:16:08 +00:00
2021-12-03 05:38:45 +00:00
let mut gamma = 0;
for i in (0..MAX_LINE_LENGTH).rev() {
let v = dominant_mask & (1 << i);
gamma <<= 1;
2021-12-02 05:18:04 +00:00
2021-12-03 05:38:45 +00:00
if v > 0 {
gamma |= 1;
} else {
gamma |= 0
2021-12-02 05:14:07 +00:00
}
2021-12-03 05:38:45 +00:00
}
2021-12-02 05:14:07 +00:00
2021-12-03 05:38:45 +00:00
let epsilon = !gamma & ((1 << MAX_LINE_LENGTH as i64) - 1);
gamma * epsilon
2021-12-02 05:18:04 +00:00
}
2021-12-03 05:38:45 +00:00
fn count_bits<const LINE_LENGTH: usize>(levels: &[u64], dominant: bool) -> u64 {
let n = levels.len();
let mut count = [0; LINE_LENGTH];
for &level in levels.iter() {
for i in 0..LINE_LENGTH {
let c = level & (1 << i);
2021-12-02 05:14:07 +00:00
2021-12-03 05:38:45 +00:00
if c != 0 {
count[i as usize] += 1;
2021-12-02 05:18:04 +00:00
}
2021-12-01 05:16:08 +00:00
}
}
2021-12-03 05:38:45 +00:00
let mut mask = 0;
for (i, c) in count.into_iter().enumerate() {
if c >= n - c {
mask |= 1 << i;
}
}
if dominant {
mask
} else {
!mask
}
2021-12-01 05:51:55 +00:00
}
fn main() {
2021-12-03 05:38:45 +00:00
let moves = cmds(INPUTS[0]);
let count = solution::<5>(&moves);
println!("Result {}", count);
let moves = cmds(INPUTS[1]);
let count = solution::<12>(&moves);
println!("Result {}", count);
2021-12-01 05:16:08 +00:00
}
2021-12-01 05:51:55 +00:00
#[bench]
fn solution_bench(b: &mut test::Bencher) {
2021-12-03 05:38:45 +00:00
let moves = cmds(INPUTS[1]);
2021-12-01 05:51:55 +00:00
b.iter(|| {
2021-12-03 05:38:45 +00:00
let v = solution::<12>(&moves);
2021-12-01 05:51:55 +00:00
test::black_box(v);
})
}
#[test]
fn solution_test() {
2021-12-03 05:38:45 +00:00
assert_eq!(solution::<5>(&cmds(INPUTS[0])), 198);
assert_eq!(solution::<12>(&cmds(INPUTS[1])), 3687446);
2021-12-01 05:51:55 +00:00
}