1
0
Fork 0

day1 solution

main
Andrew Coleman 2018-12-03 13:18:08 -05:00
parent cb276bfd5a
commit 8ed697e5fd
4 changed files with 1020 additions and 0 deletions

4
2018/day1/Cargo.lock generated Normal file
View File

@ -0,0 +1,4 @@
[[package]]
name = "day1"
version = "0.1.0"

6
2018/day1/Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "day1"
version = "0.1.0"
authors = ["Andrew Coleman <penguincoder@gmail.com>"]
[dependencies]

959
2018/day1/input Normal file
View File

@ -0,0 +1,959 @@
-2
-6
+2
-10
-4
-7
+8
-10
-15
-4
+20
+16
+20
+5
-2
+16
+8
-10
-2
-21
+13
+6
+5
-14
-13
-17
-8
-7
+19
-21
-16
+19
+7
+3
+17
+6
+8
-4
+2
+21
+8
+11
+5
+8
-15
-8
+1
+17
-7
-7
-17
+10
+6
+6
+12
+16
+1
+11
-10
-13
-2
+12
-14
+13
+13
+13
+6
-4
+1
+6
-1
-18
+14
+3
+18
-8
+15
-18
-21
+4
-8
-7
-16
-10
+2
-12
-15
+13
-11
-9
-5
-20
+6
-18
-15
-1
+8
+12
+15
+15
+20
+11
+15
+17
+10
-8
-7
+1
+12
+7
+11
+3
+5
-2
+14
-8
+15
-3
-5
-17
+15
-3
+23
+14
-11
-10
+14
-15
+6
+10
-9
+10
-3
-4
+10
+8
+16
-18
-2
+11
+14
-16
+6
-8
+16
-18
-16
-4
-6
+2
-3
+2
-8
-7
-5
+13
+2
-12
-5
-23
+13
-26
-16
-1
-9
-8
+21
-1
-16
-21
-16
+14
-10
+13
+18
+17
+3
+18
-10
+24
+10
-20
+36
-11
-19
+18
+43
+2
+5
+12
+14
-15
-14
+5
+11
+18
-15
+13
-4
+8
-5
-6
+17
+1
-3
+4
+11
+16
+16
+11
+11
+13
-12
+14
-16
-7
-9
+4
-10
-13
-19
+11
+5
-3
-9
+17
+6
+9
-12
+17
-11
+16
+13
+14
+10
-5
+2
-16
+18
-1
+8
+17
+11
+13
-18
+10
-1
+4
+9
+16
+13
-18
+1
+10
-7
+11
-12
-12
+7
-20
+18
-13
+3
+21
+17
+5
+14
-2
-14
+13
+4
-11
-19
-15
-7
+18
-1
+10
+9
-8
+2
-16
-13
-9
+3
+10
+15
+18
+9
-18
-13
+5
+10
+12
-10
+17
+16
-12
+23
+9
+18
+4
-28
+20
-4
+56
-19
-52
-28
-8
-35
-20
-3
-5
+16
-19
-8
+3
+9
+19
+22
+10
-9
+44
+4
-17
-12
-48
+7
-14
-22
+9
-4
-16
+5
+12
-3
-16
+12
+2
-15
+12
-26
-7
-12
-18
-16
+6
-15
+17
-9
-4
-8
-7
+12
-16
+8
-5
+3
+7
+8
-14
-8
-13
+10
+14
+26
+14
-6
+22
-13
-1
+11
-17
+15
+6
+18
+13
-5
+8
+1
-5
-19
-3
-3
+14
-10
-17
+5
+16
+7
+8
+5
+16
-20
+19
+4
-30
-1
-20
+5
-32
-12
+20
-14
+3
-11
-18
-21
+4
+1
-19
-17
+13
+12
+13
-1
-17
-19
+13
-6
+20
-17
-14
-22
+9
-3
+18
+24
-7
+25
-38
-96
+13
+37
+25
+58
-13
+42
+28
+8
+159
-43
-572
-62794
+19
+8
-17
-9
+3
-1
+5
+4
+16
+11
+4
-18
+5
-10
+9
+15
+16
-5
+17
-16
-4
-11
-18
+3
-12
+3
-18
-7
-7
+5
+3
-17
+8
-15
-2
+11
-6
+17
-12
-9
+6
-4
+10
+7
-1
-7
-16
+3
-1
-9
-4
-10
+2
-13
+4
-19
-7
-5
+1
-17
-3
-13
+15
-3
+9
+14
-9
+4
-1
-9
+14
-6
-3
-17
+19
-8
-15
-9
-18
+9
+19
-3
+16
-20
+3
-19
+10
+14
+11
-3
+10
+14
+12
-14
+15
-10
-10
+15
+9
+9
+4
-2
-9
-8
+11
+5
+16
-8
+16
-11
+9
-18
-11
+17
+15
-16
+2
-23
+14
+4
+14
+1
+6
+9
-19
-3
-17
-6
-16
-17
+16
+15
-9
+32
+11
-5
-22
-14
+5
-30
-13
-10
-11
-15
-5
-3
-7
+3
+9
+5
-1
-17
-17
+13
+14
-7
-16
+18
-16
+17
-9
+10
-17
-10
+19
+10
-16
-15
+16
-6
-6
-9
-17
+16
+3
-7
+17
-16
-16
-9
-9
+8
-1
-6
-14
+17
-12
-13
+12
-1
+13
+2
+10
-4
-5
-8
+14
-18
+16
+7
+8
+11
-14
-8
-15
-21
-18
+10
-3
+8
+8
-6
-13
-11
+6
-2
+14
-16
-16
-17
+10
+11
-13
-18
-5
+4
+9
-6
+5
+16
+13
+8
-15
+20
-7
+19
+7
+15
-7
-7
-18
-14
+9
-6
+13
+11
-8
+6
-5
-3
-26
-9
+6
-8
+15
-18
+12
-2
-8
-25
+15
-16
-1
-7
-18
+14
+6
+10
-15
-17
+12
-5
+8
-7
-10
-5
-18
+1
+14
-2
+11
+4
+18
-10
-19
-18
-19
+10
-14
-12
+14
-15
+10
+2
+12
-8
+14
+19
-20
+11
-8
+10
-21
+25
+21
+1
+20
+17
+6
+11
-13
-17
-20
+4
-1
-19
-11
+14
+12
-18
+5
-11
+33
+13
+8
+3
+9
-10
+30
-18
-3
+23
+42
+4
-7
+10
-14
+20
-5
+30
+1
-15
+19
+12
-3
-6
+7
+21
-3
+27
+17
+15
-18
-4
-17
+22
-14
-9
+12
+2
-1
+7
-10
+6
+3
+17
-4
-6
+13
-34
-33
-2
+18
-11
-17
-20
-3
+15
-5
-6
-48
+12
-22
-12
-17
-25
-9
-24
-15
-22
-15
+17
+5
-14
+22
+3
-1
-18
-37
-3
-8
+4
+9
+7
-5
-19
-19
-4
+12
-2
+35
-28
+8
+11
+80
+20
+12
-17
+16
-40
-43
-11
-19
-8
-2
+20
-33
+9
-71
-6
+1
-128
+1
+5
+28
-27
+16
+40
-9
+265
+54
-9
+43
-39
-63256
+12
+18
-14
+13
+15
-10
+13
+5
+1
+14
+15
-7
+18
-2
-7
-14
-6
-10
-15
-3
+12
+17
-19
+12
+8
+12
-19
+4
-18
-14
-20
-6
-19
+12
-11
-19
+2
+19
+4
-12
+3
+19
+11
+9
-17
-11
+1
+13
+11
-18
-12
-2
-14
-13
-6
+7
+2
-10
-14
-18
+1
+14
+11
-6
-6
-3
-18
-7
+127054

