Skip to content

Commit

Permalink
Update 2023-08
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Dec 8, 2023
1 parent 410fb45 commit f316954
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 35 deletions.
1 change: 1 addition & 0 deletions crates/core/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod chunk_iterator;
#[cfg(feature = "webgpu-compute")]
pub mod gpu;
pub mod id_assigner;
pub mod id_assigner_copy;
pub mod int_to_ascii;
pub mod map_windows;
pub mod md5;
Expand Down
29 changes: 4 additions & 25 deletions crates/core/src/common/id_assigner.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
use std::hash::Hash;

pub struct IdAssigner<'a, const MAX_SIZE: usize, H: Ord + Eq + ?Sized> {
//id_map: std::collections::hash_map::HashMap<&'a H, u16>,
id_map: [&'a H; MAX_SIZE],
ids: [u16; MAX_SIZE],
assigned_count: u16,
}

impl<'a, const MAX_SIZE: usize, H: Ord + Eq + ?Sized> IdAssigner<'a, MAX_SIZE, H> {
pub fn new(default: &'a H) -> Self {
pub const fn new(default: &'a H) -> Self {
Self {
//id_map: Default::default(),
id_map: [default; MAX_SIZE],
ids: [0; MAX_SIZE],
assigned_count: 0,
}
}

pub fn id_of(&mut self, name: &'a H) -> Result<u16, String> {
/*
let next_id = self.id_map.len() as u16;
Ok(match self.id_map.entry(name) {
std::collections::hash_map::Entry::Vacant(entry) => {
if usize::from(next_id) == MAX_SIZE {
return Err("Too many entries".to_string());
}
entry.insert(next_id);
next_id
}
std::collections::hash_map::Entry::Occupied(e) => *e.get(),
})
*/

Ok(
match (&self.id_map[0..(self.assigned_count as usize)]).binary_search(&name) {
match self.id_map[0..(self.assigned_count as usize)].binary_search(&name) {
Ok(idx) => self.ids[idx],
Err(idx) => {
self.id_map
Expand All @@ -51,17 +33,14 @@ impl<'a, const MAX_SIZE: usize, H: Ord + Eq + ?Sized> IdAssigner<'a, MAX_SIZE, H
}

pub fn get_id(&mut self, name: &H) -> Option<u16> {
//self.id_map.get(name).copied()

if let Ok(idx) = (&self.id_map[0..(self.assigned_count as usize)]).binary_search(&name) {
if let Ok(idx) = self.id_map[0..(self.assigned_count as usize)].binary_search(&name) {
Some(self.ids[idx])
} else {
None
}
}

pub fn len(&self) -> usize {
//self.id_map.len()
pub const fn len(&self) -> usize {
self.assigned_count as usize
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/year2015/day09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::input::Input;
const MAX_LOCATIONS: u16 = 10;

pub fn solve(input: &Input) -> Result<u32, String> {
let mut id_assigner = IdAssigner::<MAX_LOCATIONS>::new();
let mut id_assigner = IdAssigner::<{ MAX_LOCATIONS as usize }, str>::new("");

let mut places = Vec::with_capacity(MAX_LOCATIONS as usize);
let mut distances = [0; (MAX_LOCATIONS * MAX_LOCATIONS) as usize];
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/year2015/day13.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use crate::common::id_assigner::IdAssigner;
use crate::common::permutation::all_permutations;
use crate::input::Input;

const MAX_ATTENDEES: u16 = 10;
const MAX_ATTENDEES: usize = 10;

pub fn solve(input: &Input) -> Result<i32, String> {
let mut id_assigner = IdAssigner::<MAX_ATTENDEES>::new();
let mut id_assigner = IdAssigner::<MAX_ATTENDEES, str>::new("");

let mut happiness_changes = Vec::new();
for line in input.text.lines() {
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/year2019/day14.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct Reactions {

impl Reactions {
fn parse(input_string: &str) -> Result<Self, String> {
let mut id_assigner = IdAssigner::<100>::new();
let mut id_assigner = IdAssigner::<100, str>::new("");

// Indexed by chemical id that is produced, to amount produced and required.
let mut reactions: Vec<(ChemicalAmount, Vec<ChemicalAmount>)> = Vec::new();
Expand Down
17 changes: 11 additions & 6 deletions crates/core/src/year2023/day08.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use crate::common::id_assigner::IdAssigner;
use crate::common::id_assigner_copy::IdAssigner;
use crate::input::{on_error, Input};

pub fn solve(input: &Input) -> Result<u64, String> {
const MAX_ENTRIES: usize = 1024;
const MAX_START_END_NODES: usize = 32;

let mut id_assigner = IdAssigner::<MAX_ENTRIES, [u8]>::new(&[0]);
let mut id_assigner = IdAssigner::<MAX_ENTRIES, u32>::new(0);

let (instructions, map_lines) = input.text.split_once("\n\n").ok_or_else(on_error)?;

let _ = id_assigner.id_of(&[b'A', b'A', b'A'])?;
let _ = id_assigner.id_of(&[b'Z', b'Z', b'Z'])?;
let a = u32::from(b'A');
let _ = id_assigner.id_of((a << 16) + (a << 8) + a);
let z = u32::from(b'Z');
let _ = id_assigner.id_of((z << 16) + (z << 8) + z);

let mut map = [(0, 0); MAX_ENTRIES];

Expand All @@ -31,10 +33,13 @@ pub fn solve(input: &Input) -> Result<u64, String> {
start_idx = idx;
}
} else if start_idx != usize::MAX {
if str_count == 3 {
if str_count == 3 || (start_idx + 3 != idx) {
return Err("Invalid input".to_string());
}
ids[str_count] = id_assigner.id_of(&bytes[start_idx..idx])?;
let key = (u32::from(bytes[start_idx]) << 16)
+ (u32::from(bytes[start_idx + 1]) << 8)
+ u32::from(bytes[start_idx + 2]);
ids[str_count] = id_assigner.id_of(key)?;
if str_count == 0 && bytes[2] == b'A' {
starting_nodes[starting_nodes_idx] = ids[str_count];
starting_nodes_idx += 1;
Expand Down

0 comments on commit f316954

Please sign in to comment.