[Rust/2023/04] Add solution

This commit is contained in:
Felix Bargfeldt 2023-12-04 06:42:51 +01:00
parent aee6268b69
commit eaec469a5d
Signed by: Defelo
GPG key ID: 2A05272471204DD3
6 changed files with 82 additions and 2 deletions

View file

@ -1,11 +1,11 @@
# 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): 3/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2023): 3/25)
## [2023](https://adventofcode.com/2023) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2023): 4/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2023): 3/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) [<img height=12 src=".assets/rs.svg">](Rust/2023/03.rs "Rust solution for 2023/03") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JmZ3YSAiLi4vbGliLnVhIiAiTG9hZCDihpAgJnNjIgomaSAiLi4vbGliLnVhIiAiTG9hZCIKCk51bXMg4oaQICgKICDiiIo6K0Aw4oehMTAKICDDl8Ks4qyaMOKGuzBfwq8xLi4KICDDl-KNnOKZrVwrCikKU3ltYm9scyDihpAgw5fiioMo4omgQC4pKD0wTnVtcykKR2VhcnMg4oaQID1AKgpOZWlnaGJvcnMg4oaQIOKJoSjDl-KsmjDihrsp4piHMS0x4oehM18z4oipwqQKQSDihpAgKAogIE5laWdoYm9ycyDiioNTeW1ib2xzIE51bXMKICDiio_iip3ima0v4oalCikKQiDihpAgKAogIE5laWdoYm9ycyDiioNOdW1zIEdlYXJzCiAg4piHMeKNiQogIOKsmjDiiaEo4pa9PTLip7su4pa94omgMC7iip0pCiAg4omhL8OX4omh4oqP4oqZwqQKKQoK4oqDKOKKnOKImOKJoEBcbi4pKOKKgjDiiaFwYXJzZeKZrSByZWdleCAiXFxkKyIpCuKIqS8rIOKKg0IgQQo= "Uiua 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)|
|[**4**](https://adventofcode.com/2023/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2023/04.rs "Rust solution for 2023/04")|[**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|

69
Rust/2023/04.rs Normal file
View file

@ -0,0 +1,69 @@
#![feature(test)]
use std::collections::VecDeque;
use rustc_hash::FxHashSet;
type Input = Vec<Card>;
#[derive(Debug)]
struct Card {
winning: FxHashSet<u32>,
mine: FxHashSet<u32>,
}
impl Card {
fn matches(&self) -> usize {
self.winning.intersection(&self.mine).count()
}
fn points(&self) -> usize {
let matches = self.matches();
if matches >= 1 {
2usize.pow(matches as u32 - 1)
} else {
0
}
}
}
fn setup(input: &str) -> Input {
input
.lines()
.map(|line| {
let mut line = line.split(':').nth(1).unwrap().split('|');
let mut nums = || {
line.next()
.unwrap()
.split_whitespace()
.map(|n| n.parse().unwrap())
.collect()
};
Card {
winning: nums(),
mine: nums(),
}
})
.collect()
}
fn part1(input: &Input) -> usize {
input.iter().map(Card::points).sum()
}
fn part2(input: &Input) -> usize {
let mut counts = VecDeque::from(vec![1; input.len()]);
input
.iter()
.map(|card| {
let n = counts.pop_front().unwrap();
counts
.iter_mut()
.take(card.matches())
.for_each(|cnt| *cnt += n);
n
})
.sum()
}
aoc::main!(2023, 4, ex: 1);

View file

@ -231,3 +231,6 @@ path = "2023/02.rs"
[[bin]]
name = "2023_03"
path = "2023/03.rs"
[[bin]]
name = "2023_04"
path = "2023/04.rs"

6
examples/2023/4/1 Normal file
View file

@ -0,0 +1,6 @@
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11

1
examples/2023/4/1.1 Normal file
View file

@ -0,0 +1 @@
13

1
examples/2023/4/1.2 Normal file
View file

@ -0,0 +1 @@
30