Skip to content

Commit 6953c2d

Browse files
Add template
1 parent 102c3a0 commit 6953c2d

File tree

7 files changed

+271
-0
lines changed

7 files changed

+271
-0
lines changed

template/.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"[rust]": {
3+
"editor.defaultFormatter": "rust-lang.rust-analyzer",
4+
"editor.formatOnSave": true
5+
},
6+
"rust-analyzer.check.command": "clippy"
7+
}

template/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.

template/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[package]
2+
name = "day01"
3+
version = "0.1.0"
4+
edition = "2021"

template/input1.txt

Whitespace-only changes.

template/src/grid.rs

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#![allow(unused)]
2+
use std::{
3+
char,
4+
fmt::{Display, Formatter},
5+
fs,
6+
ops::{Index, Range},
7+
};
8+
9+
pub const DIRS: [(i64, i64); 4] = [(1, 0), (-1, 0), (0, 1), (0, -1)];
10+
pub const CLOCKWISE: [(i64, i64); 8] = [
11+
(1, 0),
12+
(1, 1),
13+
(0, 1),
14+
(-1, 1),
15+
(-1, 0),
16+
(-1, -1),
17+
(0, -1),
18+
(1, -1),
19+
];
20+
21+
pub trait ReadToGrid {
22+
fn read_to_grid(&self) -> Grid<char>;
23+
}
24+
25+
pub fn read_to_grid(filename: &str) -> Result<Grid<char>, std::io::Error> {
26+
let g = fs::read_to_string(filename)?
27+
.lines()
28+
.map(|l| l.chars().collect())
29+
.collect();
30+
Ok(Grid { grid: g })
31+
}
32+
33+
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
34+
pub struct Grid<T: Copy> {
35+
pub grid: Vec<Vec<T>>,
36+
}
37+
38+
impl<T: Copy> Grid<T> {
39+
#[inline]
40+
pub fn has(&self, x: i64, y: i64) -> bool {
41+
x >= 0 && x < self.width() && y >= 0 && y < self.height()
42+
}
43+
44+
#[inline]
45+
pub fn width(&self) -> i64 {
46+
self.grid[0].len() as i64
47+
}
48+
49+
#[inline]
50+
pub fn height(&self) -> i64 {
51+
self.grid.len() as i64
52+
}
53+
54+
#[inline]
55+
pub fn len(&self) -> i64 {
56+
self.width() * self.height()
57+
}
58+
59+
#[inline]
60+
pub fn get(&self, x: i64, y: i64) -> T {
61+
self.grid[y as usize][x as usize]
62+
}
63+
64+
#[inline]
65+
pub fn set(&mut self, x: i64, y: i64, c: T) {
66+
self.grid[y as usize][x as usize] = c;
67+
}
68+
69+
#[inline]
70+
pub fn row_range(&self) -> Range<i64> {
71+
0..self.height()
72+
}
73+
74+
#[inline]
75+
pub fn col_range(&self) -> Range<i64> {
76+
0..self.width()
77+
}
78+
79+
pub fn iter(&self) -> GridIterator<T> {
80+
GridIterator {
81+
grid: self,
82+
x: 0,
83+
y: 0,
84+
}
85+
}
86+
}
87+
88+
pub struct GridIterator<'a, T: Copy> {
89+
grid: &'a Grid<T>,
90+
x: i64,
91+
y: i64,
92+
}
93+
94+
impl<'a, T: Copy> Iterator for GridIterator<'a, T> {
95+
type Item = (i64, i64, T);
96+
97+
fn next(&mut self) -> Option<Self::Item> {
98+
if self.y >= self.grid.height() {
99+
return None;
100+
}
101+
102+
let c = self.grid.get(self.x, self.y);
103+
let r = (self.x, self.y, c);
104+
self.x += 1;
105+
if self.x >= self.grid.width() {
106+
self.x = 0;
107+
self.y += 1;
108+
}
109+
Some(r)
110+
}
111+
}
112+
113+
impl<T: Copy> Index<(i64, i64)> for Grid<T> {
114+
type Output = T;
115+
116+
fn index(&self, (x, y): (i64, i64)) -> &Self::Output {
117+
&self.grid[y as usize][x as usize]
118+
}
119+
}
120+
121+
impl<T: Copy + Display> Display for Grid<T> {
122+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
123+
for row in self.grid.iter() {
124+
for c in row.iter() {
125+
write!(f, "{}", c)?;
126+
}
127+
writeln!(f)?;
128+
}
129+
Ok(())
130+
}
131+
}

