```ruby @acc = [] @gamma = '' @epsilon = '' def add_to_acc(n, c) @acc[c].nil? ? @acc.insert(c, [n]) : @acc[c] << n end ########################################################## f = File.open("input.txt") f.readlines.each_with_index do |line, i| line.delete("\n").split('').map(&:to_i).each_with_index { |n, c| add_to_acc(n, c) } end @acc.each do |c| ones = c.filter { |n| n == 1 }.count zeroes = c.filter { |n| n == 0 }.count if ones > zeroes @gamma += '1' @epsilon += '0' # fucking faulheit else @gamma += '0' @epsilon += '1' # fucking faulheit end end puts "gamma: #{@gamma} - epsilon: #{@epsilon}" puts @epsilon.to_i(2) * @gamma.to_i(2) ``` ```rust= #[aoc_generator(day3)] fn parse_input_day3(input: &str) -> Vec<Vec<bool>> { input.lines().map(|l| { l.chars().map(|c| c == '1').collect::<Vec<bool>>() }).collect::<Vec<Vec<bool>>>() } fn get_commons(lines: &Vec<Vec<bool>>, most: bool) -> Vec<bool> { lines.iter().fold(vec![0; lines.first().unwrap_or(&Vec::new()).len()], | sum, l | { l.iter().zip(sum).map(|(b, count)| count + (*b as usize) ).collect() }).iter().map(|count| if most { count >= &(lines.len() - count) } else { count < &(lines.len() - count) }).collect::<Vec<bool>>() } fn bool_vec_to_dec(v: &Vec<bool>) -> u64 { v.iter().fold(0, |num, value| (num << 1) + *value as u64) } #[aoc(day3, part1)] fn solve_part1(lines: &Vec<Vec<bool>>) -> u64 { let commons = get_commons(lines, true); let gamma = bool_vec_to_dec(&commons); let epsilon = !gamma & ((1<<commons.len()) - 1); gamma * epsilon } fn reduce_to_single_by_significant(lines: &Vec<Vec<bool>>, pos: usize, most: bool) -> Vec<bool> { let commons = get_commons(lines, most); let lines: Vec<Vec<bool>> = lines.to_owned().into_iter().filter(|el| el[pos] == commons[pos]).collect(); if lines.len() == 1 { lines[0].to_owned() } else { reduce_to_single_by_significant(&lines, pos + 1, most) } } #[aoc(day3, part2)] fn solve_part2(lines: &Vec<Vec<bool>>) -> u64 { let oxygen_rating = bool_vec_to_dec(&reduce_to_single_by_significant(&lines, 0, true)); let co2_scrubber_rating = bool_vec_to_dec(&reduce_to_single_by_significant(&lines, 0, false)); oxygen_rating * co2_scrubber_rating } ``` ```rust #[aoc_generator(day1)] fn parse_input_day1(input: &str) -> Result<Vec<i32>, ParseIntError> { input.lines().map(|l| l.parse()).collect() } #[aoc(day1, part1)] fn solve_part1(depths: &[i32]) -> usize { depths.windows(2).filter(|w| w[0] < w[1]).count() } #[aoc(day1, part2)] fn solve_part2(depths: &[i32]) -> usize { let windowed_depths = depths .windows(3) .map(|w| w[0] + w[1] + w[2]) .collect::<Vec<i32>>(); solve_part1(&windowed_depths) } ```