mirror of
https://github.com/tomru/adventofcode.git
synced 2026-03-03 06:27:19 +01:00
remove js of day1
This commit is contained in:
@@ -1,24 +0,0 @@
|
||||
import * as readline from 'node:readline';
|
||||
import * as fs from 'node:fs';
|
||||
|
||||
|
||||
const data = [];
|
||||
const rl = readline.createInterface({
|
||||
input: fs.createReadStream('./input')
|
||||
});
|
||||
|
||||
for await (const line of rl) {
|
||||
data.push(parseInt(line, 10));
|
||||
}
|
||||
|
||||
|
||||
let last = null;
|
||||
let part1 = 0;
|
||||
|
||||
for (const v in data) {
|
||||
if (last !== null && v > last) counter++;
|
||||
last = v;
|
||||
}
|
||||
|
||||
console.log('part1', part1);
|
||||
|
||||
@@ -3,29 +3,67 @@ use std::fs;
|
||||
const MAX_BITS: usize = 12;
|
||||
|
||||
fn main() {
|
||||
let mut true_count: [ u16 ; MAX_BITS ] = [ 0 ; MAX_BITS ];
|
||||
let mut line_count: u16 = 0;
|
||||
let data = fs::read_to_string("./data/input").expect("Unable to read file");
|
||||
let input = fs::read_to_string("./data/input").expect("Unable to read file");
|
||||
let data = parse(&input);
|
||||
|
||||
for line in data.lines() {
|
||||
let str_rev: String = line.chars().rev().collect();
|
||||
for (pos, chr) in str_rev.chars().enumerate() {
|
||||
if chr == '1' {
|
||||
true_count[pos] += 1
|
||||
}
|
||||
}
|
||||
line_count += 1;
|
||||
}
|
||||
task1(&data);
|
||||
|
||||
task2(&data);
|
||||
}
|
||||
|
||||
fn task2(data: &Vec<Vec<bool>>) {
|
||||
let a = filter_by_first_bit_n(data, true, 0);
|
||||
|
||||
println!("{:?}", a);
|
||||
}
|
||||
|
||||
fn task1(data: &Vec<Vec<bool>>) {
|
||||
let trues = count(&data, true);
|
||||
|
||||
let even_count = line_count / 2;
|
||||
let mut gamma: u32 = 0;
|
||||
let mut epsilon: u32 = 0;
|
||||
for (pos, true_count) in true_count.iter_mut().enumerate() {
|
||||
if *true_count > even_count {
|
||||
gamma += (2 as u32).pow(pos as u32);
|
||||
for (pos, true_count) in trues.iter().enumerate() {
|
||||
let exp: u32 = (MAX_BITS - pos - 1) as u32;
|
||||
if *true_count > data.len() as u16 / 2 {
|
||||
gamma += (2 as u32).pow(exp);
|
||||
} else {
|
||||
epsilon += (2 as u32).pow(pos as u32);
|
||||
epsilon += (2 as u32).pow(exp);
|
||||
}
|
||||
}
|
||||
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>> {
|
||||
let mut result: Vec<Vec<bool>> = vec![];
|
||||
for line in data.lines() {
|
||||
let thing: Vec<bool> = line.chars()
|
||||
.map(|v| if v == '1' { return true } else { return false })
|
||||
.collect();
|
||||
|
||||
result.push(thing);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
fn count(data: &Vec<Vec<bool>>, value: bool) -> Vec<u16> {
|
||||
let mut result = vec![0 ; data[0].len()];
|
||||
for item in data.iter() {
|
||||
for (pos, v) in item.iter().enumerate() {
|
||||
if *v == value { result[pos] += 1 }
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user