Skip to content

Commit e26a3ae

Browse files
committed
changes from previous years
1 parent 2cd7573 commit e26a3ae

File tree

17 files changed

+1039
-8
lines changed

17 files changed

+1039
-8
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ jobs:
2525
restore-keys: ${{ runner.os }}-cargo-
2626
- name: cargo test
2727
run: cargo test
28-
# uncomment to enable clippy linter
29-
# - name: cargo clippy
30-
# run: cargo clippy -- -D warnings
31-
# uncomment to enable format linter
32-
# - name: cargo fmt
33-
# run: cargo fmt --check
28+
- name: cargo clippy
29+
run: cargo clippy -- -D warnings
30+
- name: cargo fmt
31+
run: cargo fmt --check

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ pico-args = "0.5.0"
2828
tinyjson = "2.5.1"
2929

3030
# Solution dependencies
31+
fastrand = "2.0.1"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<img src="./.assets/christmas_ferris.png" width="164">
22

3-
# 🎄 Advent of Code {year}
3+
# 🎄 Advent of Code 2024
44

55
Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.rust-lang.org/).
66

@@ -230,7 +230,7 @@ To enable it, complete the following steps:
230230

231231
#### 1. Create a private leaderboard
232232

233-
Go to the leaderboard page of the year you want to track and click _Private Leaderboard_. If you have not created a leaderboard yet, create one by clicking _Create It_. Your leaderboard should be accessible under `https://adventofcode.com/{year}/leaderboard/private/view/{aoc_user_id}`.
233+
Go to the leaderboard page of the year you want to track and click _Private Leaderboard_. If you have not created a leaderboard yet, create one by clicking _Create It_. Your leaderboard should be accessible under `https://adventofcode.com/2024/leaderboard/private/view/{aoc_user_id}`.
234234

235235
#### 2. Set repository secrets
236236

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
pub mod template;
22

33
// Use this file to add helper functions and additional modules.
4+
pub mod majcn;
5+
pub mod maneatingape;

src/majcn/bignumbers.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#[derive(Clone, Copy)]
2+
pub struct UX64<const N: usize>([u64; N]);
3+
4+
impl<const N: usize> UX64<N> {
5+
pub fn count_ones(&self) -> u32 {
6+
self.0.iter().map(|x| x.count_ones()).sum()
7+
}
8+
9+
pub const ZERO: UX64<N> = UX64([0; N]);
10+
pub const ONE: UX64<N> = {
11+
let mut result = UX64([0; N]);
12+
result.0[0] = 1;
13+
result
14+
};
15+
}
16+
17+
impl<const N: usize> std::ops::Shl<usize> for UX64<N> {
18+
type Output = UX64<N>;
19+
20+
fn shl(self, rhs: usize) -> Self::Output {
21+
let mut result = UX64::ZERO;
22+
let word_shift = rhs / 64;
23+
let bit_shift = rhs % 64;
24+
for i in 0..N {
25+
if bit_shift < 64 && i + word_shift < N {
26+
result.0[i + word_shift] += self.0[i] << bit_shift;
27+
}
28+
if bit_shift > 0 && i + word_shift + 1 < N {
29+
result.0[i + word_shift + 1] += self.0[i] >> (64 - bit_shift);
30+
}
31+
}
32+
result
33+
}
34+
}
35+
36+
impl<const N: usize> std::ops::Shr<usize> for UX64<N> {
37+
type Output = UX64<N>;
38+
39+
fn shr(self, shift: usize) -> Self::Output {
40+
let mut result = UX64::ZERO;
41+
let word_shift = shift / 64;
42+
let bit_shift = shift % 64;
43+
for i in word_shift..N {
44+
result.0[i - word_shift] += self.0[i] >> bit_shift;
45+
if bit_shift > 0 && i < N - 1 {
46+
result.0[i - word_shift] += self.0[i + 1] << (64 - bit_shift);
47+
}
48+
}
49+
result
50+
}
51+
}
52+
53+
impl<const N: usize> std::ops::BitAnd for UX64<N> {
54+
type Output = UX64<N>;
55+
56+
fn bitand(self, rhs: Self) -> Self::Output {
57+
let mut result = UX64::ZERO;
58+
for i in 0..N {
59+
result.0[i] = self.0[i] & rhs.0[i];
60+
}
61+
result
62+
}
63+
}
64+
65+
impl<const N: usize> std::ops::BitOr for UX64<N> {
66+
type Output = UX64<N>;
67+
68+
fn bitor(self, rhs: Self) -> Self::Output {
69+
let mut result = UX64::ZERO;
70+
for i in 0..N {
71+
result.0[i] = self.0[i] | rhs.0[i];
72+
}
73+
result
74+
}
75+
}
76+
77+
impl<const N: usize> std::ops::BitOrAssign for UX64<N> {
78+
fn bitor_assign(&mut self, rhs: Self) {
79+
for i in 0..N {
80+
self.0[i] = self.0[i] | rhs.0[i];
81+
}
82+
}
83+
}
84+
85+
impl<const N: usize> std::ops::Not for UX64<N> {
86+
type Output = UX64<N>;
87+
88+
fn not(self) -> Self::Output {
89+
let mut result = UX64::ZERO;
90+
for i in 0..N {
91+
result.0[i] = !self.0[i];
92+
}
93+
result
94+
}
95+
}

