Skip to content

Instantly share code, notes, and snippets.

View neofight78's full-sized avatar

William Hoggarth neofight78

View GitHub Profile
@neofight78
neofight78 / main.rs
Created June 20, 2021 16:43
Simple FEN Parser v1
use std::fmt::{Display, Formatter};
use std::{env, fmt, process};
#[derive(Debug, Copy, Clone)]
enum Pieces {
None,
WPawn,
WKnight,
WBishop,
WRook,
@neofight78
neofight78 / sonar_sweep.rs
Created December 1, 2021 20:19
Advent of Code 2021 - Day 1: Sonar Sweep
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>>(),
@neofight78
neofight78 / dive.rs
Last active December 2, 2021 10:18
Advent of Code 2021 - Day 2: Dive!
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 {
@neofight78
neofight78 / binary_diagnostic.rs
Created December 3, 2021 13:25
Advent of Code 2021 - Day 3: Binary Diagnostic
// 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))
}
@neofight78
neofight78 / giant_squid.rs
Created December 4, 2021 11:35
Advent of Code 2021 - Day 4: Giant Squid
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);
@neofight78
neofight78 / hydrothermal_venture.rs
Last active December 5, 2021 10:31
Advent of Code 2021 - Day 5: Hydrothermal Venture
struct Point {
x: i16,
y: i16,
}
struct Line {
start: Point,
end: Point,
}
@neofight78
neofight78 / lanternfish.rs
Last active December 6, 2021 08:55
Advent of Code 2021 - Day 6: Lanternfish
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];
}
@neofight78
neofight78 / the_treachery_of_whales.rs
Created December 7, 2021 07:29
Advent of Code 2021 - Day 7: The Treachery of Whales
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| {
@neofight78
neofight78 / seven_segment_search.rs
Last active December 8, 2021 16:52
Advent of Code 2021 - Day 8: Seven Segment Search
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()
}
@neofight78
neofight78 / seven_segment_search.rs
Last active December 8, 2021 17:21
Advent of Code 2021 - Day 8: Seven Segment Search (Original)
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()
}