Skip to content

Commit

Permalink
2015 day 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaikia committed Jul 17, 2024
1 parent 3f64c54 commit 2dc4225
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/bin/2015_01/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use aoc::common;

fn part1(input: &str) -> i32 {
input.chars().filter(|&c| c == '(').count() as i32 - input.chars().filter(|&c| c == ')').count() as i32
}

fn part2(input: &str) -> usize {
let mut ret = 1;
let mut floor = 0;
for (i, e) in input.chars().enumerate() {
if e == '(' {
floor += 1;
} else {
floor -= 1;
}

if floor == -1 {
ret = i + 1;
break;
}
}
ret
}

fn main() {
let input = common::get_input();
common::timed(&input, part1, true);
common::timed(&input, part2, false);
}
30 changes: 30 additions & 0 deletions src/bin/2015_02/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use aoc::common;

fn part1(input: &str) -> i32 {
let mut total_wrapping_paper_area = 0;
for dims_str in input.lines() {
let dims = dims_str.split('x').map(|s| s.parse::<i32>().unwrap()).collect::<Vec<_>>();
let mut areas = [dims[0] * dims[1], dims[1] * dims[2], dims[2] * dims[0]];
areas.sort();
let wrapping_paper_area = areas[0] + 2 * (areas[0] + areas[1] + areas[2]);
total_wrapping_paper_area += wrapping_paper_area;
}
total_wrapping_paper_area
}

fn part2(input: &str) -> i32 {
let mut total_ribbon_length = 0;
for dims_str in input.lines() {
let mut dims = dims_str.split('x').map(|s| s.parse::<i32>().unwrap()).collect::<Vec<_>>();
dims.sort();
let ribbon_length = 2 * (dims[0] + dims[1]) + dims[0] * dims[1] * dims[2];
total_ribbon_length += ribbon_length;
}
total_ribbon_length
}

fn main() {
let input = common::get_input();
common::timed(&input, part1, true);
common::timed(&input, part2, false);
}

0 comments on commit 2dc4225

Please sign in to comment.