src/majcn/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod bignumbers;

src/maneatingape/LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
**Copyright © 2023 maneatingape**
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

src/maneatingape/grid.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// source: https://github.com/maneatingape/advent-of-code-rust/blob/72c8feb22030d55d7a08f16c119425b2ba759470/src/util/hash.rs
2+
3+
//! Fast 2 dimensional Grid backed by a single `vec`. This module is designed to work with [`Point`].
4+
//!
5+
//! The traits [`Index`] and [`IndexMut`] are implemented for [`Point`] to allow usage like:
6+
//!
7+
//! ```
8+
//! # use aoc::util::grid::Grid;
9+
//! # use aoc::util::point::Point;
10+
//!
11+
//! let mut grid = Grid::parse("1");
12+
//! let point = Point::new(0, 0);
13+
//!
14+
//! let foo = grid[point];
15+
//! assert_eq!(foo, b'1');
16+
//!
17+
//! grid[point] = foo + 1;
18+
//! assert_eq!(grid[point], b'2');
19+
//! ```
20+
//!
21+
//! A convenience [`parse`] method creates a `Grid` directly from a 2 dimenionsal set of
22+
//! ASCII characters, a common occurence in Advent of Code inputs. The [`default_copy`] function
23+
//! creates a grid of the same size, that can be used for in BFS algorithms for tracking visited
24+
//! location or for tracking cost in Djikstra.
25+
//!
26+
//! [`Point`]: crate::util::point
27+
//! [`parse`]: Grid::parse
28+
//! [`default_copy`]: Grid::default_copy
29+
use crate::maneatingape::point::*;
30+
use std::ops::{Index, IndexMut};
31+
32+
#[derive(Clone, PartialEq, Eq, Hash)]
33+
pub struct Grid<T> {
34+
pub width: i32,
35+
pub height: i32,
36+
pub bytes: Vec<T>,
37+
}
38+
39+
impl Grid<u8> {
40+
pub fn parse(input: &str) -> Self {
41+
let raw: Vec<_> = input.lines().map(str::as_bytes).collect();
42+
let width = raw[0].len() as i32;
43+
let height = raw.len() as i32;
44+
let mut bytes = Vec::with_capacity((width * height) as usize);
45+
raw.iter().for_each(|slice| bytes.extend_from_slice(slice));
46+
Grid {
47+
width,
48+
height,
49+
bytes,
50+
}
51+
}
52+
}
53+
54+
impl<T: Copy + PartialEq> Grid<T> {
55+
pub fn default_copy<U: Default + Copy>(&self) -> Grid<U> {
56+
Grid {
57+
width: self.width,
58+
height: self.height,
59+
bytes: vec![U::default(); (self.width * self.height) as usize],
60+
}
61+
}
62+
63+
pub fn find(&self, needle: T) -> Option<Point> {
64+
let to_point = |index| {
65+
let x = (index as i32) % self.width;
66+
let y = (index as i32) / self.width;
67+
Point::new(x, y)
68+
};
69+
self.bytes.iter().position(|&h| h == needle).map(to_point)
70+
}
71+
72+
#[inline]
73+
pub fn contains(&self, point: Point) -> bool {
74+
point.x >= 0 && point.x < self.width && point.y >= 0 && point.y < self.height
75+
}
76+
}
77+
78+
impl<T> Index<Point> for Grid<T> {
79+
type Output = T;
80+
81+
#[inline]
82+
fn index(&self, point: Point) -> &Self::Output {
83+
&self.bytes[(self.width * point.y + point.x) as usize]
84+
}
85+
}
86+
87+
impl<T> IndexMut<Point> for Grid<T> {
88+
#[inline]
89+
fn index_mut(&mut self, point: Point) -> &mut Self::Output {
90+
&mut self.bytes[(self.width * point.y + point.x) as usize]
91+
}
92+
}

0 commit comments

Comments
 (0)