[Rust/2023/03] Add solution
This commit is contained in:
parent
286c16180e
commit
be3ee8d629
6 changed files with 108 additions and 2 deletions
|
@ -1,10 +1,10 @@
|
|||
# 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): 2/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2023): 2/25)
|
||||
## [2023](https://adventofcode.com/2023) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2023): 3/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2023): 2/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") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JmZ3YSAiLi4vbGliLnVhIiAiTG9hZCDihpAgJnNjIgomaSAiLi4vbGliLnVhIiAiTG9hZCIKCk51bXMg4oaQIHsib25lIiAidHdvIiAidGhyZWUiICJmb3VyIiAiZml2ZSIKICAgICAgICAic2l4IiAic2V2ZW4iICJlaWdodCIgIm5pbmUifQpEaWdpdHMg4oaQICtAMeKHoTkKRmluZCDihpAgLysgw5crMeKHoTkg4omh4oqQ4oyVCkV4dHJhY3Qg4oaQICvDlzEwIOKIqeKKouKKg-KImOKHjCDilr3iiaAwLgoK4o2Y4oqfLysg4oqcKOKKnyDiiKlFeHRyYWN0IOKKg-KGpeKImCDiiKlGaW5kIERpZ2l0cyxOdW1zwqQp4omgQFxuLgo= "Uiua solution for 2023/01")|[**2**](https://adventofcode.com/2023/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2023/02.rs "Rust solution for 2023/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JmZ3YSAiLi4vbGliLnVhIiAiTG9hZCDihpAgJnNjIgomaSAiLi4vbGliLnVhIiAiTG9hZCIKCkNvbG9ycyDihpAgeyJyZWQiICJncmVlbiIgImJsdWUifQpQYXJzZUNvdW50IOKGkCDiipfiiplDb2xvcnMg4oqZ4oqQcGFyc2Ug4o2Y4oqf4oeMIOKKnOKWoeKJoEAgLgpQYXJzZVJvdW5kIOKGkCAvK-KKnCjDl-KsmjDihq_CpDPijZjiippQYXJzZUNvdW50KeKJoEAsLgpQYXJzZUdhbWUg4oaQIOKKnFBhcnNlUm91bmTiiaBAOy4g4oaYKzLiipdAOi4KCuKKnCgv4oalUGFyc2VHYW1lKeKJoEBcbi4K4oipLysg4oqDL8OXKMOXKzHih6Hip7suL8OX4omkMTJfMTNfMTQp4o2JCg== "Uiua solution for 2023/02")|[**3**](https://adventofcode.com/2023/day/3)|
|
||||
|||||[**1**](https://adventofcode.com/2023/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2023/01.rs "Rust solution for 2023/01") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JmZ3YSAiLi4vbGliLnVhIiAiTG9hZCDihpAgJnNjIgomaSAiLi4vbGliLnVhIiAiTG9hZCIKCk51bXMg4oaQIHsib25lIiAidHdvIiAidGhyZWUiICJmb3VyIiAiZml2ZSIKICAgICAgICAic2l4IiAic2V2ZW4iICJlaWdodCIgIm5pbmUifQpEaWdpdHMg4oaQICtAMeKHoTkKRmluZCDihpAgLysgw5crMeKHoTkg4omh4oqQ4oyVCkV4dHJhY3Qg4oaQICvDlzEwIOKIqeKKouKKg-KImOKHjCDilr3iiaAwLgoK4o2Y4oqfLysg4oqcKOKKnyDiiKlFeHRyYWN0IOKKg-KGpeKImCDiiKlGaW5kIERpZ2l0cyxOdW1zwqQp4omgQFxuLgo= "Uiua solution for 2023/01")|[**2**](https://adventofcode.com/2023/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2023/02.rs "Rust solution for 2023/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JmZ3YSAiLi4vbGliLnVhIiAiTG9hZCDihpAgJnNjIgomaSAiLi4vbGliLnVhIiAiTG9hZCIKCkNvbG9ycyDihpAgeyJyZWQiICJncmVlbiIgImJsdWUifQpQYXJzZUNvdW50IOKGkCDiipfiiplDb2xvcnMg4oqZ4oqQcGFyc2Ug4o2Y4oqf4oeMIOKKnOKWoeKJoEAgLgpQYXJzZVJvdW5kIOKGkCAvK-KKnCjDl-KsmjDihq_CpDPijZjiippQYXJzZUNvdW50KeKJoEAsLgpQYXJzZUdhbWUg4oaQIOKKnFBhcnNlUm91bmTiiaBAOy4g4oaYKzLiipdAOi4KCuKKnCgv4oalUGFyc2VHYW1lKeKJoEBcbi4K4oipLysg4oqDL8OXKMOXKzHih6Hip7suL8OX4omkMTJfMTNfMTQp4o2JCg== "Uiua solution for 2023/02")|[**3**](https://adventofcode.com/2023/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2023/03.rs "Rust solution for 2023/03")|
|
||||
|[**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)|
|
||||
|
|
91
Rust/2023/03.rs
Normal file
91
Rust/2023/03.rs
Normal file
|
@ -0,0 +1,91 @@
|
|||
#![feature(test)]
|
||||
|
||||
use itertools::{FoldWhile::Continue, Itertools};
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
type Input = Vec<Vec<Cell>>;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum Cell {
|
||||
Digit(u8),
|
||||
Symbol { gear: bool },
|
||||
Empty,
|
||||
}
|
||||
|
||||
fn setup(input: &str) -> Input {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
line.bytes()
|
||||
.map(|b| match b {
|
||||
b'0'..=b'9' => Cell::Digit(b - b'0'),
|
||||
b'.' => Cell::Empty,
|
||||
b'*' => Cell::Symbol { gear: true },
|
||||
_ => Cell::Symbol { gear: false },
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn find_part_numbers(
|
||||
input: &Input,
|
||||
) -> impl Iterator<Item = (u32, impl Iterator<Item = (usize, usize)> + '_)> + '_ {
|
||||
input.iter().enumerate().flat_map(move |(i, line)| {
|
||||
line.iter()
|
||||
.copied()
|
||||
.enumerate()
|
||||
.batching(|it| {
|
||||
let (num, j) = it.find_map(|(j, c)| match c {
|
||||
Cell::Digit(d) => Some((d as u32, j)),
|
||||
_ => None,
|
||||
})?;
|
||||
let (num, k) = it
|
||||
.fold_while((num, j), |acc @ (num, _), (k, c)| {
|
||||
match match c {
|
||||
Cell::Digit(d) => Some(d),
|
||||
_ => None,
|
||||
} {
|
||||
Some(d) => Continue((num * 10 + d as u32, k)),
|
||||
None => itertools::FoldWhile::Done(acc),
|
||||
}
|
||||
})
|
||||
.into_inner();
|
||||
Some((num, j, k))
|
||||
})
|
||||
.map(move |(num, j, k)| {
|
||||
let gears = (i.saturating_sub(1)..=(i + 1).min(input.len() - 1))
|
||||
.dedup()
|
||||
.flat_map(move |i| {
|
||||
(j.saturating_sub(1)..=(k + 1).min(input[i].len() - 1))
|
||||
.filter(move |&j| matches!(input[i][j], Cell::Symbol { .. }))
|
||||
.map(move |j| (i, j))
|
||||
});
|
||||
(num, gears)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn part1(input: &Input) -> u32 {
|
||||
find_part_numbers(input)
|
||||
.filter_map(|(num, mut parts)| parts.next().map(|_| num))
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn part2(input: &Input) -> u32 {
|
||||
let mut gears = FxHashMap::<(usize, usize), _>::default();
|
||||
for (num, gs) in find_part_numbers(input) {
|
||||
for g in gs {
|
||||
gears.entry(g).or_insert_with(Vec::new).push(num);
|
||||
}
|
||||
}
|
||||
gears
|
||||
.into_iter()
|
||||
.filter(|&((i, j), ref nums)| {
|
||||
matches!(input[i][j], Cell::Symbol { gear: true }) && nums.len() == 2
|
||||
})
|
||||
.map(|(_, nums)| nums.into_iter().product::<u32>())
|
||||
.sum()
|
||||
}
|
||||
|
||||
aoc::main!(2023, 3, ex: 1);
|
|
@ -228,3 +228,6 @@ path = "2023/01.rs"
|
|||
[[bin]]
|
||||
name = "2023_02"
|
||||
path = "2023/02.rs"
|
||||
[[bin]]
|
||||
name = "2023_03"
|
||||
path = "2023/03.rs"
|
||||
|
|
10
examples/2023/3/1
Normal file
10
examples/2023/3/1
Normal file
|
@ -0,0 +1,10 @@
|
|||
467..114..
|
||||
...*......
|
||||
..35..633.
|
||||
......#...
|
||||
617*......
|
||||
.....+.58.
|
||||
..592.....
|
||||
......755.
|
||||
...$.*....
|
||||
.664.598..
|
1
examples/2023/3/1.1
Normal file
1
examples/2023/3/1.1
Normal file
|
@ -0,0 +1 @@
|
|||
4361
|
1
examples/2023/3/1.2
Normal file
1
examples/2023/3/1.2
Normal file
|
@ -0,0 +1 @@
|
|||
467835
|
Loading…
Add table
Add a link
Reference in a new issue