Skip to content

Commit 72eb1e3

Browse files
committed
2 parents cd31054 + d34a444 commit 72eb1e3

File tree

3 files changed

+90
-48
lines changed

3 files changed

+90
-48
lines changed

day23/src/main.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//#![feature(slice_patterns)]
2-
use std::io::prelude::*;
1+
// #![feature(slice_patterns)]
2+
use std::io::prelude::*
33
use std::fs::File;
44
use std::collections::HashMap;
55

@@ -14,7 +14,7 @@ fn main() {
1414
Ok(mut file) => {
1515
file.read_to_string(&mut content).unwrap();
1616
lines = content.trim().split("\n").collect();
17-
},
17+
}
1818
};
1919

2020
let mut regs = HashMap::new();
@@ -24,42 +24,56 @@ fn main() {
2424
let mut i: usize = 0;
2525

2626
let mut out = false;
27-
27+
2828
while !out {
2929
match lines.get(i) {
3030
None => {
31-
out=true;
32-
},
31+
out = true;
32+
}
3333

3434
Some(ref s) => {
3535
let operations: Vec<&str> = s.splitn(2, ' ').collect();
3636
let args: Vec<&str> = operations[1].split(", ").collect();
3737
let mut plus: i32 = 1;
3838
match operations[0] {
39-
"jmp" => {plus = args[0].parse().expect(args[0]);}
39+
"jmp" => {
40+
plus = args[0].parse().expect(args[0]);
41+
}
4042
other => {
41-
let reg_value = match regs.get(&args[0]){
43+
let reg_value = match regs.get(&args[0]) {
4244
Some(&val) => val,
43-
_ => panic!("register not found")
45+
_ => panic!("register not found"),
4446
};
4547
match other {
46-
"hlf" => {regs.insert(&args[0], reg_value / 2);},
47-
"tpl" => {regs.insert(&args[0], reg_value * 3);},
48-
"inc" => {regs.insert(&args[0], reg_value + 1);},
49-
"jie" => {plus = if reg_value % 2 == 0 {args[1].parse().expect(args[1])} else {1};},
48+
"hlf" => {
49+
regs.insert(&args[0], reg_value / 2);
50+
}
51+
"tpl" => {
52+
regs.insert(&args[0], reg_value * 3);
53+
}
54+
"inc" => {
55+
regs.insert(&args[0], reg_value + 1);
56+
}
57+
"jie" => {
58+
plus = if reg_value % 2 == 0 {
59+
args[1].parse().expect(args[1])
60+
} else {
61+
1
62+
};
63+
}
5064
"jio" => {
5165
if reg_value == 1 {
5266
plus = args[1].parse().expect(args[1]);
5367
};
54-
},
68+
}
5569
_ => panic!("out"),
5670
};
5771
}
5872
};
5973
plus = i as i32 + plus;
6074
i = plus as usize;
61-
},
75+
}
6276
}
63-
};
77+
}
6478
println!("{:?}", regs);
6579
}

day23_v2/src/main.rs

Lines changed: 46 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//#![feature(slice_patterns)]
1+
// #![feature(slice_patterns)]
22
use std::io::prelude::*;
33
use std::fs::File;
44
use std::collections::HashMap;
@@ -12,7 +12,7 @@ fn main() {
1212
Ok(mut file) => {
1313
file.read_to_string(&mut content).unwrap();
1414
content = content.trim().replace(",", "");
15-
},
15+
}
1616
};
1717

1818
let lines: Vec<&str> = content.split("\n").collect();
@@ -22,24 +22,36 @@ fn main() {
2222
instr: &'a str,
2323
value: i32,
2424
};
25-
25+
2626
let mut operations: Vec<Operation> = Vec::new();
27-
27+
2828
for line in &lines {
2929
let ops: Vec<&str> = line.split(' ').collect();
3030
let op = match ops[0] {
31-
r @ "jmp" => Operation {reg: "",
32-
instr: r,
33-
value: ops[1].parse().expect(ops[1])},
34-
r @ "jio" | r @ "jie" => Operation {reg: ops[1],
35-
instr: r,
36-
value: ops[2].parse().expect(ops[2])},
37-
r => Operation {reg: ops[1],
38-
instr: r,
39-
value: 0},
31+
r @ "jmp" => {
32+
Operation {
33+
reg: "",
34+
instr: r,
35+
value: ops[1].parse().expect(ops[1]),
36+
}
37+
}
38+
r @ "jio" | r @ "jie" => {
39+
Operation {
40+
reg: ops[1],
41+
instr: r,
42+
value: ops[2].parse().expect(ops[2]),
43+
}
44+
}
45+
r => {
46+
Operation {
47+
reg: ops[1],
48+
instr: r,
49+
value: 0,
50+
}
51+
}
4052
};
4153
operations.push(op);
42-
};
54+
}
4355

4456
fn run_program(reg_a_value: i32, operations: &Vec<Operation>) {
4557
let mut regs = HashMap::new();
@@ -50,34 +62,44 @@ fn main() {
5062
while i < operations.len() {
5163
match operations[i] {
5264

53-
Operation {reg, instr, value} => {
65+
Operation { reg, instr, value } => {
5466
let mut plus: i32 = 1;
5567
match instr {
5668
"jmp" => plus = value,
5769
other => {
58-
let reg_value = match regs.get(&reg){
70+
let reg_value = match regs.get(&reg) {
5971
Some(&val) => val,
60-
_ => panic!("register not found")
72+
_ => panic!("register not found"),
6173
};
6274
match other {
63-
"hlf" => {regs.insert(reg, reg_value / 2);},
64-
"tpl" => {regs.insert(reg, reg_value * 3);},
65-
"inc" => {regs.insert(reg, reg_value + 1);},
66-
"jie" => if reg_value % 2 == 0 {plus = value},
75+
"hlf" => {
76+
regs.insert(reg, reg_value / 2);
77+
}
78+
"tpl" => {
79+
regs.insert(reg, reg_value * 3);
80+
}
81+
"inc" => {
82+
regs.insert(reg, reg_value + 1);
83+
}
84+
"jie" => {
85+
if reg_value % 2 == 0 {
86+
plus = value
87+
}
88+
}
6789
"jio" => {
6890
if reg_value == 1 {
6991
plus = value;
7092
};
71-
},
93+
}
7294
_ => panic!("out"),
7395
};
7496
}
7597
};
7698
plus = i as i32 + plus;
7799
i = plus as usize;
78-
},
100+
}
79101
}
80-
};
102+
}
81103
println!("{:?}", regs);
82104
};
83105
run_program(0, &operations);

day25/src/main.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
11
fn main() {
2-
//let mut value: u32 = 27995004;
3-
fn find_code (mut max_row: u32, mut row: u32, mut col: u32, mut value: u64, to_row: u32, to_col: u32) -> u64 {
2+
// let mut value: u32 = 27995004;
3+
fn find_code(mut max_row: u32,
4+
mut row: u32,
5+
mut col: u32,
6+
mut value: u64,
7+
to_row: u32,
8+
to_col: u32)
9+
-> u64 {
410
loop {
511
match (row == to_row) & (col == to_col) {
6-
true => return value,
12+
true => return value,
713
false => {
814
value = value * 252533 % 33554393;
915
if row > 1 {
10-
col +=1;
11-
row -=1;
16+
col += 1;
17+
row -= 1;
1218
} else {
1319
max_row += 1;
1420
row = max_row;
1521
col = 1;
1622
};
17-
//find_code(max_row, row, col, value, to_row, to_col)
23+
// find_code(max_row, row, col, value, to_row, to_col)
1824
}
19-
25+
2026
}
2127
}
22-
28+
2329
};
2430

2531
let code = find_code(11, 6, 6, 27995004, 3010, 3019);

0 commit comments

Comments
 (0)