Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f8d8ce8

Browse files
authored
Merge pull request #34 from ChAoSUnItY/main
ChAoS-UnItY's Day 10 Solution
2 parents 21a4f19 + 4c7bd3d commit f8d8ce8

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

‎2022/ChAoS-UnItY/Cargo.toml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "ChAoS-UnItY"
3+
version = "0.1.0"
4+
edition = "2021"
5+
license = "MIT"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

‎2022/ChAoS-UnItY/LICENSE‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../01/ChAoS-UnItY/LICENSE

‎2022/ChAoS-UnItY/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Solution
2+
3+
This solution uses state to store each iteration's result, which
4+
allows us to use `fold` function from iteration of instruction
5+
datasets.

‎2022/ChAoS-UnItY/src/main.rs‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
trait Cyclable {
2+
fn run_noop(&mut self) {
3+
self.run_cycle();
4+
}
5+
6+
fn run_addx(&mut self) {
7+
self.run_cycle();
8+
self.run_cycle();
9+
}
10+
11+
fn run_instruction(&mut self, instruction: String);
12+
13+
fn run_cycle(&mut self);
14+
}
15+
16+
struct CycleState {
17+
cycle: i32,
18+
register_x: i32,
19+
signal_sum: i32
20+
}
21+
22+
impl CycleState {
23+
fn new() -> Self {
24+
Self{
25+
cycle: 0,
26+
register_x: 1,
27+
signal_sum: 0
28+
}
29+
}
30+
}
31+
32+
impl Cyclable for CycleState {
33+
fn run_instruction(&mut self, instruction: String) {
34+
let mut instruction_segments = instruction.split(" ");
35+
36+
match instruction_segments.next().unwrap() {
37+
"noop" => self.run_noop(),
38+
"addx" => {
39+
self.run_addx();
40+
self.register_x += instruction_segments.next()
41+
.unwrap()
42+
.parse::<i32>()
43+
.unwrap();
44+
}
45+
_ => {}
46+
}
47+
}
48+
49+
fn run_cycle(&mut self) {
50+
self.cycle += 1;
51+
if self.cycle == 20 || (self.cycle - 20) % 40 == 0 {
52+
self.signal_sum += self.cycle * self.register_x;
53+
}
54+
}
55+
}
56+
57+
struct CrtState {
58+
cycle: i32,
59+
register_x: i32,
60+
crt_image: Vec<char>
61+
}
62+
63+
impl CrtState {
64+
fn new() -> Self {
65+
Self{
66+
cycle: 0,
67+
register_x: 1,
68+
crt_image: vec![]
69+
}
70+
}
71+
}
72+
73+
impl Cyclable for CrtState {
74+
fn run_instruction(&mut self, instruction: String) {
75+
let mut instruction_segments = instruction.split(" ");
76+
77+
match instruction_segments.next().unwrap() {
78+
"noop" => self.run_noop(),
79+
"addx" => {
80+
self.run_addx();
81+
self.register_x += instruction_segments.next()
82+
.unwrap()
83+
.parse::<i32>()
84+
.unwrap();
85+
}
86+
_ => {}
87+
}
88+
}
89+
90+
fn run_cycle(&mut self) {
91+
self.crt_image.push(if self.cycle >= self.register_x - 1 && self.cycle <= self.register_x + 1 {
92+
'.'
93+
} else {
94+
'#'
95+
});
96+
self.cycle += 1;
97+
if self.cycle % 40 == 0 {
98+
self.cycle = 0;
99+
self.crt_image.push('\n');
100+
}
101+
}
102+
}
103+
104+
fn main() {
105+
let data = include_str!("../Day10.txt");
106+
let processed_data = process_data(data);
107+
108+
println!("{}", part1(&processed_data));
109+
println!("{}", part2(&processed_data));
110+
}
111+
112+
fn part1(data: &Vec<String>) -> i32 {
113+
data.iter()
114+
.fold(CycleState::new(), |mut state, instruction| {
115+
state.run_instruction(instruction.to_string());
116+
state
117+
}).signal_sum
118+
}
119+
120+
fn part2(data: &Vec<String>) -> String {
121+
data.iter()
122+
.fold(CrtState::new(), |mut state, instruction| {
123+
state.run_instruction(instruction.to_string());
124+
state
125+
}).crt_image.iter().collect()
126+
}
127+
128+
fn process_data(data: &'static str) -> Vec<String> {
129+
data.replace("\r\n", "\n")
130+
.split("\n")
131+
.map(|s| s.to_string())
132+
.collect()
133+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /