Skip to content
Snippets Groups Projects
Commit 01a40fa0 authored by Philip Molares's avatar Philip Molares :popcorn:
Browse files

added day10

parent 346386b8
No related branches found
No related tags found
No related merge requests found
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "day10"
version = "0.1.0"
[package]
name = "day10"
version = "0.1.0"
authors = ["Philip Molares <philip.molares@udo.edu>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
95
43
114
118
2
124
120
127
140
21
66
103
102
132
136
93
59
131
32
9
20
141
94
109
143
142
65
73
27
83
133
104
60
110
89
29
78
49
76
16
34
17
105
98
15
106
4
57
1
67
71
14
92
39
68
125
113
115
26
33
61
45
46
11
99
7
25
130
42
3
10
54
44
139
50
8
58
86
64
77
35
79
72
36
80
126
28
123
119
51
22
\ No newline at end of file
use std::fs::File;
use std::io::{self, prelude::*, BufReader};
fn calculate_differences(input: &Vec<i64>) -> (i64, i64, i64) {
let mut ones = 0;
let mut twos = 0;
let mut threes = 0;
let mut joltage = 0;
for number in input {
match number - joltage {
0 => (),
1 => ones += 1,
2 => twos += 1,
3 => threes += 1,
_ => unimplemented!()
}
joltage = *number;
}
(ones, twos, threes)
}
fn part_2(input: &Vec<i64>) -> i64 {
let mut slices = vec![];
let mut current_slice = vec![];
for window in input.windows(2) {
match window[1] - window[0] {
1 => current_slice.push(window[0]),
3 => {
current_slice.push(window[0]);
slices.push(current_slice);
current_slice = vec![];
}
_ => (),
}
}
slices
.iter()
.map(|slice| match slice.len() {
1 => 1,
2 => 1,
3 => 2,
4 => 4,
5 => 7,
_ => panic!("unexpected slice of size N > 5 consecutive 1-diff elements"),
})
.product()
}
fn main() -> io::Result<()> {
println!("Advent of Code 2020 – Day 10:");
let file = File::open("input.txt")?;
let reader = BufReader::new(file);
let mut input = reader
.lines()
.map(|l| l.unwrap().parse::<i64>().unwrap())
.collect::<Vec<i64>>();
input.sort();
input.insert(0, 0);
input.push(*input.last().unwrap() + 3);
let (ones, _, threes) = calculate_differences(&input);
println!("Answer Part 1: {}", ones*threes);
let answer2 = part_2(&input);
println!("Answer Part 2: {}", answer2);
Ok(())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment