summaryrefslogtreecommitdiff
path: root/2017/src/lib.rs
blob: 53d7d20ee95560f316796bbc7586c56cc3e77aae (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
extern crate structopt;
#[macro_use]
extern crate structopt_derive;
use structopt::StructOpt;

use std::path::PathBuf;
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;
use std::process;

#[derive(StructOpt, Debug)]
#[structopt(name = "AOC2017", about = "An Advent of Code CLI arguments object.")]
struct AdventCli {
    #[structopt(help = "Which part of the puzzle you are solving")]
    part: u32,

    #[structopt(help = "Input file", parse(from_os_str))]
    input: PathBuf
}

pub struct AdventArgs {
    pub part: u32,
    pub input: Vec<String>
}

impl AdventArgs {
    pub fn init() -> AdventArgs {
        let opt = AdventCli::from_args();
        let input = match AdventArgs::read_file(&opt.input) {
            Ok(input) => input,
            Err(error) => {
                // Typically I would think of exiting the program like
                // this to be bad form, but in this case I'm matching the
                // interface of StructOpt: if the input parameters were
                // invalid, just quit now with a nice message.
                eprintln!("Error reading file: {}", error);
                process::exit(1);
            }
        };
        AdventArgs {
            part: opt.part,
            input: input
        }
    }

    fn read_file(file: &PathBuf) -> Result<Vec<String>, std::io::Error> {
        let file = File::open(file)?;
        let file_reader = BufReader::new(file);
        file_reader.lines()
            .collect::<Result<Vec<_>, _>>()
            .map(AdventArgs::preprocess_file_lines)
    }

    fn preprocess_file_lines(lines: Vec<String>) -> Vec<String> {
        lines.iter()
            .filter(|line| line.len() > 0)
            .map(|line| line.trim_right().to_string())
            .collect()
    }

    pub fn one_number_input(&self) -> Result<i32, std::num::ParseIntError> {
        self.input[0].parse()
    }
    pub fn number_per_line_input(&self) -> Result<Vec<i32>, std::num::ParseIntError> {
        self.input.iter().map(|line| line.parse()).collect()
    }
}

pub fn parse_space_separated_ints(line: &String) -> Result<Vec<i32>, std::num::ParseIntError> {
    line.split_whitespace()
        .map(|x| x.parse::<i32>())
        .collect()
}


#[derive(Hash, Eq, PartialEq, Debug, Clone, Copy)]
pub struct Point {
    pub x: i32,
    pub y: i32
}

impl Point {
    pub fn up(&self) -> Point {
        Point {
            y: self.y-1,
            ..*self
        }
    }

    pub fn down(&self) -> Point {
        Point {
            y: self.y+1,
            ..*self
        }
    }

    pub fn left(&self) -> Point {
        Point {
            x: self.x-1,
            ..*self
        }
    }

    pub fn right(&self) -> Point {
        Point {
            x: self.x+1,
            ..*self
        }
    }

    pub fn shift(&self, dir: &Direction) -> Point {
        use Direction::*;
        
        match *dir {
            Right => self.right(),
            Left => self.left(),
            Up => self.up(),
            Down => self.down()
        }
    }
}

#[derive(Debug)]
pub enum Direction {
    Left,
    Up,
    Down,
    Right
}

impl Direction {
    pub fn rotate_left(&self) -> Direction {
        use Direction::*;
        match *self {
            Right => Up,
            Up => Left,
            Left => Down,
            Down => Right
        }
    }

    pub fn rotate_right(&self) -> Direction {
        use Direction::*;
        match *self {
            Right => Down,
            Up => Right,
            Left => Up,
            Down => Left
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Point3d {
    pub x: i32,
    pub y: i32,
    pub z: i32
}

impl std::ops::Add for Point3d {
    type Output = Point3d;

    fn add(self, other: Point3d) -> Point3d {
        Point3d {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z + other.z
        }
    }
    
}

impl Point3d {
    pub fn manhattan_distance(&self) -> i32 {
        self.x.abs() + self.y.abs() + self.z.abs()
    }
}

pub fn knot_hash(input: &String) -> String {
    let suffix: [usize; 5] = [17, 31, 73, 47, 23];
    let lengths: Vec<usize> = input.as_bytes()
        .iter().map(|&x| x as usize)
        .chain(suffix.iter().cloned())
        .collect();

    let mut position = 0;
    let mut list: Vec<u32> = (0..256).collect();

    for i in 0..64 {
        let skip = lengths.len() * i;
        knot_hash_round(&mut list, &lengths, &mut position, skip);            
    }

    let mut current_char = 0;
    let mut result = String::new();
    for (i, l) in list.iter().enumerate() {
        current_char = current_char ^ l;
        if i % 16 == 15 {
            result.push_str(&format!("{:02x}", current_char));
            current_char = 0;
        }
    }
    result
}

fn knot_hash_round(list: &mut Vec<u32>, lengths: &Vec<usize>, position: &mut usize, skip: usize) {
    for (inner_skip, &length) in lengths.iter().enumerate() {
        knot_hash_reverse_segment(list, *position, length);
        *position = (*position + length + skip + inner_skip) % list.len();
    }
}

fn knot_hash_reverse_segment(list: &mut Vec<u32>, position: usize, length: usize) {
    let mut a = position;
    let mut b = position + length - 1;
    let len = list.len();
    while a < b {
        list.swap(a%len, b%len);
        
        a += 1;
        b -= 1;
    }
}