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 main() { | |
let scanners = parse_scanners(INPUT); | |
let map = Map::from(scanners); | |
println!("Beacons: {}", map.beacon_count()); | |
println!("Manhattan distance: {:?}", map.max_manhattan_distance()); | |
} | |
fn parse_scanners(input: &str) -> Vec<Scanner> { | |
let scanners = input |
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 magnitude(numbers: &[&str]) -> u64 { | |
let total = SNumber::from(numbers[0]); | |
numbers[1..] | |
.iter() | |
.fold(total, |total, &number| total + SNumber::from(number)) | |
.magnitude() | |
} | |
fn max_magnitude(numbers: &[&str]) -> 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 main() { | |
let hits = find_all_hits((128, 160, -142, -88)); | |
let max_height = hits.iter().map(|&(_, y)| triangle(y)).max().unwrap(); | |
let total_hits = hits.len(); | |
println!("The highest position reached was {}.", max_height); | |
println!("The total number of hits was {}.", total_hits); | |
} |
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::collections::VecDeque; | |
fn main() { | |
let transmission = "20546C8802538E136091C1802689BCD7DA45948D319D1B100747A009C97696E8B4ABFCA6AB8F4F26C401964A6271C80F802D392C01CEDDCE6E5CB829802F600A00021B14E34C361006E0AC418BB2CA6800BE4599BB6A73507002A52BEEB14D201802F600849E64D3369D37C74100866785B3D0ADFD8E601E5EB9DE2366D93ECB8B040142CB8ACE07CCB5CF34CA89380410B6134CE6FEF104A2B200243396976A00401A45004313D68435DBDDDA61CE6428C01491AEBF0C7E580AE00CCC401B86514216880370EE3443D2013DF003750004361343D88800084C4C8B116A679018300740010C8571BA32080350DA0D42800043A3044189AE0174B314D76E1F3ACF3BDAE3EE7298FF134002EF9DBCD0644127E3CAE7FCBA9A80393544F9A927C973DF1A500965A5CEA94C4DDA5658B94C6C3002A798A629CF21280532BAB4F4C7271E45EE6E71D8143A9BC7948804AB94D1D6006AC200EC1E8A10C00010985316A35C3620061E641644D661A4C012993E99208FC60097802F28F528F738606008CA47205400814C89CC8890064D400AB4BE0A66F2BF253E73AE8401424A7BFB16C0037E06CE0641E0013B08010A8930CE2B980351161DC3730066274188B020054A5E16965940057895ADEB5BF56A635ADE2354191D70566273A6F5B078266 |
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
#[derive(Copy, Clone)] | |
struct Point { | |
x: usize, | |
y: usize, | |
} | |
impl Point { | |
fn new(x: usize, y: usize) -> Self { | |
Self { x, y } | |
} |
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(template: &[char], rules: &HashMap<(char, char), char>, steps: u64) -> u64 { | |
let mut total = HashMap::new(); | |
let mut counts = HashMap::new(); | |
template.windows(2).for_each(|pair| { | |
count_for_pair(pair[0], pair[1], rules, steps, &mut counts); | |
merge(&mut total, &counts[&(pair[0], pair[1], steps)]); | |
}); | |
template.iter().for_each(|&element| { |
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
#[derive(Clone, Copy)] | |
enum Fold { | |
Horizontal(usize), | |
Vertical(usize), | |
} | |
impl From<&str> for Fold { | |
fn from(fold: &str) -> Self { | |
let (instruction, position) = fold.split_once('=').unwrap(); | |
let position = position.parse::<usize>().unwrap(); |
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_paths(connections: HashMap<&str, Vec<&str>>, can_revisit: bool) -> u64 { | |
let mut path = vec!["start"]; | |
find_path(&connections, &mut path, can_revisit) | |
} | |
fn find_path<'a>( | |
connections: &HashMap<&'a str, Vec<&'a str>>, | |
path: &mut Vec<&'a str>, | |
can_revisit: bool, | |
) -> 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
struct Map { | |
octopuses: Vec<Vec<u32>>, | |
width: usize, | |
height: usize, | |
} | |
impl Map { | |
fn parse(input: &str) -> Self { | |
let octopuses = input | |
.lines() |
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 parse(subsystem: Vec<Vec<char>>) -> u64 { | |
subsystem | |
.iter() | |
.map(|line| { | |
if let (Some(c), _) = check_line(line) { | |
match c { | |
')' => 3, | |
']' => 57, | |
'}' => 1197, | |
'>' => 25137, |