-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.rs
91 lines (72 loc) · 1.83 KB
/
day4.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
use std::io::{BufRead, BufReader};
//use std::str::*;
use std::fs::File;
fn main()
{
part1();
part2();
}
fn part1()
{
let input_file = File::open("input_day4.txt").expect("Error opening");
let buf = BufReader::new(input_file);
let mut sum : i16 = 0;
'outer: for line in buf.lines()
{
let line = line.unwrap();
let input : Vec<&str> = line.split(char::is_whitespace).collect();
for x in 0..input.iter().len()
{
for y in 0..input.len()
{
if x != y && input[x] == input[y]
{
continue 'outer;
}
}
}
sum += 1;
}
println!{"Part 1: {}", sum};
}
fn part2()
{
let input_file = File::open("input_day4.txt").expect("Error opening file");
let buf = BufReader::new(input_file);
let mut sum : i16 = 0;
'outer:
for line in buf.lines()
{
let line = line.unwrap();
let input : Vec<&str> = line.split(char::is_whitespace).collect();
// ["mary", "little", "lamb", "ramy"]
let input : Vec<String> = input.iter().map(|x| String::from(*x)).collect();
let mut words : Vec<Vec<u8>> = Vec::new();
for x in input.iter()
{
words.push((*x).as_bytes().to_vec());
}
for x in words.iter_mut()
{
(*x).sort();
}
/*
for y in words.iter()
{
if words.contains(y)
{
continue 'outer;
}
}
*/
//let new : Vec<Vec<u8>> = words.dedup().clone();
let length = words.len();
words.sort();
words.dedup();
if length == words.len()
{
sum += 1;
}
}
println!("Part 2: {}", sum);
}