Rust/2024/05: add solution
This commit is contained in:
parent
ec673bcff5
commit
b2bf8bb064
7 changed files with 106 additions and 3 deletions
|
@ -9,11 +9,11 @@
|
|||
|[2021](https://adventofcode.com/2021/leaderboard)|**136**|438|13.41% (of 1014)|0.0532% (of ≥255548)|
|
||||
|[2020](https://adventofcode.com/2020/leaderboard)|**621**|46|65.23% (of 952)|0.3146% (of ≥197402)|
|
||||
|
||||
## [2024](https://adventofcode.com/2024) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2024): 4/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2024): 4/25 | [<img height=18 src=".assets/rb.svg"> Ruby](Ruby/2024): 4/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2024): 3/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2024): 1/25)
|
||||
## [2024](https://adventofcode.com/2024) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2024): 5/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2024): 4/25 | [<img height=18 src=".assets/rb.svg"> Ruby](Ruby/2024): 4/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2024): 3/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2024): 1/25)
|
||||
|Mo|Tu|We|Th|Fr|Sa|Su|
|
||||
|-|-|-|-|-|-|-|
|
||||
|||||||[**1**](https://adventofcode.com/2024/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2024/01.rs "Rust solution for 2024/01") [<img height=12 src=".assets/hs.svg">](Haskell/2024/01.hs "Haskell solution for 2024/01") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrCsOKKn-KNieKKnOKKnOKLleKIqeKJoEBcbixAIC4K4oipLyviioMo4pmtw5fiip7iirg9fOKMtS3iiKnijYYpCg== "Uiua solution for 2024/01") [<img height=12 src=".assets/nix.svg">](Nix/2024/01.nix "Nix solution for 2024/01") [<img height=12 src=".assets/rb.svg">](Ruby/2024/01.rb "Ruby solution for 2024/01")|
|
||||
|[**2**](https://adventofcode.com/2024/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2024/02.rs "Rust solution for 2024/02") [<img height=12 src=".assets/hs.svg">](Haskell/2024/02.hs "Haskell solution for 2024/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4pah4oqc4ouVKeKIqeKJoEBcbixAIC4KCkNoZWNrIOKGkCDDl-KKgygvw5fiiaQz4oy1fOKJjcKkMeKMteKXtMKxKeKniC0K4oipLyviiaHil4fiioMoL-KGpeKJoShDaGVja-KWvSniip7iiaAu4oeh4qe74p-cwqR8Q2hlY2spCg== "Uiua solution for 2024/02") [<img height=12 src=".assets/rb.svg">](Ruby/2024/02.rb "Ruby solution for 2024/02")|[**3**](https://adventofcode.com/2024/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2024/03.rs "Rust solution for 2024/03") [<img height=12 src=".assets/hs.svg">](Haskell/2024/03.hs "Haskell solution for 2024/03") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrilr3irJoxXCjihqXihqfiipnCsOKKnynijYniip_CrOKIqeKMlSJkb24ndCgpIiwiZG8oKSIuLgriiKkoLysvw5fii5Xihpgx4o2JcmVnZXgibXVsXFwoKFxcZCspLChcXGQrKVxcKSIpCg== "Uiua solution for 2024/03") [<img height=12 src=".assets/rb.svg">](Ruby/2024/03.rb "Ruby solution for 2024/03")|[**4**](https://adventofcode.com/2024/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2024/04.rs "Rust solution for 2024/04") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiJjiiaBAXG4uCgripLAg4oaQIOKIqeKKleKWoeKKmSzijZzima3iipviip7iioMtK-KIqeKHocKw4oqf4oq44pazCgrin5woLyviiaHil4coLyvijJUiWE1BUyIp4oqC4p-c4o2a4oeM4oqC4oqDKOKKguKksHziioLiiKniiaHilqHin5zijYkpKQovK-KZreKniCjDl-KIqSgv4oal4omh4omN4pahIk1BUyLiioLin5zijZrih4wp4qSwKTNfMwo= "Uiua solution for 2024/04") [<img height=12 src=".assets/rb.svg">](Ruby/2024/04.rb "Ruby solution for 2024/04")|[**5**](https://adventofcode.com/2024/day/5)|[**6**](https://adventofcode.com/2024/day/6)|[**7**](https://adventofcode.com/2024/day/7)|[**8**](https://adventofcode.com/2024/day/8)|
|
||||
|[**2**](https://adventofcode.com/2024/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2024/02.rs "Rust solution for 2024/02") [<img height=12 src=".assets/hs.svg">](Haskell/2024/02.hs "Haskell solution for 2024/02") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4pah4oqc4ouVKeKIqeKJoEBcbixAIC4KCkNoZWNrIOKGkCDDl-KKgygvw5fiiaQz4oy1fOKJjcKkMeKMteKXtMKxKeKniC0K4oipLyviiaHil4fiioMoL-KGpeKJoShDaGVja-KWvSniip7iiaAu4oeh4qe74p-cwqR8Q2hlY2spCg== "Uiua solution for 2024/02") [<img height=12 src=".assets/rb.svg">](Ruby/2024/02.rb "Ruby solution for 2024/02")|[**3**](https://adventofcode.com/2024/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2024/03.rs "Rust solution for 2024/03") [<img height=12 src=".assets/hs.svg">](Haskell/2024/03.hs "Haskell solution for 2024/03") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgrilr3irJoxXCjihqXihqfiipnCsOKKnynijYniip_CrOKIqeKMlSJkb24ndCgpIiwiZG8oKSIuLgriiKkoLysvw5fii5Xihpgx4o2JcmVnZXgibXVsXFwoKFxcZCspLChcXGQrKVxcKSIpCg== "Uiua solution for 2024/03") [<img height=12 src=".assets/rb.svg">](Ruby/2024/03.rb "Ruby solution for 2024/03")|[**4**](https://adventofcode.com/2024/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2024/04.rs "Rust solution for 2024/04") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipziiJjiiaBAXG4uCgripLAg4oaQIOKIqeKKleKWoeKKmSzijZzima3iipviip7iioMtK-KIqeKHocKw4oqf4oq44pazCgrin5woLyviiaHil4coLyvijJUiWE1BUyIp4oqC4p-c4o2a4oeM4oqC4oqDKOKKguKksHziioLiiKniiaHilqHin5zijYkpKQovK-KZreKniCjDl-KIqSgv4oal4omh4omN4pahIk1BUyLiioLin5zijZrih4wp4qSwKTNfMwo= "Uiua solution for 2024/04") [<img height=12 src=".assets/rb.svg">](Ruby/2024/04.rb "Ruby solution for 2024/04")|[**5**](https://adventofcode.com/2024/day/5) [<img height=12 src=".assets/rs.svg">](Rust/2024/05.rs "Rust solution for 2024/05")|[**6**](https://adventofcode.com/2024/day/6)|[**7**](https://adventofcode.com/2024/day/7)|[**8**](https://adventofcode.com/2024/day/8)|
|
||||
|[**9**](https://adventofcode.com/2024/day/9)|[**10**](https://adventofcode.com/2024/day/10)|[**11**](https://adventofcode.com/2024/day/11)|[**12**](https://adventofcode.com/2024/day/12)|[**13**](https://adventofcode.com/2024/day/13)|[**14**](https://adventofcode.com/2024/day/14)|[**15**](https://adventofcode.com/2024/day/15)|
|
||||
|[**16**](https://adventofcode.com/2024/day/16)|[**17**](https://adventofcode.com/2024/day/17)|[**18**](https://adventofcode.com/2024/day/18)|[**19**](https://adventofcode.com/2024/day/19)|[**20**](https://adventofcode.com/2024/day/20)|[**21**](https://adventofcode.com/2024/day/21)|[**22**](https://adventofcode.com/2024/day/22)|
|
||||
|[**23**](https://adventofcode.com/2024/day/23)|[**24**](https://adventofcode.com/2024/day/24)|[**25**](https://adventofcode.com/2024/day/25)|26|27|28|29|
|
||||
|
|
70
Rust/2024/05.rs
Normal file
70
Rust/2024/05.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
#![feature(test)]
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
use itertools::Itertools;
|
||||
use rustc_hash::FxHashSet;
|
||||
|
||||
struct Input {
|
||||
rules: FxHashSet<Rule>,
|
||||
updates: Vec<Vec<Page>>,
|
||||
}
|
||||
|
||||
type Rule = (Page, Page);
|
||||
type Page = i32;
|
||||
|
||||
fn setup(input: &str) -> Input {
|
||||
let mut blocks = input.trim().split("\n\n");
|
||||
let rules = blocks
|
||||
.next()
|
||||
.unwrap()
|
||||
.lines()
|
||||
.map(|line| {
|
||||
line.split('|')
|
||||
.map(|x| x.parse().unwrap())
|
||||
.collect_tuple()
|
||||
.unwrap()
|
||||
})
|
||||
.collect();
|
||||
let updates = blocks
|
||||
.next()
|
||||
.unwrap()
|
||||
.lines()
|
||||
.map(|line| line.split(',').map(|x| x.parse().unwrap()).collect())
|
||||
.collect();
|
||||
Input { rules, updates }
|
||||
}
|
||||
|
||||
fn make_sort_cmp(rules: &FxHashSet<Rule>) -> impl (Fn(&Page, &Page) -> Ordering) + Copy + use<'_> {
|
||||
|&a, &b| match () {
|
||||
_ if rules.contains(&(a, b)) => Ordering::Less,
|
||||
_ if rules.contains(&(b, a)) => Ordering::Greater,
|
||||
_ => Ordering::Equal,
|
||||
}
|
||||
}
|
||||
|
||||
fn part1(input: &Input) -> Page {
|
||||
let cmp = make_sort_cmp(&input.rules);
|
||||
input
|
||||
.updates
|
||||
.iter()
|
||||
.filter(|&u| u.is_sorted_by(|a, b| cmp(a, b).is_le()))
|
||||
.map(|u| u[u.len() >> 1])
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn part2(input: &Input) -> Page {
|
||||
let cmp = make_sort_cmp(&input.rules);
|
||||
input
|
||||
.updates
|
||||
.iter()
|
||||
.filter(|&u| !u.is_sorted_by(|a, b| cmp(a, b).is_le()))
|
||||
.cloned()
|
||||
.map(|mut u| {
|
||||
let n = u.len() >> 1;
|
||||
*u.select_nth_unstable_by(n, cmp).1
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
aoc::main!(2024, 5, ex: 1);
|
|
@ -3,7 +3,7 @@ aoc::year! {
|
|||
"02.rs",
|
||||
"03.rs",
|
||||
"04.rs",
|
||||
// "05.rs",
|
||||
"05.rs",
|
||||
// "06.rs",
|
||||
// "07.rs",
|
||||
// "08.rs",
|
||||
|
|
|
@ -372,3 +372,6 @@ path = "2024/03.rs"
|
|||
[[bin]]
|
||||
name = "2024_04"
|
||||
path = "2024/04.rs"
|
||||
[[bin]]
|
||||
name = "2024_05"
|
||||
path = "2024/05.rs"
|
||||
|
|
28
examples/2024/5/1
Normal file
28
examples/2024/5/1
Normal file
|
@ -0,0 +1,28 @@
|
|||
47|53
|
||||
97|13
|
||||
97|61
|
||||
97|47
|
||||
75|29
|
||||
61|13
|
||||
75|53
|
||||
29|13
|
||||
97|29
|
||||
53|29
|
||||
61|53
|
||||
97|53
|
||||
61|29
|
||||
47|13
|
||||
75|47
|
||||
97|75
|
||||
47|61
|
||||
75|61
|
||||
47|29
|
||||
75|13
|
||||
53|13
|
||||
|
||||
75,47,61,53,29
|
||||
97,61,53,29,13
|
||||
75,29,13
|
||||
75,97,47,61,53
|
||||
61,13,29
|
||||
97,13,75,29,47
|
1
examples/2024/5/1.1
Normal file
1
examples/2024/5/1.1
Normal file
|
@ -0,0 +1 @@
|
|||
143
|
1
examples/2024/5/1.2
Normal file
1
examples/2024/5/1.2
Normal file
|
@ -0,0 +1 @@
|
|||
123
|
Loading…
Add table
Add a link
Reference in a new issue