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

Appearance settings

Commit 34f6e6a

Browse filesBrowse files
2017, day 16
1 parent 42bc019 commit 34f6e6a
Copy full SHA for 34f6e6a

File tree

4 files changed

+95
-0
lines changed
Filter options

4 files changed

+95
-0
lines changed

‎inputs/2017/day16.txt

Copy file name to clipboardExpand all lines: inputs/2017/day16.txt
+1Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

‎src/main.rs

Copy file name to clipboardExpand all lines: src/main.rs
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ fn main() {
8282
(2017, 13) => year_2017::day13::run(inputs::read(year, day)),
8383
(2017, 14) => year_2017::day14::run("wenycdww"),
8484
(2017, 15) => year_2017::day15::run(591, 393),
85+
(2017, 16) => year_2017::day16::run(&inputs::read_first_line(year, day)),
8586

87+
// Other
8688
(_, _) => panic!("Not implemented :("),
8789
}
8890
}

‎src/year_2017/day16.rs

Copy file name to clipboard
+91Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use common::sfy::Sfy;
2+
use regex::Regex;
3+
4+
pub fn run(input: &[u8]) {
5+
println!("pro-tip: compile with --release!");
6+
let part1 = solve_part1(input).sfy();
7+
println!("part 1: {}", part1);
8+
assert_eq!(part1, "bkgcdefiholnpmja");
9+
10+
let part2 = solve_part2(input, 1000000000).sfy();
11+
println!("part 2: {}", part2);
12+
assert_eq!(part2, "knmdfoijcbpghlea");
13+
}
14+
15+
fn solve_part1(input: &[u8]) -> Vec<u8> {
16+
let a = "abcdefghijklmnop";
17+
return transform(input.sfy(), a.as_bytes());
18+
}
19+
20+
fn solve_part2(input: &[u8], iter: usize) -> Vec<u8> {
21+
let a = "abcdefghijklmnop".as_bytes();
22+
23+
// measure cycle
24+
let mut b: Vec<u8> = a.to_vec();
25+
let mut cycle = 0;
26+
loop {
27+
b = transform(input.sfy(), &b);
28+
cycle += 1;
29+
if a.to_vec() == b {
30+
break;
31+
}
32+
}
33+
println!("cycle length: {}", cycle);
34+
35+
// perform iter modulo cycle transformations
36+
let mut b = a.to_vec();
37+
for _ in 0..(iter % cycle) {
38+
b = transform(input.sfy(), &b);
39+
}
40+
return b;
41+
}
42+
43+
fn transform(moves: String, state: &[u8]) -> Vec<u8> {
44+
let L = state.len();
45+
let mut r = Vec::new();
46+
for i in 0..L {
47+
r.push(state[i]);
48+
}
49+
50+
let re_s = Regex::new(r"s([0-9]+)").unwrap();
51+
let re_x = Regex::new(r"x([0-9]+)/([0-9]+)").unwrap();
52+
let re_p = Regex::new(r"p([a-z]+)/([a-z]+)").unwrap();
53+
54+
for m in moves.split(",") {
55+
let cap = re_s.captures(m);
56+
if cap.is_some() {
57+
let spin: usize = cap.unwrap().get(1).unwrap().as_str().parse().unwrap();
58+
let t = r.clone();
59+
for i in 0..state.len() {
60+
r[(i + spin) % L] = t[i];
61+
}
62+
continue;
63+
}
64+
let cap = re_x.captures(m);
65+
if cap.is_some() {
66+
let cap = cap.unwrap();
67+
let a: usize = cap.get(1).unwrap().as_str().parse().unwrap();
68+
let b: usize = cap.get(2).unwrap().as_str().parse().unwrap();
69+
let t = r[a];
70+
r[a] = r[b];
71+
r[b] = t;
72+
continue;
73+
}
74+
let cap = re_p.captures(m);
75+
if cap.is_some() {
76+
let cap = cap.unwrap();
77+
let pa: &str = cap.get(1).unwrap().as_str();
78+
let pb: &str = cap.get(2).unwrap().as_str();
79+
let a = r.iter().position(|&c| c == pa.as_bytes()[0]).unwrap();
80+
let b = r.iter().position(|&c| c == pb.as_bytes()[0]).unwrap();
81+
82+
let t = r[a];
83+
r[a] = r[b];
84+
r[b] = t;
85+
continue;
86+
}
87+
panic!("invalid input: {}", m);
88+
}
89+
90+
return r;
91+
}

‎src/year_2017/mod.rs

Copy file name to clipboardExpand all lines: src/year_2017/mod.rs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ pub mod day12;
1313
pub mod day13;
1414
pub mod day14;
1515
pub mod day15;
16+
pub mod day16;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.