This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::fmt::{Display, Formatter}; | |
| use std::{env, fmt, process}; | |
| #[derive(Debug, Copy, Clone)] | |
| enum Pieces { | |
| None, | |
| WPawn, | |
| WKnight, | |
| WBishop, | |
| WRook, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn count_increments(measurements: &[u64]) -> usize { | |
| measurements.windows(2).filter(|&w| w[1] > w[0]).count() | |
| } | |
| fn count_three_measurement_increments(measurements: &[u64]) -> usize { | |
| count_increments( | |
| &measurements | |
| .windows(3) | |
| .map(|w| w.iter().sum()) | |
| .collect::<Vec<u64>>(), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn plot_course(instructions: &[&str]) -> i64 { | |
| let mut pos = 0; | |
| let mut depth = 0; | |
| for &instruction in instructions { | |
| let mut parts = instruction.split_whitespace(); | |
| let direction = parts.next().unwrap(); | |
| let units = parts.next().unwrap().parse::<i64>().unwrap(); | |
| match direction { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // bits = width in bits of the input data | |
| fn power_consumption(numbers: &[u64], bits: usize) -> u64 { | |
| let gamma = (0..bits).fold(0, |gamma, bit| { | |
| let (ones, zeros) = count(numbers, bit); | |
| gamma | if ones > zeros { 1 << bit } else { 0 } | |
| }); | |
| gamma * (!gamma & ((1 << bits) - 1)) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Board { | |
| numbers: [[u64; 5]; 5], | |
| marked: u64, | |
| has_won: bool, | |
| } | |
| impl Board { | |
| fn is_marked(&self, col: usize, row: usize) -> bool { | |
| let mask = (1 << col) << (row * 8); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct Point { | |
| x: i16, | |
| y: i16, | |
| } | |
| struct Line { | |
| start: Point, | |
| end: Point, | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn breed(fish: &[u8], generations: u64) -> u64 { | |
| let mut lifetimes = vec![0u64; 9]; | |
| fish.iter().for_each(|&f| lifetimes[f as usize] += 1); | |
| for _ in 0..generations { | |
| lifetimes.rotate_left(1); | |
| lifetimes[6] += lifetimes[8]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn align(positions: &[i64]) -> i64 { | |
| (*positions.iter().min().unwrap()..=*positions.iter().max().unwrap()) | |
| .map(|target| positions.iter().map(|&p| (p - target).abs()).sum()) | |
| .min() | |
| .unwrap() | |
| } | |
| fn align2(positions: &[i64]) -> i64 { | |
| (*positions.iter().min().unwrap()..=*positions.iter().max().unwrap()) | |
| .map(|target| { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn count(notes: Vec<(Vec<&str>, Vec<&str>)>) -> usize { | |
| notes | |
| .iter() | |
| .flat_map(|(_, output)| { | |
| output | |
| .iter() | |
| .filter(|digit| [2, 4, 3, 7].contains(&digit.len())) | |
| }) | |
| .count() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fn count(notes: Vec<(Vec<&str>, Vec<&str>)>) -> usize { | |
| notes | |
| .iter() | |
| .flat_map(|(_, output)| { | |
| output | |
| .iter() | |
| .filter(|digit| [2, 4, 3, 7].contains(&digit.len())) | |
| }) | |
| .count() | |
| } |
OlderNewer