template/src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![allow(unused)]
2+
use grid::*;
3+
use std::collections::{BinaryHeap, HashMap, HashSet, VecDeque};
4+
use std::fs;
5+
use util::*;
6+
7+
mod grid;
8+
mod util;
9+
10+
fn main() {
11+
let input = fs::read_to_string("input.txt").expect("Could not read file");
12+
let lines = input.lines().collect::<Vec<_>>();
13+
// let grid = grid::read_to_grid("input.txt").expect("Could not read file");
14+
}

template/src/util.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#![allow(unused)]
2+
3+
pub fn uints(s: &str) -> Vec<u64> {
4+
let mut result = Vec::new();
5+
let bytes = s.as_bytes();
6+
let mut i = 0;
7+
while i < bytes.len() {
8+
while i < bytes.len() && !bytes[i].is_ascii_digit() {
9+
i += 1;
10+
}
11+
if i == bytes.len() {
12+
break;
13+
}
14+
let mut n = 0u64;
15+
while i < bytes.len() && bytes[i].is_ascii_digit() {
16+
n *= 10;
17+
n += (bytes[i] - b'0') as u64;
18+
i += 1;
19+
}
20+
result.push(n);
21+
}
22+
result
23+
}
24+
25+
pub fn iints(s: &str) -> Vec<i64> {
26+
let mut result = Vec::new();
27+
let bytes = s.as_bytes();
28+
let mut i = 0;
29+
while i < bytes.len() {
30+
while i < bytes.len()
31+
&& !bytes[i].is_ascii_digit()
32+
&& !(i < bytes.len() - 1 && bytes[i] == b'-' && bytes[i + 1].is_ascii_digit())
33+
{
34+
i += 1;
35+
}
36+
if i == bytes.len() {
37+
break;
38+
}
39+
let neg = if bytes[i] == b'-' {
40+
i += 1;
41+
true
42+
} else {
43+
false
44+
};
45+
let mut n = 0i64;
46+
while i < bytes.len() && bytes[i].is_ascii_digit() {
47+
n *= 10;
48+
n += (bytes[i] - b'0') as i64;
49+
i += 1;
50+
}
51+
if neg {
52+
n = -n;
53+
}
54+
result.push(n);
55+
}
56+
result
57+
}
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_uints() {
65+
assert_eq!(uints(""), vec![]);
66+
assert_eq!(uints("1 2 3 4 5"), vec![1, 2, 3, 4, 5]);
67+
assert_eq!(
68+
uints("one 1 two 2 three 3 four 4 5 6 end"),
69+
vec![1, 2, 3, 4, 5, 6]
70+
);
71+
assert_eq!(uints("Range: 10-42"), vec![10, 42]);
72+
assert_eq!(
73+
uints("Ranges: 10-42, 15-16, 0-8, 99-110"),
74+
vec![10, 42, 15, 16, 0, 8, 99, 110]
75+
);
76+
assert_eq!(
77+
uints("Ranges: 10-42, 15-16, 0-8, 99-110 (inclusive)"),
78+
vec![10, 42, 15, 16, 0, 8, 99, 110]
79+
);
80+
assert_eq!(uints("Button A: X+95, Y+110"), vec![95, 110]);
81+
assert_eq!(uints("----10---"), vec![10]);
82+
}
83+
84+
#[test]
85+
fn test_iints() {
86+
assert_eq!(iints(""), vec![]);
87+
assert_eq!(iints("-1"), vec![-1]);
88+
assert_eq!(iints("1 -2 3 -4 5"), vec![1, -2, 3, -4, 5]);
89+
assert_eq!(
90+
iints("one -1 two 2 three -3 four 4 -5 -6 end"),
91+
vec![-1, 2, -3, 4, -5, -6]
92+
);
93+
assert_eq!(iints("Two numbers: +10-42"), vec![10, -42]);
94+
assert_eq!(
95+
iints("Numbers: +10-42, +15-16, +0-8, -99+110"),
96+
vec![10, -42, 15, -16, 0, -8, -99, 110]
97+
);
98+
assert_eq!(
99+
iints("Numbers: +10-42, +15-16, +0-8, -99+110 (inclusive)"),
100+
vec![10, -42, 15, -16, 0, -8, -99, 110]
101+
);
102+
assert_eq!(iints("Button A: X+95, Y+110"), vec![95, 110]);
103+
assert_eq!(iints("Trailing minus: X-95, Y+110-"), vec![-95, 110]);
104+
assert_eq!(iints("Trailing minuses: X-95, Y+110--"), vec![-95, 110]);
105+
assert_eq!(iints("----10---"), vec![-10]);
106+
assert_eq!(iints("a 1 b -0 negative zero"), vec![1, 0]);
107+
}
108+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy