Skip to content

Commit 38d9bd1

Browse files
committed
Refactoring
1 parent 2b37de2 commit 38d9bd1

File tree

5 files changed

+225
-220
lines changed

5 files changed

+225
-220
lines changed

src/day1.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use super::utils;
2+
3+
fn day1_part1(input: &Vec<i32>) -> i32 {
4+
for i in 0..input.len() - 1 {
5+
for j in i + 1..input.len() {
6+
if input[i] + input[j] == 2020 {
7+
return input[i] * input[j];
8+
}
9+
}
10+
}
11+
return 0;
12+
}
13+
14+
fn day1_part2(input: &Vec<i32>) -> i32 {
15+
for i in 0..input.len() - 2 {
16+
for j in i + 1..input.len() - 1 {
17+
for k in j + 1..input.len() {
18+
if input[i] + input[j] + input[k] == 2020 {
19+
return input[i] * input[j] * input[k];
20+
}
21+
}
22+
}
23+
}
24+
return 0;
25+
}
26+
27+
pub fn run() {
28+
println!("day1");
29+
30+
let mut input: Vec<i32> = Vec::new();
31+
32+
if let Ok(lines) = utils::read_lines("data/day1.txt") {
33+
for line in lines {
34+
if let Ok(v) = line {
35+
if let Ok(n) = v.parse::<i32>() {
36+
input.push(n);
37+
}
38+
}
39+
}
40+
}
41+
42+
// part 1
43+
println!("{}", day1_part1(&input));
44+
45+
// part 2
46+
println!("{}", day1_part2(&input));
47+
}

src/day2.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use super::utils;
2+
3+
struct Policy {
4+
ch: char,
5+
min: i32,
6+
max: i32,
7+
}
8+
9+
fn day2_part1(p: &Policy, pass: &str) -> bool {
10+
let mut n: i32 = 0;
11+
for c in pass.chars() {
12+
if c == p.ch {
13+
n = n + 1;
14+
}
15+
}
16+
return n >= p.min && n <= p.max;
17+
}
18+
19+
fn day2_part2(p: &Policy, pass: &str) -> bool {
20+
let first: char = pass.chars().nth(p.min as usize - 1).unwrap_or('\0');
21+
let second: char = pass.chars().nth(p.max as usize - 1).unwrap_or('\0');
22+
23+
return (first == p.ch || second == p.ch) && first != second;
24+
}
25+
26+
pub fn run() {
27+
println!("day2");
28+
29+
let mut count1: i32 = 0;
30+
let mut count2: i32 = 0;
31+
if let Ok(lines) = utils::read_lines("data/day2.txt") {
32+
for line in lines {
33+
if let Ok(v) = line {
34+
let tokens: Vec<&str> = v.split(" ").collect();
35+
let tmp: Vec<&str> = tokens[0].split("-").collect();
36+
let p: Policy = Policy {
37+
ch: tokens[1].chars().nth(0).unwrap(),
38+
min: tmp[0].parse::<i32>().unwrap(),
39+
max: tmp[1].parse::<i32>().unwrap(),
40+
};
41+
42+
if day2_part1(&p, tokens[2]) {
43+
count1 = count1 + 1;
44+
}
45+
if day2_part2(&p, tokens[2]) {
46+
count2 = count2 + 1;
47+
}
48+
}
49+
}
50+
}
51+
52+
println!("part1: {}", count1);
53+
println!("part2: {}", count2);
54+
}
55+
56+
#[test]
57+
fn test_day2_part1() {
58+
assert_eq!(
59+
true,
60+
day2_part1(
61+
&Policy {
62+
ch: 'a',
63+
min: 1,
64+
max: 3
65+
},
66+
"abcde"
67+
)
68+
);
69+
assert_eq!(
70+
false,
71+
day2_part1(
72+
&Policy {
73+
ch: 'b',
74+
min: 1,
75+
max: 3
76+
},
77+
"cdefg"
78+
)
79+
);
80+
assert_eq!(
81+
true,
82+
day2_part1(
83+
&Policy {
84+
ch: 'c',
85+
min: 2,
86+
max: 9
87+
},
88+
"ccccccccc"
89+
)
90+
);
91+
}

src/day3.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use super::utils;
2+
3+
fn day3_calc(map: &Vec<String>, right: usize, down: usize) -> usize {
4+
let mut cnt: usize = 0;
5+
let mut n: usize = 0;
6+
for i in (0..map.len()).step_by(down) {
7+
if i == 0 {
8+
continue;
9+
}
10+
n = n + 1;
11+
let row: &String = &map[i];
12+
let p: usize = n * right;
13+
if row.chars().nth(p % row.len()).unwrap_or('\0') == '#' {
14+
cnt = cnt + 1;
15+
}
16+
}
17+
return cnt;
18+
}
19+
20+
#[test]
21+
fn test_day3_calc() {
22+
let map: Vec<String> = vec![
23+
String::from("..##......."),
24+
String::from("#...#...#.."),
25+
String::from(".#....#..#."),
26+
String::from("..#.#...#.#"),
27+
String::from(".#...##..#."),
28+
String::from("..#.##....."),
29+
String::from(".#.#.#....#"),
30+
String::from(".#........#"),
31+
String::from("#.##...#..."),
32+
String::from("#...##....#"),
33+
String::from(".#..#...#.#"),
34+
];
35+
assert_eq!(2, day3_calc(&map, 1, 1));
36+
assert_eq!(7, day3_calc(&map, 3, 1));
37+
assert_eq!(3, day3_calc(&map, 5, 1));
38+
assert_eq!(4, day3_calc(&map, 7, 1));
39+
assert_eq!(2, day3_calc(&map, 1, 2));
40+
}
41+
fn day3_part1(map: &Vec<String>) -> usize {
42+
return day3_calc(map, 3, 1);
43+
}
44+
45+
fn day3_part2(map: &Vec<String>) -> usize {
46+
let a1 = day3_calc(map, 1, 1);
47+
let a2 = day3_calc(map, 3, 1);
48+
let a3 = day3_calc(map, 5, 1);
49+
let a4 = day3_calc(map, 7, 1);
50+
let a5 = day3_calc(map, 1, 2);
51+
return a1 * a2 * a3 * a4 * a5;
52+
}
53+
54+
pub fn run() {
55+
println!("day3");
56+
57+
let mut map: Vec<String> = Vec::new();
58+
if let Ok(lines) = utils::read_lines("data/day3.txt") {
59+
for line in lines {
60+
if let Ok(v) = line {
61+
map.push(v);
62+
}
63+
}
64+
}
65+
println!("{}", day3_part1(&map));
66+
println!("{}", day3_part2(&map));
67+
}

0 commit comments

Comments
 (0)