From b563dcbb180762b26dab924d3f23bb0376fee2cb Mon Sep 17 00:00:00 2001 From: dece Date: Sun, 7 Nov 2021 15:51:58 +0100 Subject: [PATCH] 2015 day 5 --- 2015/src/bin/day5.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++ prepare.py | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 2015/src/bin/day5.rs diff --git a/2015/src/bin/day5.rs b/2015/src/bin/day5.rs new file mode 100644 index 0000000..2eac906 --- /dev/null +++ b/2015/src/bin/day5.rs @@ -0,0 +1,59 @@ +use aoc::input; + +fn main() { + let lines = input::read_lines(); + + // Part 1 + let mut goods = 0; + let bad_pairs = vec!("ab", "cd", "pq", "xy"); + for line in &lines { + let mut has_bad_pair = false; + for p in &bad_pairs { + if line.contains(p) { has_bad_pair = true; break } + } + if has_bad_pair { continue } + let mut num_vowels = 0; + let mut cc = ' '; + let mut has_doubled = false; + for c in line.chars() { + if "aeiou".contains(c) { num_vowels += 1 } + if c == cc { has_doubled = true } + cc = c; + } + if num_vowels < 3 { continue } + if !has_doubled { continue } + goods += 1; + } + println!("Good strings: {}", goods); + + // Part 2 + goods = 0; + for line in &lines { + let mut cond1 = false; + let chars: Vec = line.chars().collect(); + for i in 0..(chars.len() - 2) { + let c1 = chars[i]; + let c2 = chars[i + 1]; + for j in (i + 2)..(chars.len() - 1) { + if chars[j] == c1 && chars[j + 1] == c2 { + cond1 = true; + break + } + } + if cond1 { break } + } + if !cond1 { continue } + + let mut cond2 = false; + let mut cm2 = ' '; + let mut cm1 = ' '; + for c in line.chars() { + if c == cm2 { cond2 = true } + cm2 = cm1; + cm1 = c; + } + if !cond2 { continue } + goods += 1; + } + println!("Best strings: {}", goods); +} diff --git a/prepare.py b/prepare.py index 4f7a5b0..43abfe9 100644 --- a/prepare.py +++ b/prepare.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python3 import argparse import os import shutil @@ -25,7 +26,6 @@ def main(): input_text = fetch(input_url) create_files(input_text, args.day, args.year, args.lang) webbrowser.open_new_tab(day_url) - webbrowser.open_new_tab(input_url) def fetch(input_url): response = requests.get(input_url, cookies={"session": SESSION_ID})