Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl Combat {
mod tests {
use super::*;

static TEST_GAME: &str = &r"Player 1:
static TEST_GAME: &str = r"Player 1:
9
2
6
Expand Down
8 changes: 4 additions & 4 deletions src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,15 +271,15 @@ mod tests {
let passport_str = r#"ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm"#;
let passport: UnvalidatedPassport = passport_str.parse().unwrap();
assert_eq!(true, passport.has_fields(&valid_passport_fields()))
assert!(passport.has_fields(&valid_passport_fields()))
}

#[test]
fn passport_missing_hgt() {
let passport_str = r#"iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929"#;
let passport: UnvalidatedPassport = passport_str.parse().unwrap();
assert_eq!(false, passport.has_fields(&valid_passport_fields()))
assert!(!passport.has_fields(&valid_passport_fields()))
}

#[test]
Expand All @@ -289,15 +289,15 @@ eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm"#;
let passport: UnvalidatedPassport = passport_str.parse().unwrap();
assert_eq!(true, passport.has_fields(&valid_passport_fields()))
assert!(passport.has_fields(&valid_passport_fields()))
}

#[test]
fn passport_missing_cid_byr() {
let passport_str = r#"hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in"#;
let passport: UnvalidatedPassport = passport_str.parse().unwrap();
assert_eq!(false, passport.has_fields(&valid_passport_fields()))
assert!(!passport.has_fields(&valid_passport_fields()))
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions src/cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl Space3 {
.sum()
}

#[must_use]
pub fn step(&self) -> Space3 {
let mut new = self.clone();
let _ = self
Expand Down Expand Up @@ -203,6 +204,7 @@ impl Space4 {
.sum()
}

#[must_use]
pub fn step(&self) -> Space4 {
let mut new = self.clone();
let _ = self
Expand Down
4 changes: 2 additions & 2 deletions src/dock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,12 @@ impl Mask {
mod tests {
use super::*;

static TEST_INPUT: &str = &r"mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
static TEST_INPUT: &str = r"mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
mem[8] = 11
mem[7] = 101
mem[8] = 0";

static TEST_INPUT_V2: &str = &r"mask = 000000000000000000000000000000X1001X
static TEST_INPUT_V2: &str = r"mask = 000000000000000000000000000000X1001X
mem[42] = 100
mask = 00000000000000000000000000000000X0XX
mem[26] = 1";
Expand Down
10 changes: 5 additions & 5 deletions src/expense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ mod tests {
fn test_pair_with_sum() {
let report = Report::new(&[1721, 979, 366, 299, 675, 1456]);
let nums = report.pair_with_sum(2020).unwrap();
assert_eq!(true, nums.contains(&1721));
assert_eq!(true, nums.contains(&299));
assert!(nums.contains(&1721));
assert!(nums.contains(&299));
}

#[test]
fn test_triple_with_sum() {
let report = Report::new(&[1721, 979, 366, 299, 675, 1456]);
let nums = report.triple_with_sum(2020).unwrap();
assert_eq!(true, nums.contains(&979));
assert_eq!(true, nums.contains(&366));
assert_eq!(true, nums.contains(&675));
assert!(nums.contains(&979));
assert!(nums.contains(&366));
assert!(nums.contains(&675));
}
}
2 changes: 2 additions & 0 deletions src/ferry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl std::str::FromStr for WaitingArea {
}

impl WaitingArea {
#[must_use]
pub fn stabilize_adjacent(&self) -> WaitingArea {
let mut old = self.clone();
let mut new = old.step_adjacent();
Expand All @@ -83,6 +84,7 @@ impl WaitingArea {
new
}

#[must_use]
pub fn stabilize_first_visible(&self) -> WaitingArea {
let mut old = self.clone();
let mut new = old.step_first_visible();
Expand Down
2 changes: 1 addition & 1 deletion src/food.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Menu {
mod tests {
use super::*;

static TEST_MENU: &str = &r"mxmxvkd kfcds sqjhc nhms (contains dairy, fish)
static TEST_MENU: &str = r"mxmxvkd kfcds sqjhc nhms (contains dairy, fish)
trh fvjkl sbzzf mxmxvkd (contains dairy)
sqjhc fvjkl (contains soy)
sqjhc mxmxvkd sbzzf (contains fish)";
Expand Down
5 changes: 2 additions & 3 deletions src/game_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl Program {
}
}

#[must_use]
pub fn modify_to_terminate(&self) -> Program {
for i in 0..self.codes.len() {
let mut program = self.clone();
Expand Down Expand Up @@ -88,9 +89,7 @@ impl ExecutionContext {
if self.executed_instructions.contains(&self.program_counter) {
return ExitReason::Loop;
}
if self.program_counter < 0 {
panic!("program counter goes negative!");
}
assert!(self.program_counter >= 0, "program counter goes negative!");
if self.program_counter >= program.codes.len() as i32 {
return ExitReason::Terminate;
}
Expand Down
8 changes: 2 additions & 6 deletions src/jigsaw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ impl std::str::FromStr for Tiles {
Ok(tile)
})
.collect::<Result<Vec<_>>>()?;
let tiles = tiles
.iter()
.map(|t| t.rotations())
.flatten()
.collect::<Vec<_>>();
let tiles = tiles.iter().flat_map(|t| t.rotations()).collect::<Vec<_>>();
Ok(Tiles(tiles))
}
}
Expand Down Expand Up @@ -367,7 +363,7 @@ fn data_join(data: Vec<Vec<i8>>, next_tile: Vec<Vec<i8>>) -> Vec<Vec<i8>> {
mod tests {
use super::*;

static TEST_INPUT: &str = &r"Tile 2311:
static TEST_INPUT: &str = r"Tile 2311:
..##.#..#.
##..#.....
#...##..#.
Expand Down
9 changes: 3 additions & 6 deletions src/luggage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub struct RulesGraph(HashMap<Color, RulesGraphNode>);

#[derive(Debug)]
struct RulesGraphNode {
color: Color,
held_by: HashSet<Color>,
holds: Vec<(usize, Color)>,
}
Expand Down Expand Up @@ -72,14 +71,12 @@ impl RulesGraph {
fn insert(&mut self, c: Color, contains: Vec<(usize, Color)>) {
for (_quantity, color) in &contains {
let node = self.0.entry(color.clone()).or_insert(RulesGraphNode {
color: color.clone(),
held_by: HashSet::new(),
holds: Vec::new(),
});
node.held_by.insert(c.clone());
}
let node = self.0.entry(c.clone()).or_insert(RulesGraphNode {
color: c,
let node = self.0.entry(c).or_insert(RulesGraphNode {
held_by: HashSet::new(),
holds: Vec::new(),
});
Expand All @@ -95,7 +92,7 @@ impl std::str::FromStr for RulesGraph {
let _ = s
.trim()
.lines()
.map(|line| parse_bag_rule(line))
.map(parse_bag_rule)
.map(|pair| {
let pair = pair?;
rules_graph.insert(pair.0, pair.1);
Expand Down Expand Up @@ -139,7 +136,7 @@ fn parse_bag_rule(s: &str) -> Result<(Color, Vec<(usize, Color)>)> {
let contained = contained
.trim_end_matches('.')
.split(',')
.map(|s| parse_bag_str(s))
.map(parse_bag_str)
.collect::<Result<Vec<_>>>()?
.into_iter()
.flatten()
Expand Down
12 changes: 6 additions & 6 deletions src/password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,41 +79,41 @@ mod tests {
fn test_password_frequency_one_a_passes() {
let line = "1-3 a: abcde";
let (policy, password) = parse_password_policy_line(line).unwrap();
assert_eq!(true, meets_policy_frequency(&policy, &password));
assert!(meets_policy_frequency(&policy, &password));
}

#[test]
fn test_password_frequency_no_b_fails() {
let line = "1-3 b: cdefg";
let (policy, password) = parse_password_policy_line(line).unwrap();
assert_eq!(false, meets_policy_frequency(&policy, &password));
assert!(!meets_policy_frequency(&policy, &password));
}

#[test]
fn test_password_frequency_many_c_passes() {
let line = "2-9 c: ccccccccc";
let (policy, password) = parse_password_policy_line(line).unwrap();
assert_eq!(true, meets_policy_frequency(&policy, &password));
assert!(meets_policy_frequency(&policy, &password));
}

#[test]
fn test_password_posiiton_one_a_passes() {
let line = "1-3 a: abcde";
let (policy, password) = parse_password_policy_line(line).unwrap();
assert_eq!(true, meets_policy_position(&policy, &password));
assert!(meets_policy_position(&policy, &password));
}

#[test]
fn test_password_posiiton_no_b_fails() {
let line = "1-3 b: cdefg";
let (policy, password) = parse_password_policy_line(line).unwrap();
assert_eq!(false, meets_policy_position(&policy, &password));
assert!(!meets_policy_position(&policy, &password));
}

#[test]
fn test_password_posiiton_many_c_fails() {
let line = "2-9 c: ccccccccc";
let (policy, password) = parse_password_policy_line(line).unwrap();
assert_eq!(false, meets_policy_position(&policy, &password));
assert!(!meets_policy_position(&policy, &password));
}
}
16 changes: 8 additions & 8 deletions src/sea_monster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,14 @@ impl RuleMap {
mod tests {
use super::*;

static TEST_RULES: &str = &r#"0: 4 1 5
static TEST_RULES: &str = r#"0: 4 1 5
1: 2 3 | 3 2
2: 4 4 | 5 5
3: 4 5 | 5 4
4: "a"
5: "b""#;

static TEST_RULES_2: &str = &r#"42: 9 14 | 10 1
static TEST_RULES_2: &str = r#"42: 9 14 | 10 1
9: 14 27 | 1 26
10: 23 14 | 28 1
1: "a"
Expand Down Expand Up @@ -199,17 +199,17 @@ mod tests {
#[test]
fn test_parse_rule_map() {
let rule_map = TEST_RULES.parse::<RuleMap>();
assert_eq!(true, rule_map.is_ok());
assert!(rule_map.is_ok());
}

#[test]
fn test_rule_map_matches() {
let rule_map = TEST_RULES.parse::<RuleMap>().unwrap();
assert_eq!(true, rule_map.matches("ababbb"));
assert_eq!(false, rule_map.matches("bababa"));
assert_eq!(true, rule_map.matches("abbbab"));
assert_eq!(false, rule_map.matches("aaabbb"));
assert_eq!(false, rule_map.matches("aaaabbb"));
assert!(rule_map.matches("ababbb"));
assert!(!rule_map.matches("bababa"));
assert!(rule_map.matches("abbbab"));
assert!(!rule_map.matches("aaabbb"));
assert!(!rule_map.matches("aaaabbb"));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Map {
mod tests {
use super::*;

static TEST_INPUT: &str = &r"sesenwnenenewseeswwswswwnenewsewsw
static TEST_INPUT: &str = r"sesenwnenenewseeswwswswwnenewsewsw
neeenesenwnwwswnenewnwwsewnenwseswesw
seswneswswsenwwnwse
nwnwneseeswswnenewneswwnewseswneseene
Expand Down
20 changes: 10 additions & 10 deletions src/train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ pub fn resolve_field_map(mut m: HashMap<String, HashSet<usize>>) -> HashMap<Stri
mod tests {
use super::*;

static TEST_RULES: &str = &r"class: 1-3 or 5-7
static TEST_RULES: &str = r"class: 1-3 or 5-7
row: 6-11 or 33-44
seat: 13-40 or 45-50";

static TEST_RULES_2: &str = &r"class: 0-1 or 4-19
static TEST_RULES_2: &str = r"class: 0-1 or 4-19
row: 0-5 or 8-19
seat: 0-13 or 16-19";

Expand All @@ -144,11 +144,11 @@ seat: 0-13 or 16-19";
.lines()
.map(|line| line.parse::<Rule>().unwrap())
.collect::<Vec<_>>();
assert_eq!(true, "7,1,14".parse::<Ticket>().unwrap().is_valid(&rules));
assert_eq!(true, "7,3,47".parse::<Ticket>().unwrap().is_valid(&rules));
assert_eq!(false, "40,4,50".parse::<Ticket>().unwrap().is_valid(&rules));
assert_eq!(false, "55,2,20".parse::<Ticket>().unwrap().is_valid(&rules));
assert_eq!(false, "38,6,12".parse::<Ticket>().unwrap().is_valid(&rules));
assert!("7,1,14".parse::<Ticket>().unwrap().is_valid(&rules));
assert!("7,3,47".parse::<Ticket>().unwrap().is_valid(&rules));
assert!(!"40,4,50".parse::<Ticket>().unwrap().is_valid(&rules));
assert!(!"55,2,20".parse::<Ticket>().unwrap().is_valid(&rules));
assert!(!"38,6,12".parse::<Ticket>().unwrap().is_valid(&rules));
}

#[test]
Expand Down Expand Up @@ -176,8 +176,8 @@ seat: 0-13 or 16-19";
.into_iter()
.map(|str| str.parse::<Ticket>().unwrap())
.collect::<Vec<_>>();
assert_eq!(true, rules[1].possible_fields(&tickets).contains(&0));
assert_eq!(true, rules[0].possible_fields(&tickets).contains(&1));
assert_eq!(true, rules[2].possible_fields(&tickets).contains(&2));
assert!(rules[1].possible_fields(&tickets).contains(&0));
assert!(rules[0].possible_fields(&tickets).contains(&1));
assert!(rules[2].possible_fields(&tickets).contains(&2));
}
}
6 changes: 3 additions & 3 deletions src/xmas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ mod tests {

#[test]
fn test_find_sum() {
assert_eq!(true, find_sum(&TEST_NUMS[0..5], 40).is_some());
assert_eq!(true, find_sum(&TEST_NUMS[1..6], 62).is_some());
assert_eq!(true, find_sum(&TEST_NUMS[9..14], 127).is_none());
assert!(find_sum(&TEST_NUMS[0..5], 40).is_some());
assert!(find_sum(&TEST_NUMS[1..6], 62).is_some());
assert!(find_sum(&TEST_NUMS[9..14], 127).is_none());
}

#[test]
Expand Down