Compare commits

...
2 Commits
Author SHA1 Message Date
niku 44e5a16b48 Added 2022 day 4 2022-12-04 20:56:40 +01:00
niku 422c1a4124 Updated license 2022-12-04 20:56:25 +01:00
5 changed files with 1089 additions and 1 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License
Copyright (c) <year> <copyright holders>
Copyright (c) 2022 niku
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "aoc2022"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
File diff suppressed because it is too large Load Diff
+78
View File
@@ -0,0 +1,78 @@
#[derive(Debug)]
struct Range(i32, i32);
impl Range {
fn contains(&self, other: &Range) -> bool {
return self.0 <= other.0 && self.1 >= other.1;
}
fn from_str(string: &str) -> Range {
let (start, end) = string.split_once('-').unwrap();
return Range(start.parse::<i32>().unwrap(), end.parse::<i32>().unwrap());
}
}
fn parse_pairs(line: &str) -> (Range, Range) {
let (left, right) = line.split_once(',').unwrap();
return (Range::from_str(left), Range::from_str(right));
}
fn solution_part1(input: &str) -> i32 {
let mut result: i32 = 0;
for line in input.lines() {
let (left, right) = parse_pairs(line);
if left.contains(&right) || right.contains(&left) {
result += 1;
}
}
return result;
}
fn solution_part2(input: &str) -> i32 {
let mut result: i32 = 0;
for line in input.lines() {
let (left, right) = parse_pairs(line);
if left.0 <= right.1 && left.1 >= right.0 {
result += 1;
}
}
return result;
}
#[cfg(test)]
mod tests {
use std::fs;
use super::*;
#[test]
fn contains() {
assert!(Range(4, 6).contains(&Range(6, 6)))
}
#[test]
fn basic_example() {
let input = "2-4,6-8\n2-3,4-5\n5-7,7-9\n2-8,3-7\n6-6,4-6\n2-6,4-8";
let expected = 2;
let result = solution_part1(input);
assert_eq!(result, expected);
}
#[test]
fn answer_part1() {
let input = fs::read_to_string("inputs/day_4.txt").unwrap();
println!(
"Answer to part 1 of day 4: {:#?}",
solution_part1(input.as_str())
);
}
#[test]
fn answer_part2() {
let input = fs::read_to_string("inputs/day_4.txt").unwrap();
println!(
"Answer to part 2 of day 4: {:#?}",
solution_part2(input.as_str())
);
}
}
+2
View File
@@ -0,0 +1,2 @@
#[allow(dead_code)]
mod day_4;