[Rust/2023/01] Add solution
This commit is contained in:
parent
ce8bb8ec04
commit
5574d48675
8 changed files with 76 additions and 1 deletions
|
@ -1,6 +1,15 @@
|
|||
# AdventOfCode
|
||||
[Advent of Code](https://adventofcode.com/) solutions in [<img height=12 src=".assets/rs.svg"> Rust](Rust), [<img height=12 src=".assets/hs.svg"> Haskell](Haskell), [<img height=12 src=".assets/py.svg"> Python](Python), [<img height=12 src=".assets/apl.svg"> APL](APL) and [<img height=12 src=".assets/ua.png"> Uiua](Uiua)
|
||||
|
||||
## [2023](https://adventofcode.com/2023) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2023): 1/25)
|
||||
|Mo|Tu|We|Th|Fr|Sa|Su|
|
||||
|-|-|-|-|-|-|-|
|
||||
|||||[**1**](https://adventofcode.com/2023/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2023/01.rs "Rust solution for 2023/01")|[**2**](https://adventofcode.com/2023/day/2)|[**3**](https://adventofcode.com/2023/day/3)|
|
||||
|[**4**](https://adventofcode.com/2023/day/4)|[**5**](https://adventofcode.com/2023/day/5)|[**6**](https://adventofcode.com/2023/day/6)|[**7**](https://adventofcode.com/2023/day/7)|[**8**](https://adventofcode.com/2023/day/8)|[**9**](https://adventofcode.com/2023/day/9)|[**10**](https://adventofcode.com/2023/day/10)|
|
||||
|[**11**](https://adventofcode.com/2023/day/11)|[**12**](https://adventofcode.com/2023/day/12)|[**13**](https://adventofcode.com/2023/day/13)|[**14**](https://adventofcode.com/2023/day/14)|[**15**](https://adventofcode.com/2023/day/15)|[**16**](https://adventofcode.com/2023/day/16)|[**17**](https://adventofcode.com/2023/day/17)|
|
||||
|[**18**](https://adventofcode.com/2023/day/18)|[**19**](https://adventofcode.com/2023/day/19)|[**20**](https://adventofcode.com/2023/day/20)|[**21**](https://adventofcode.com/2023/day/21)|[**22**](https://adventofcode.com/2023/day/22)|[**23**](https://adventofcode.com/2023/day/23)|[**24**](https://adventofcode.com/2023/day/24)|
|
||||
|[**25**](https://adventofcode.com/2023/day/25)|26|27|28|29|30|31|
|
||||
|
||||
## [2022](https://adventofcode.com/2022) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2022): 25/25 | [<img height=18 src=".assets/py.svg"> Python](Python/2022): 18/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2022): 9/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2022): 7/25)
|
||||
|Mo|Tu|We|Th|Fr|Sa|Su|
|
||||
|-|-|-|-|-|-|-|
|
||||
|
|
48
Rust/2023/01.rs
Normal file
48
Rust/2023/01.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
#![feature(test)]
|
||||
|
||||
type Input = Vec<String>;
|
||||
|
||||
fn setup(input: &str) -> Input {
|
||||
input.lines().map(ToOwned::to_owned).collect()
|
||||
}
|
||||
|
||||
fn part1(input: &Input) -> u32 {
|
||||
input
|
||||
.iter()
|
||||
.map(|line| {
|
||||
let digits = line
|
||||
.bytes()
|
||||
.filter(|b| b.is_ascii_digit())
|
||||
.map(|b| (b - b'0') as u32);
|
||||
digits.clone().next().unwrap() * 10 + digits.last().unwrap()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn part2(input: &Input) -> u32 {
|
||||
input
|
||||
.iter()
|
||||
.map(|line| {
|
||||
let bytes = line.as_bytes();
|
||||
let digits = (0..line.len()).filter_map(|i| {
|
||||
let test = |s: &str| &bytes[i..bytes.len().min(i + s.len())] == s.as_bytes();
|
||||
match bytes[i] {
|
||||
b @ b'0'..=b'9' => Some((b - b'0') as u32),
|
||||
_ if test("one") => Some(1),
|
||||
_ if test("two") => Some(2),
|
||||
_ if test("three") => Some(3),
|
||||
_ if test("four") => Some(4),
|
||||
_ if test("five") => Some(5),
|
||||
_ if test("six") => Some(6),
|
||||
_ if test("seven") => Some(7),
|
||||
_ if test("eight") => Some(8),
|
||||
_ if test("nine") => Some(9),
|
||||
_ => None,
|
||||
}
|
||||
});
|
||||
digits.clone().next().unwrap() * 10 + digits.last().unwrap()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
aoc::main!(2023, 1, ex: 1[a], 2[b]);
|
|
@ -220,3 +220,8 @@ path = "2022/24.rs"
|
|||
[[bin]]
|
||||
name = "2022_25"
|
||||
path = "2022/25.rs"
|
||||
|
||||
# 2023
|
||||
[[bin]]
|
||||
name = "2023_01"
|
||||
path = "2023/01.rs"
|
||||
|
|
4
examples/2023/1/1
Normal file
4
examples/2023/1/1
Normal file
|
@ -0,0 +1,4 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
1
examples/2023/1/1.1
Normal file
1
examples/2023/1/1.1
Normal file
|
@ -0,0 +1 @@
|
|||
142
|
7
examples/2023/1/2
Normal file
7
examples/2023/1/2
Normal file
|
@ -0,0 +1,7 @@
|
|||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
1
examples/2023/1/2.2
Normal file
1
examples/2023/1/2.2
Normal file
|
@ -0,0 +1 @@
|
|||
281
|
|
@ -27,7 +27,7 @@ print("# AdventOfCode")
|
|||
lst = [f"[{logo(k)} {v}]({v})" for k, v in names.items()]
|
||||
print(f"[Advent of Code](https://adventofcode.com/) solutions in {', '.join(lst[:-1])} and {lst[-1]}")
|
||||
|
||||
for year in range(2022, 2014, -1):
|
||||
for year in range(2023, 2014, -1):
|
||||
lines = []
|
||||
line = [""] * date(year, 12, 1).weekday()
|
||||
langs = Counter()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue