Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Aug 6, 2023
1 parent 557fac3 commit a449bdc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pie
"Day 11" : 2386
"Day 16" : 2331
"Day 23" : 2017
"Others" : 1189
"Others" : 1178
```

| Day | Problem | Solution | Benchmark (μs) |
Expand All @@ -92,7 +92,7 @@ pie
| 18 | [Boiling Boulders](https://adventofcode.com/2022/day/18) | [Source](src/year2022/day18.rs) | 129 |
| 19 | [Not Enough Minerals](https://adventofcode.com/2022/day/19) | [Source](src/year2022/day19.rs) | 3416 |
| 20 | [Grove Positioning System](https://adventofcode.com/2022/day/20) | [Source](src/year2022/day20.rs) | 7449 |
| 21 | [Monkey Math](https://adventofcode.com/2022/day/21) | [Source](src/year2022/day21.rs) | 72 |
| 21 | [Monkey Math](https://adventofcode.com/2022/day/21) | [Source](src/year2022/day21.rs) | 61 |
| 22 | [Monkey Map](https://adventofcode.com/2022/day/22) | [Source](src/year2022/day22.rs) | 132 |
| 23 | [Unstable Diffusion](https://adventofcode.com/2022/day/23) | [Source](src/year2022/day23.rs) | 2017 |
| 24 | [Blizzard Basin](https://adventofcode.com/2022/day/24) | [Source](src/year2022/day24.rs) | 80 |
Expand Down
3 changes: 2 additions & 1 deletion src/year2020/day24.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//!
//! [`day 17`]: crate::year2020::day17
use crate::util::hash::*;
use std::array::from_fn;

#[derive(PartialEq, Eq, Hash)]
pub struct Hex {
Expand Down Expand Up @@ -85,7 +86,7 @@ pub fn part2(input: &FastSet<Hex>) -> usize {
let width = q2 - q1 + 203;
let height = r2 - r1 + 203;
let neighbors: [i32; 6] = [-1, 1, -width, width, 1 - width, width - 1];
let neighbors: [usize; 6] = std::array::from_fn(|i| neighbors[i] as usize);
let neighbors: [usize; 6] = from_fn(|i| neighbors[i] as usize);

let mut active = Vec::with_capacity(5_000);
let mut candidates = Vec::with_capacity(5_000);
Expand Down
4 changes: 3 additions & 1 deletion src/year2021/day15.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
//! memory.
//!
//! [`BinaryHeap`]: std::collections::BinaryHeap
use std::array::from_fn;

pub struct Square {
size: usize,
bytes: Vec<u8>,
Expand Down Expand Up @@ -66,7 +68,7 @@ fn dijkstra(square: &Square) -> usize {
let end = size * size - 1;

// Initialise our specialized priority queue with 10 vecs.
let mut todo: [Vec<u32>; 10] = std::array::from_fn(|_| Vec::with_capacity(1_000));
let mut todo: [Vec<u32>; 10] = from_fn(|_| Vec::with_capacity(1_000));
let mut cost = vec![u16::MAX; size * size];
let mut risk = 0;

Expand Down
5 changes: 3 additions & 2 deletions src/year2021/day23.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::util::hash::*;
use std::array::from_fn;
use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::hash::*;
Expand Down Expand Up @@ -83,7 +84,7 @@ struct Burrow {

impl Burrow {
fn new(rooms: [[usize; 4]; 4]) -> Burrow {
Burrow { hallway: Hallway::new(), rooms: std::array::from_fn(|i| Room::new(rooms[i])) }
Burrow { hallway: Hallway::new(), rooms: from_fn(|i| Room::new(rooms[i])) }
}
}

Expand Down Expand Up @@ -142,7 +143,7 @@ fn organize(burrow: Burrow) -> usize {

while let Some(state) = todo.pop() {
let State { mut burrow, energy } = state;
let open: [bool; 4] = std::array::from_fn(|i| burrow.rooms[i].open(i));
let open: [bool; 4] = from_fn(|i| burrow.rooms[i].open(i));

let mut changed = false;
for (i, &open) in open.iter().enumerate() {
Expand Down
22 changes: 11 additions & 11 deletions src/year2022/day21.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::util::hash::*;
use crate::util::parse::*;

#[derive(Clone, Copy)]
enum Operation {
Expand All @@ -14,14 +15,13 @@ enum Monkey {
}

impl Monkey {
fn parse(bytes: &[u8], indices: &FastMap<&[u8], usize>) -> Monkey {
if bytes.len() < 11 {
let number = std::str::from_utf8(bytes).unwrap().parse().unwrap();
Monkey::Number(number)
fn parse(str: &str, indices: &FastMap<&str, usize>) -> Monkey {
if str.len() < 11 {
Monkey::Number(str.signed())
} else {
let left = indices[&bytes[0..4]];
let right = indices[&bytes[7..11]];
let operation = match bytes[5] {
let left = indices[&str[0..4]];
let right = indices[&str[7..11]];
let operation = match str.as_bytes()[5] {
b'+' => Operation::Add,
b'-' => Operation::Sub,
b'*' => Operation::Mul,
Expand All @@ -41,16 +41,16 @@ pub struct Input {
}

pub fn parse(input: &str) -> Input {
let lines: Vec<&[u8]> = input.lines().map(|line| line.as_bytes()).collect();
let lines: Vec<_> = input.lines().collect();

let indices: FastMap<&[u8], usize> =
let indices: FastMap<&str, usize> =
lines.iter().enumerate().map(|(index, line)| (&line[0..4], index)).collect();

let monkeys: Vec<Monkey> =
lines.iter().map(|line| Monkey::parse(&line[6..], &indices)).collect();

let root = indices["root".as_bytes()];
let humn = indices["humn".as_bytes()];
let root = indices["root"];
let humn = indices["humn"];
let mut input =
Input { root, monkeys, yell: vec![0; lines.len()], unknown: vec![false; lines.len()] };

Expand Down

0 comments on commit a449bdc

Please sign in to comment.