Skip to content

Commit abbe657

Browse files
committed
feat: completed day 8
1 parent 0029c7b commit abbe657

File tree

3 files changed

+115
-16
lines changed

3 files changed

+115
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
- [Day 4](https://github.com/ankjevel/adventofcode/tree/2020/day_04) 🌟 🌟
1010
- [Day 5](https://github.com/ankjevel/adventofcode/tree/2020/day_05) 🌟 🌟
1111
- [Day 6](https://github.com/ankjevel/adventofcode/tree/2020/day_06) 🌟 🌟
12-
- [Day 7](#)
13-
- [Day 8](#)
12+
- [Day 7](https://github.com/ankjevel/adventofcode/tree/2020/day_07) 🌟 🌟
13+
- [Day 8](https://github.com/ankjevel/adventofcode/tree/2020/day_08) 🌟 🌟
1414
- [Day 9](#)
1515
- [Day 10](#)
1616
- [Day 11](#)

day_08/src/part_01.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,16 @@ impl Program {
3636
if *visited {
3737
break;
3838
}
39+
3940
let argument = &instruction.argument;
40-
let next = next(
41-
self.index,
42-
if instruction.operation == JMP {
43-
argument
44-
} else {
45-
&1
46-
},
47-
);
48-
if instruction.operation == ACC {
41+
let operation = instruction.operation;
42+
43+
if operation == ACC {
4944
self.accumulator += argument
5045
}
46+
5147
*visited = true;
52-
self.index = next;
48+
self.index = next(self.index, if operation == JMP { argument } else { &1 });
5349
}
5450

5551
self.accumulator

day_08/src/part_02.rs

Lines changed: 107 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,112 @@
11
use std::io::Result;
22

3-
use crate::Input;
3+
use crate::{instruction::Instruction, instruction::Operation::*, Input};
44

5-
pub fn main(_input: &Input) -> Result<()> {
6-
Ok(())
5+
#[derive(Default, Debug, Eq, PartialEq, Clone)]
6+
struct Program {
7+
instructions: Vec<(bool, Instruction)>,
8+
original: Vec<Instruction>,
9+
accumulator: i64,
10+
index: usize,
11+
change: usize,
12+
}
13+
14+
impl Program {
15+
fn new(input: &Input) -> Self {
16+
Self {
17+
original: input.to_owned(),
18+
..Program::default()
19+
}
20+
}
21+
22+
fn fix(&mut self) {
23+
let mut to_change = self.change.to_owned();
24+
let len = self.original.len();
25+
26+
self.accumulator = 0;
27+
self.index = 0;
28+
29+
self.instructions = self
30+
.original
31+
.iter()
32+
.enumerate()
33+
.map(|(index, instruction)| {
34+
if to_change != index {
35+
return (false, instruction.to_owned());
36+
}
37+
38+
let operation = instruction.operation;
39+
let argument = instruction.argument;
40+
41+
match operation {
42+
ACC => {
43+
to_change += 1;
44+
(false, instruction.to_owned())
45+
}
46+
NOP => (
47+
false,
48+
Instruction {
49+
operation: JMP,
50+
argument,
51+
},
52+
),
53+
JMP => (
54+
false,
55+
Instruction {
56+
operation: NOP,
57+
argument,
58+
},
59+
),
60+
}
61+
})
62+
.collect::<Vec<_>>();
63+
64+
self.change = to_change + 1 % len;
65+
}
66+
67+
fn run(mut self) -> i64 {
68+
let len = self.original.len() as i64;
69+
let next = |curr: usize, next: &i64| -> usize {
70+
let next_index = curr as i64 + next;
71+
(if next_index > len {
72+
len - next_index
73+
} else {
74+
next_index
75+
} % len) as usize
76+
};
77+
78+
'main: loop {
79+
self.fix();
80+
81+
'test: loop {
82+
let (visited, instruction) = self.instructions.get_mut(self.index).unwrap();
83+
if *visited {
84+
break 'test;
85+
}
86+
87+
let argument = &instruction.argument;
88+
let operation = instruction.operation;
89+
let next = next(self.index, if operation == JMP { argument } else { &1 });
90+
91+
if operation == ACC {
92+
self.accumulator += argument;
93+
}
94+
95+
if self.index != 0 && next == 0 {
96+
break 'main;
97+
}
98+
99+
*visited = true;
100+
self.index = next;
101+
}
102+
}
103+
104+
self.accumulator
105+
}
106+
}
107+
108+
pub fn main(input: &Input) -> Result<i64> {
109+
Ok(Program::new(input).run())
7110
}
8111

9112
#[cfg(test)]
@@ -26,7 +129,7 @@ mod tests {
26129

27130
#[test]
28131
fn it_gets_the_example_correct() -> Result<()> {
29-
assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, ());
132+
assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 8);
30133
Ok(())
31134
}
32135
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy