[Rust/2020/07] Add solution
This commit is contained in:
parent
9f68df1afa
commit
130042f413
8 changed files with 106 additions and 2 deletions
|
@ -19,11 +19,11 @@
|
|||
|[**20**](https://adventofcode.com/2021/day/20) [<img height=12 src=".assets/rs.svg">](Rust/2021/20.rs "Rust solution for 2021/20") [<img height=12 src=".assets/py.svg">](Python/2021/20.ipynb "Python solution for 2021/20")|[**21**](https://adventofcode.com/2021/day/21) [<img height=12 src=".assets/rs.svg">](Rust/2021/21.rs "Rust solution for 2021/21") [<img height=12 src=".assets/py.svg">](Python/2021/21.ipynb "Python solution for 2021/21")|[**22**](https://adventofcode.com/2021/day/22) [<img height=12 src=".assets/rs.svg">](Rust/2021/22.rs "Rust solution for 2021/22") [<img height=12 src=".assets/py.svg">](Python/2021/22.ipynb "Python solution for 2021/22")|[**23**](https://adventofcode.com/2021/day/23) [<img height=12 src=".assets/rs.svg">](Rust/2021/23.rs "Rust solution for 2021/23") [<img height=12 src=".assets/py.svg">](Python/2021/23.ipynb "Python solution for 2021/23")|[**24**](https://adventofcode.com/2021/day/24) [<img height=12 src=".assets/rs.svg">](Rust/2021/24.rs "Rust solution for 2021/24") [<img height=12 src=".assets/py.svg">](Python/2021/24.ipynb "Python solution for 2021/24")|[**25**](https://adventofcode.com/2021/day/25) [<img height=12 src=".assets/rs.svg">](Rust/2021/25.rs "Rust solution for 2021/25") [<img height=12 src=".assets/py.svg">](Python/2021/25.ipynb "Python solution for 2021/25")|26|
|
||||
|27|28|29|30|31|||
|
||||
|
||||
## [2020](https://adventofcode.com/2020) ([<img height=18 src=".assets/py.svg"> Python](Python/2020): 25/25 | [<img height=18 src=".assets/rs.svg"> Rust](Rust/2020): 6/25)
|
||||
## [2020](https://adventofcode.com/2020) ([<img height=18 src=".assets/py.svg"> Python](Python/2020): 25/25 | [<img height=18 src=".assets/rs.svg"> Rust](Rust/2020): 7/25)
|
||||
|Mo|Tu|We|Th|Fr|Sa|Su|
|
||||
|-|-|-|-|-|-|-|
|
||||
||[**1**](https://adventofcode.com/2020/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2020/01.rs "Rust solution for 2020/01") [<img height=12 src=".assets/py.svg">](Python/2020/01.ipynb "Python solution for 2020/01")|[**2**](https://adventofcode.com/2020/day/2) [<img height=12 src=".assets/rs.svg">](Rust/2020/02.rs "Rust solution for 2020/02") [<img height=12 src=".assets/py.svg">](Python/2020/02.ipynb "Python solution for 2020/02")|[**3**](https://adventofcode.com/2020/day/3) [<img height=12 src=".assets/rs.svg">](Rust/2020/03.rs "Rust solution for 2020/03") [<img height=12 src=".assets/py.svg">](Python/2020/03.ipynb "Python solution for 2020/03")|[**4**](https://adventofcode.com/2020/day/4) [<img height=12 src=".assets/rs.svg">](Rust/2020/04.rs "Rust solution for 2020/04") [<img height=12 src=".assets/py.svg">](Python/2020/04.ipynb "Python solution for 2020/04")|[**5**](https://adventofcode.com/2020/day/5) [<img height=12 src=".assets/rs.svg">](Rust/2020/05.rs "Rust solution for 2020/05") [<img height=12 src=".assets/py.svg">](Python/2020/05.ipynb "Python solution for 2020/05")|[**6**](https://adventofcode.com/2020/day/6) [<img height=12 src=".assets/rs.svg">](Rust/2020/06.rs "Rust solution for 2020/06") [<img height=12 src=".assets/py.svg">](Python/2020/06.ipynb "Python solution for 2020/06")|
|
||||
|[**7**](https://adventofcode.com/2020/day/7) [<img height=12 src=".assets/py.svg">](Python/2020/07.ipynb "Python solution for 2020/07")|[**8**](https://adventofcode.com/2020/day/8) [<img height=12 src=".assets/py.svg">](Python/2020/08.ipynb "Python solution for 2020/08")|[**9**](https://adventofcode.com/2020/day/9) [<img height=12 src=".assets/py.svg">](Python/2020/09.ipynb "Python solution for 2020/09")|[**10**](https://adventofcode.com/2020/day/10) [<img height=12 src=".assets/py.svg">](Python/2020/10.ipynb "Python solution for 2020/10")|[**11**](https://adventofcode.com/2020/day/11) [<img height=12 src=".assets/py.svg">](Python/2020/11.ipynb "Python solution for 2020/11")|[**12**](https://adventofcode.com/2020/day/12) [<img height=12 src=".assets/py.svg">](Python/2020/12.ipynb "Python solution for 2020/12")|[**13**](https://adventofcode.com/2020/day/13) [<img height=12 src=".assets/py.svg">](Python/2020/13.ipynb "Python solution for 2020/13")|
|
||||
|[**7**](https://adventofcode.com/2020/day/7) [<img height=12 src=".assets/rs.svg">](Rust/2020/07.rs "Rust solution for 2020/07") [<img height=12 src=".assets/py.svg">](Python/2020/07.ipynb "Python solution for 2020/07")|[**8**](https://adventofcode.com/2020/day/8) [<img height=12 src=".assets/py.svg">](Python/2020/08.ipynb "Python solution for 2020/08")|[**9**](https://adventofcode.com/2020/day/9) [<img height=12 src=".assets/py.svg">](Python/2020/09.ipynb "Python solution for 2020/09")|[**10**](https://adventofcode.com/2020/day/10) [<img height=12 src=".assets/py.svg">](Python/2020/10.ipynb "Python solution for 2020/10")|[**11**](https://adventofcode.com/2020/day/11) [<img height=12 src=".assets/py.svg">](Python/2020/11.ipynb "Python solution for 2020/11")|[**12**](https://adventofcode.com/2020/day/12) [<img height=12 src=".assets/py.svg">](Python/2020/12.ipynb "Python solution for 2020/12")|[**13**](https://adventofcode.com/2020/day/13) [<img height=12 src=".assets/py.svg">](Python/2020/13.ipynb "Python solution for 2020/13")|
|
||||
|[**14**](https://adventofcode.com/2020/day/14) [<img height=12 src=".assets/py.svg">](Python/2020/14.ipynb "Python solution for 2020/14")|[**15**](https://adventofcode.com/2020/day/15) [<img height=12 src=".assets/py.svg">](Python/2020/15.ipynb "Python solution for 2020/15")|[**16**](https://adventofcode.com/2020/day/16) [<img height=12 src=".assets/py.svg">](Python/2020/16.ipynb "Python solution for 2020/16")|[**17**](https://adventofcode.com/2020/day/17) [<img height=12 src=".assets/py.svg">](Python/2020/17.ipynb "Python solution for 2020/17")|[**18**](https://adventofcode.com/2020/day/18) [<img height=12 src=".assets/py.svg">](Python/2020/18.ipynb "Python solution for 2020/18")|[**19**](https://adventofcode.com/2020/day/19) [<img height=12 src=".assets/py.svg">](Python/2020/19.ipynb "Python solution for 2020/19")|[**20**](https://adventofcode.com/2020/day/20) [<img height=12 src=".assets/py.svg">](Python/2020/20.ipynb "Python solution for 2020/20")|
|
||||
|[**21**](https://adventofcode.com/2020/day/21) [<img height=12 src=".assets/py.svg">](Python/2020/21.ipynb "Python solution for 2020/21")|[**22**](https://adventofcode.com/2020/day/22) [<img height=12 src=".assets/py.svg">](Python/2020/22.ipynb "Python solution for 2020/22")|[**23**](https://adventofcode.com/2020/day/23) [<img height=12 src=".assets/py.svg">](Python/2020/23.ipynb "Python solution for 2020/23")|[**24**](https://adventofcode.com/2020/day/24) [<img height=12 src=".assets/py.svg">](Python/2020/24.ipynb "Python solution for 2020/24")|[**25**](https://adventofcode.com/2020/day/25) [<img height=12 src=".assets/py.svg">](Python/2020/25.ipynb "Python solution for 2020/25")|26|27|
|
||||
|28|29|30|31||||
|
||||
|
|
82
Rust/2020/07.rs
Normal file
82
Rust/2020/07.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
#![feature(test)]
|
||||
|
||||
use regex::Regex;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
struct Input {
|
||||
bags: Vec<Vec<(u32, usize)>>,
|
||||
start: usize,
|
||||
}
|
||||
|
||||
fn setup(input: &str) -> Input {
|
||||
let name_regex = Regex::new(r"^(.+?) bags").unwrap();
|
||||
let content_regex = Regex::new(r"(\d+) (.+?) bags?").unwrap();
|
||||
let names = input
|
||||
.lines()
|
||||
.enumerate()
|
||||
.map(|(i, line)| {
|
||||
let name = name_regex.captures(line).unwrap().get(1).unwrap().as_str();
|
||||
(name, i)
|
||||
})
|
||||
.collect::<FxHashMap<_, _>>();
|
||||
let bags = input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
content_regex
|
||||
.captures_iter(line)
|
||||
.map(|c| {
|
||||
let count = c.get(1).unwrap().as_str().parse().unwrap();
|
||||
let name = c.get(2).unwrap().as_str();
|
||||
(count, names[name])
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
Input {
|
||||
bags,
|
||||
start: names["shiny gold"],
|
||||
}
|
||||
}
|
||||
|
||||
fn part1(input: &Input) -> u32 {
|
||||
let mut rev = vec![vec![]; input.bags.len()];
|
||||
for (i, bag) in input.bags.iter().enumerate() {
|
||||
for &(_, j) in bag {
|
||||
rev[j].push(i);
|
||||
}
|
||||
}
|
||||
let mut out = 0;
|
||||
let mut queue = vec![input.start];
|
||||
let mut seen = vec![false; input.bags.len()];
|
||||
seen[input.start] = true;
|
||||
while let Some(p) = queue.pop() {
|
||||
for &q in &rev[p] {
|
||||
if !seen[q] {
|
||||
seen[q] = true;
|
||||
out += 1;
|
||||
queue.push(q);
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn count_bags(input: &Input, counts: &mut [Option<u32>], bag: usize) -> u32 {
|
||||
if let Some(out) = counts[bag] {
|
||||
return out;
|
||||
}
|
||||
|
||||
let mut out = 1;
|
||||
for &(cnt, q) in &input.bags[bag] {
|
||||
out += cnt * count_bags(input, counts, q);
|
||||
}
|
||||
counts[bag] = Some(out);
|
||||
out
|
||||
}
|
||||
|
||||
fn part2(input: &Input) -> u32 {
|
||||
let mut counts = vec![None; input.bags.len()];
|
||||
count_bags(input, &mut counts, input.start) - 1
|
||||
}
|
||||
|
||||
aoc::main!(2020, 7, ex: 1, 2[b]);
|
|
@ -54,6 +54,9 @@ path = "2020/05.rs"
|
|||
[[bin]]
|
||||
name = "2020_06"
|
||||
path = "2020/06.rs"
|
||||
[[bin]]
|
||||
name = "2020_07"
|
||||
path = "2020/07.rs"
|
||||
|
||||
# 2021
|
||||
[[bin]]
|
||||
|
|
9
examples/2020/7/1
Normal file
9
examples/2020/7/1
Normal file
|
@ -0,0 +1,9 @@
|
|||
light red bags contain 1 bright white bag, 2 muted yellow bags.
|
||||
dark orange bags contain 3 bright white bags, 4 muted yellow bags.
|
||||
bright white bags contain 1 shiny gold bag.
|
||||
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
|
||||
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
|
||||
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
|
||||
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
|
||||
faded blue bags contain no other bags.
|
||||
dotted black bags contain no other bags.
|
1
examples/2020/7/1.1
Normal file
1
examples/2020/7/1.1
Normal file
|
@ -0,0 +1 @@
|
|||
4
|
1
examples/2020/7/1.2
Normal file
1
examples/2020/7/1.2
Normal file
|
@ -0,0 +1 @@
|
|||
32
|
7
examples/2020/7/2
Normal file
7
examples/2020/7/2
Normal file
|
@ -0,0 +1,7 @@
|
|||
shiny gold bags contain 2 dark red bags.
|
||||
dark red bags contain 2 dark orange bags.
|
||||
dark orange bags contain 2 dark yellow bags.
|
||||
dark yellow bags contain 2 dark green bags.
|
||||
dark green bags contain 2 dark blue bags.
|
||||
dark blue bags contain 2 dark violet bags.
|
||||
dark violet bags contain no other bags.
|
1
examples/2020/7/2.2
Normal file
1
examples/2020/7/2.2
Normal file
|
@ -0,0 +1 @@
|
|||
126
|
Loading…
Add table
Add a link
Reference in a new issue