-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
75 lines (64 loc) · 1.56 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
use std::io;
#[derive(Debug, Clone)]
struct Board
{
buf: Vec<u8>,
elves: [usize; 2],
}
fn main() -> Result<(), io::Error>
{
let board = Board{ buf: vec!(3, 7, 1, 0), elves: [0, 1] };
let input = 505961;
println!("Part 1: {}", part1(input, board.clone()));
println!("Part 2: {}", part2(input.to_string(), board));
Ok(())
}
fn part1(steps: usize, mut b: Board) -> String
{
while b.buf.len() < steps + 10 {
do_step(&mut b)
}
let len = b.buf.len();
b.buf[len-10..len].iter()
.map(|c| (c + 0x30) as char)
.collect()
}
fn part2(sought_str: String, mut b: Board) -> usize
{
let sought: Vec<u8> = sought_str
.chars()
.map(|c| c as u8 - 0x30)
.collect();
b.buf.reserve(200_000_000);
loop {
let prev_size = b.buf.len();
do_step(&mut b);
let new_size = b.buf.len();
let size_diff = new_size - prev_size;
for i in 0..size_diff {
if i + sought.len() > new_size {
break
}
let rng = &b.buf[new_size-i-sought.len()..new_size-i];
if rng == &sought[..] {
return new_size - sought.len() - i;
}
}
}
}
fn do_step(b: &mut Board) -> ()
{
let new_val : u8 = b.elves
.iter()
.map(|e| b.buf[*e])
.sum();
if new_val > 9 {
b.buf.push(new_val / 10);
}
b.buf.push(new_val % 10);
for e in b.elves.iter_mut() {
let nv = b.buf[*e] as usize + 1 + *e;
let nv = nv % b.buf.len();
*e = nv;
}
}