-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
76 lines (70 loc) · 1.66 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::fs;
fn get_input(fp: &str) -> Vec<isize> {
fs::read_to_string(fp)
.expect("Cannot read file")
.trim_end()
.chars()
.map(|c| c.to_digit(10).unwrap() as isize)
.collect()
}
fn fft(a: Vec<isize>) -> Vec<isize> {
let patt: [isize; 4] = [0, 1, 0, -1];
let n = a.len();
let mut new = Vec::new();
for i in 1..=n {
let mut res = 0isize;
for j in 1..=n {
res += a[j - 1] * patt[(j / i) % 4];
}
new.push(res.abs() % 10);
}
new
}
fn fast_fft(a: &[isize], offset: usize, rep: usize) -> String {
let mut b = a[offset..].to_vec();
for _ in 0..rep {
let mut new = Vec::new();
let mut res = 0;
for x in b.iter().rev() {
res += x;
new.push(res.abs() % 10);
}
new.reverse();
b = new;
}
b[..8]
.iter()
.map(|i| i.to_string())
.collect::<Vec<String>>()
.join("")
}
fn part_one(inp: &[isize]) -> String {
let mut x = inp.to_vec();
for _ in 0..100 {
x = fft(x);
}
x[..8]
.iter()
.map(|i| i.to_string())
.collect::<Vec<String>>()
.join("")
}
fn part_two(inp: &[isize]) -> String {
let offset: usize = inp[..7]
.iter()
.map(|i| i.to_string())
.collect::<Vec<String>>()
.join("")
.parse()
.unwrap();
let mut a = Vec::new();
for _ in 0..10000 {
a.extend(inp.iter());
}
fast_fft(&a, offset, 100)
}
fn main() {
let inp = get_input("../input.txt");
println!("Part one: {}", part_one(&inp));
println!("Part two: {}", part_two(&inp));
}