51
2018/day1/src/main.rs Normal file
View File

@ -0,0 +1,51 @@
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::iter::Iterator;
fn print_final_frequency(ints: &Vec<i32>) {
let mut frequency = 0;
for drift in ints {
frequency = frequency + drift;
}
println!("frequency is {}", frequency)
}
fn print_first_duped_frequency(line_count: usize, offsets: &Vec<i32>) {
let mut freaks: Vec<i32> = vec![0];
let first_duped_frequency = duped_frequency(0, line_count, offsets, &mut freaks);
println!("first duped frequency is {}", first_duped_frequency);
}
fn duped_frequency(
index: usize,
max_count: usize,
offsets: &Vec<i32>,
freaks: &mut Vec<i32>,
) -> i32 {
let cur_offset = offsets.get(index).unwrap();
let next_freq = freaks.last().unwrap() + cur_offset;
let mut found = false;
for freq in freaks.iter() {
if freq == &next_freq {
found = true;
break;
}
}
if found {
next_freq
} else {
let next_index = (index + 1) % max_count;
freaks.push(next_freq);
duped_frequency(next_index, max_count, offsets, freaks)
}
}
fn main() {
let ints: Vec<i32> = BufReader::new(File::open("input").unwrap())
.lines()
.map(|line| line.unwrap().parse::<i32>().unwrap())
.collect();
let line_count: usize = ints.len();
print_final_frequency(&ints);
print_first_duped_frequency(line_count, &ints)
}