[Rust/2020/08] Add solution
This commit is contained in:
parent
130042f413
commit
9d1b45fd54
6 changed files with 112 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): 7/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): 8/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/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")|
|
||||
|[**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/rs.svg">](Rust/2020/08.rs "Rust solution for 2020/08") [<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||||
|
||||
|
|
96
Rust/2020/08.rs
Normal file
96
Rust/2020/08.rs
Normal file
|
@ -0,0 +1,96 @@
|
|||
#![feature(test)]
|
||||
|
||||
type Input = Vec<Instruction>;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct Instruction {
|
||||
operation: Operation,
|
||||
argument: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
enum Operation {
|
||||
Nop,
|
||||
Acc,
|
||||
Jmp,
|
||||
}
|
||||
|
||||
fn setup(input: &str) -> Input {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let mut line = line.split_whitespace();
|
||||
let operation = match line.next().unwrap() {
|
||||
"nop" => Operation::Nop,
|
||||
"acc" => Operation::Acc,
|
||||
"jmp" => Operation::Jmp,
|
||||
_ => panic!(),
|
||||
};
|
||||
let argument = line.next().unwrap().parse().unwrap();
|
||||
Instruction {
|
||||
operation,
|
||||
argument,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn part1(input: &Input) -> i32 {
|
||||
let mut exec = vec![false; input.len()];
|
||||
let mut i = 0;
|
||||
let mut acc = 0;
|
||||
while !exec[i] {
|
||||
exec[i] = true;
|
||||
match input[i].operation {
|
||||
Operation::Nop => i += 1,
|
||||
Operation::Acc => {
|
||||
acc += input[i].argument;
|
||||
i += 1;
|
||||
}
|
||||
Operation::Jmp => i = (i as i32 + input[i].argument) as _,
|
||||
}
|
||||
}
|
||||
acc
|
||||
}
|
||||
|
||||
fn simulate(input: &Input) -> Option<i32> {
|
||||
let mut exec = vec![false; input.len()];
|
||||
let mut i = 0;
|
||||
let mut acc = 0;
|
||||
loop {
|
||||
if i == input.len() {
|
||||
return Some(acc);
|
||||
}
|
||||
if i > input.len() || exec[i] {
|
||||
return None;
|
||||
}
|
||||
|
||||
exec[i] = true;
|
||||
match input[i].operation {
|
||||
Operation::Nop => i += 1,
|
||||
Operation::Acc => {
|
||||
acc += input[i].argument;
|
||||
i += 1;
|
||||
}
|
||||
Operation::Jmp => i = (i as i32 + input[i].argument).try_into().ok()?,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn part2(input: &Input) -> i32 {
|
||||
let mut prog = input.clone();
|
||||
for i in 0..prog.len() {
|
||||
prog[i].operation = match prog[i].operation {
|
||||
Operation::Nop => Operation::Jmp,
|
||||
Operation::Acc => continue,
|
||||
Operation::Jmp => Operation::Nop,
|
||||
};
|
||||
if let Some(acc) = simulate(&prog) {
|
||||
return acc;
|
||||
}
|
||||
prog[i].operation = input[i].operation;
|
||||
}
|
||||
panic!()
|
||||
}
|
||||
|
||||
aoc::main!(2020, 8, ex: 1);
|
|
@ -57,6 +57,9 @@ path = "2020/06.rs"
|
|||
[[bin]]
|
||||
name = "2020_07"
|
||||
path = "2020/07.rs"
|
||||
[[bin]]
|
||||
name = "2020_08"
|
||||
path = "2020/08.rs"
|
||||
|
||||
# 2021
|
||||
[[bin]]
|
||||
|
|
9
examples/2020/8/1
Normal file
9
examples/2020/8/1
Normal file
|
@ -0,0 +1,9 @@
|
|||
nop +0
|
||||
acc +1
|
||||
jmp +4
|
||||
acc +3
|
||||
jmp -3
|
||||
acc -99
|
||||
acc +1
|
||||
jmp -4
|
||||
acc +6
|
1
examples/2020/8/1.1
Normal file
1
examples/2020/8/1.1
Normal file
|
@ -0,0 +1 @@
|
|||
5
|
1
examples/2020/8/1.2
Normal file
1
examples/2020/8/1.2
Normal file
|
@ -0,0 +1 @@
|
|||
8
|
Loading…
Add table
Add a link
Reference in a new issue