Day 1 2018

This commit is contained in:
Wesley Moore 2018-12-02 09:53:05 +11:00
parent be7a6b76b3
commit 696b90944d
No known key found for this signature in database
GPG key ID: BF67766C0BC2D0EE
4 changed files with 1039 additions and 0 deletions

4
2018/Cargo.lock generated Normal file
View file

@ -0,0 +1,4 @@
[[package]]
name = "advent-of-code"
version = "0.1.0"

6
2018/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "advent-of-code"
version = "0.1.0"
authors = ["Wesley Moore <wes@wezm.net>"]
[dependencies]

993
2018/input/2018/day1.txt Normal file
View file

@ -0,0 +1,993 @@
+3
+15
-1
-18
+3
-10
+3
+10
-8
-20
+13
+11
-21
-10
-16
-2
-6
+19
-8
+12
-13
+4
-9
-20
+12
+15
-17
-18
+11
-17
-9
-12
-9
+3
+9
-6
+18
+16
+9
-11
-1
+8
-20
+17
-3
-16
-7
+10
+6
+3
+15
+3
-2
+9
-8
-6
+11
+5
+24
+10
+4
-8
+2
+13
-16
+13
-16
+11
+7
-9
-13
-12
+17
-4
-5
-1
+16
+22
+8
+12
+9
+2
-3
-5
+12
+4
+4
+8
+18
+9
-14
-11
-5
-11
+15
-18
-7
-12
+1
+5
-18
+14
-13
-10
+17
+8
+11
-12
+20
-16
+22
+5
+1
-9
+16
+13
+6
+5
+7
-4
-4
-7
+16
+4
-1
-15
-11
+19
+9
+17
-15
+21
-7
+4
-16
-3
-9
+4
+11
+12
+11
+7
+12
+14
-6
-1
-18
-3
-13
+4
+10
-15
-10
-8
+15
+15
+19
+16
-3
-14
+13
+16
+1
+10
+9
+7
-11
-19
+2
-3
+10
-2
+13
-19
-7
+3
+18
+1
+9
+2
+12
-13
-9
-7
-15
-18
-6
-1
-8
-12
+19
-1
-14
-6
-16
-4
+1
+4
-22
-19
-2
+4
+18
-8
-19
-16
+6
+16
-7
+4
+8
-20
-11
-11
+20
+15
+19
+30
+19
+19
-11
-18
+4
+1
+21
-9
-9
+12
+18
-1
-6
+12
+16
+12
+3
+1
-9
-15
-15
-1
-1
-18
-32
-11
-17
-20
+4
+14
-1
+16
+20
+53
+19
+16
+14
+14
+6
-8
+14
-17
-5
+3
+14
-3
-15
-10
-19
+17
+3
+17
+5
+14
-7
+5
+14
+18
-2
-19
+8
+10
+15
-6
+13
-11
+19
+10
-15
-15
+2
-4
-3
+11
+2
+18
+3
+17
+9
-16
-8
+6
+15
-11
-3
+20
-1
+12
+1
+8
+11
-8
+6
+7
+17
-12
+13
-8
+16
+15
-8
-12
+7
-12
-18
+5
+5
+7
-8
+6
-4
-15
+17
-15
-10
-1
+17
+18
-21
-19
-9
+19
+16
+18
-5
-15
+19
-11
+10
+9
-1
+4
+4
+9
-5
+20
+4
-18
+17
+12
-18
+14
-21
-15
-5
-18
-17
-19
+11
+12
-20
-19
+12
-9
+8
-1
-22
+4
+15
+14
+9
-10
-26
-22
-10
+7
+9
-1
-16
-21
-18
+8
+6
+18
+1
+5
+11
+18
+6
+20
-7
-10
-5
+33
-21
-8
-13
-5
+7
+1
-7
-39
-14
-12
+16
-14
-15
+4
+2
+12
-11
+16
+8
+15
-1
+21
-5
+13
-18
-7
+2
-15
+19
+8
+26
-24
+37
+59
+21
-19
+18
+6
-11
+15
-2
+5
-2
+20
+8
+2
-7
+18
-8
-1
-8
+3
+17
+12
+18
+2
-8
+3
+40
+3
+21
+5
-1
+15
+2
+16
-50
+15
+5
+21
-31
-17
-15
-10
-9
-2
-18
-14
+7
-14
+6
-20
-8
+13
-27
+8
+2
-16
+3
-7
-6
+23
+31
+52
-6
-1
+2
-54
-42
-6
+18
+2
+1
-29
+36
-42
+7
-146
-2
-14
-13
+19
-8
+19
-15
-18
-4
-18
-1
+2
-3
+11
-16
-23
-9
-30
-31
+58
+18
-19
+45
+11
+14
-16
+19
-10
+17
+12
-2
-8
+19
-50
+8
-114
-40
-62
-153
-60
+12
-23
-40
-87
+377
-79652
-12
+1
-9
-5
-12
-14
+9
-3
-2
+8
-15
-18
+19
-6
+4
-14
-2
-18
-19
-8
+2
+11
-19
-11
+5
-3
+4
+1
-3
+9
+7
+15
-9
-2
-6
+3
+2
+10
+9
+3
-5
-4
+12
+15
-6
-11
-9
-15
+9
-19
-15
+4
+7
+7
-16
+15
-4
-19
-8
-11
-18
-14
-18
-16
+5
+5
+12
-9
-16
+2
-6
-2
+20
-13
-9
-16
+4
-1
+2
-3
-17
-2
-11
+1
+6
+17
-4
-15
-3
-19
+6
-16
-18
-6
+2
+12
-9
+6
+7
-6
+7
-9
-18
-3
+10
-17
+11
+16
+16
+6
+13
-17
+2
+3
+17
-10
+8
-1
-9
-15
+11
+2
-14
+7
-12
-19
-3
-7
-7
-15
-17
+5
+3
+14
+6
-5
+17
-9
-5
+1
-8
-13
+7
+17
-12
-14
-18
+10
+16
-3
-19
+7
+14
+14
+11
-1
+18
-16
-4
+10
-14
+9
-17
-1
+4
-14
-16
+1
-14
-7
+1
+7
-11
-4
-14
+19
-11
+8
+14
+8
-15
-18
+2
-17
-19
+13
+3
+6
+8
-10
+7
-3
-9
+8
+12
-4
+15
+7
+10
+11
-10
-20
-16
+11
-1
-21
-13
-19
-15
-15
-7
+10
+19
-17
-6
-10
-13
+12
-18
+16
+10
-13
+12
-1
+12
+12
+10
+16
-18
-5
-10
-13
-7
+19
+14
+18
-2
-4
-13
+14
+18
-7
+11
-13
-21
-12
+14
+16
+7
-19
+16
+7
+2
-5
+1
+19
+16
+14
+9
+12
+14
-8
-28
+13
+11
+13
-17
-21
+15
-37
-11
+19
+1
+8
+12
-1
-27
-21
-19
-21
+15
-24
-7
-11
-17
-15
-11
-11
+8
-9
-9
-4
-6
+15
-10
-3
+19
+9
+8
+6
+11
-4
-14
-1
+3
-15
+17
+18
-10
-22
-15
+14
+6
-14
-11
+4
-18
+5
-16
-9
-13
+14
+3
+9
+17
-9
+22
-12
-8
-5
-1
-6
-6
-19
+17
+9
+28
+20
+14
+11
+6
+7
-2
+17
+17
-12
+7
+21
-2
-4
+10
+2
+12
-19
-22
-8
-20
+25
-17
-15
-8
-1
+13
-15
+16
+15
+19
-128
-4
-16
-4
+13
-10
+20
-16
-13
+15
+9
+1
-11
-16
+13
+1
+15
+1
-31
+11
-53
-11
-9
-16
-11
+7
+6
+13
+6
-12
-3
+16
+2
-5
+7
-16
+17
-13
-9
+15
-3
+13
+8
+5
-8
-1
-5
-21
+25
+23
+12
+1
-2
+7
-25
-51
-5
+4
+14
-8
+4
+11
-5
-21
-14
+9
+8
-13
+4
+80915

36
2018/src/bin/day1.rs Normal file
View file

@ -0,0 +1,36 @@
use std::io::{self, BufRead, BufReader};
use std::fs::{self, File};
use std::collections::HashSet;
const INPUT: &str = "input/2018/day1.txt";
fn main() {
part1();
part2();
}
fn part1() {
let input = File::open(INPUT).expect("input");
let input = BufReader::new(input);
let frequency = input.lines().map(|line| {
line.unwrap().parse::<i32>().expect("unable to parse line")
}).sum::<i32>();
println!("{}", frequency);
}
fn part2() {
let input = fs::read_to_string(INPUT).expect("input");
let mut frequency = 0;
let mut seen = HashSet::new();
for line in input.lines().cycle() {
let drift = line.parse::<i32>().expect("unable to parse line");
frequency += drift;
if !seen.insert(frequency) {
break;
}
}
println!("{}", frequency);
}