-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12.rs
142 lines (123 loc) · 4.17 KB
/
day12.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
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
use std::fs::File;
use std::io::{BufReader, BufRead};
use std::collections::HashMap;
fn main()
{
part1();
part2();
}
fn part1()
{
// open the text file and store
let input_file = File::open("input_day12.txt").expect("No such file");
// create buffer to read the contents from file
let mut buf = BufReader::new(input_file);
// output vector
let mut output: Vec<usize> = vec![0];
// dictionary to store all the inputs
let mut dict: HashMap<usize, Vec<usize>> = HashMap::new();
// create String variable to store each line of file in
let mut line: String;
for _ in 0..2000 {
// read line from file and store into line variable (String)
// no assignment as no return value is needed
line = "".to_owned();
buf.read_line(&mut line).expect("Failed to read line");
let line = line.trim();
// replace unwanted tokens in string, store in String temp
let mut temp = line.replace(" <-> ", " ");
temp = temp.replace(",", "");
// create vector of usize of each number in the line
// includes left AND right side
let array : Vec<usize> = temp.split(" ").map(|x| x.parse::<usize>().unwrap()).collect();
// create vector of "pipes" aka values the port maps to
let mut pipes : Vec<usize> = Vec::new();
// iterate throuh the array of values starting at
// first pipe (index 1) and add to pipes vector
for spot in 1..array.len() {
pipes.push(array[spot]);
}
// insert key-value pair (port, pipes) to dictionary
dict.insert(array[0], pipes);
}
for x in 0.. {
if x == output.len() {
break;
}
let lol : Vec<usize> = dict[&output[x]].clone();
for num in lol.iter() {
if !output.contains(num) {
output.push(*num);
}
}
}
println!("Part 1: {}", output.len());
}
fn part2()
{
// open the text file and store
let input_file = File::open("input_day12.txt").expect("No such file");
// create buffer to read the contents from file
let mut buf = BufReader::new(input_file);
// output vector
let mut output : Vec<usize> = vec![0];
// dictionary to store all the inputs
let mut dict : HashMap<usize, Vec<usize>> = HashMap::new();
// create String variable to store each line of file in
let mut line : String;
for _ in 0..2000
{
// read line from file and store into line variable (String)
// no assignment as no return value is needed
line = "".to_owned();
buf.read_line(&mut line).expect("Failed to read line");
let line = line.trim();
// replace unwanted tokens in string, store in String temp
let mut temp = line.replace(" <-> ", " ");
temp = temp.replace(",", "");
// create vector of usize of each number in the line
// includes left AND right side
let array : Vec<usize> = temp.split(" ").map(|x| x.parse::<usize>().unwrap()).collect();
// create vector of "pipes" aka values the port maps to
let mut pipes : Vec<usize> = Vec::new();
// iterate throuh the array of values starting at
// first pipe (index 1) and add to pipes vector
for spot in 1..array.len()
{
pipes.push(array[spot]);
}
// insert key-value pair (port, pipes) to dictionary
dict.insert(array[0], pipes);
}
let mut lol : Vec<usize>;
let mut sum = 0;
while dict.len() > 0
{
for x in 0..
{
if x == output.len()
{
sum += 1;
break;
}
lol = dict[&output[x]].clone();
for num in lol.iter()
{
if !output.contains(num)
{
output.push(*num);
}
}
}
for y in output.iter()
{
dict.remove(y);
}
output.clear();
if dict.len() > 0
{
output.push(*dict.keys().next().unwrap());
}
}
println!("Part 2: {}", sum);
}