Skip to content
Merged
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
53 changes: 53 additions & 0 deletions Day-01/rust/duckulus/day01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::common::{InputType, read_lines};

const DAY: usize = 1;

pub fn part_one(input_type: InputType) {
let lines = read_lines(DAY, &input_type);
let mut dial = 50;
let mut password = 0;
for line in lines {
let direction = if &line.as_str()[..1] == "L" { -1 } else { 1 };
dial += direction * line.as_str()[1..].parse::<i32>().unwrap();
dial = dial % 100;
while dial < 0 {
dial = 100 + dial
}
if dial == 0 {
password += 1;
}
}
println!("{}", password);
}

pub fn part_two(input_type: InputType) {
let lines = read_lines(DAY, &input_type);
let mut dial = 50;
let mut password = 0;
for line in lines {
let direction = if &line.as_str()[..1] == "L" { -1 } else { 1 };
let change = line.as_str()[1..].parse::<i32>().unwrap();

if direction == -1 {
let t = dial;
dial -= change;
if dial < 0 && t == 0 {
password -= 1;
}
while dial < 0 {
dial = 100 + dial;
password += 1;
}
if dial == 0 {
password += 1;
}
} else {
dial += change;
if dial >= 100 {
password += dial / 100;
dial = dial % 100;
}
}
}
println!("{}", password);
}
62 changes: 62 additions & 0 deletions Day-02/rust/duckulus/day02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::common::{InputType, read_input};
use std::ops::{Range, RangeInclusive};

const DAY: usize = 2;

pub fn part_one(input_type: InputType) {
let ranges = read_ranges(input_type);

let mut sum = 0;
for range in ranges {
for i in range {
let s = i.to_string();
let str = s.as_str();
let len = str.len();

if len % 2 != 0 {
continue
}

if str[..len / 2] == str[len / 2..] {
sum += i;
}
}
}
println!("{}", sum);
}

pub fn part_two(input_type: InputType) {
let ranges = read_ranges(input_type);

let mut sum = 0;
for range in ranges {
for i in range {
let s = i.to_string();
let str = s.as_str();
let len = str.len();
for pref in 1..=len / 2 {
if len % pref != 0 {
continue;
}
let reps = len / pref;
if str[..len / reps].repeat(reps) == str {
sum += i;
break;
}
}
}
}
println!("{}", sum);
}

fn read_ranges(input_type: InputType) -> Vec<RangeInclusive<u64>> {
let ranges: Vec<_> = read_input(DAY, &input_type)
.as_str()
.split(",")
.map(|s| {
let split: Vec<_> = s.split("-").collect();
split[0].trim().parse::<u64>().unwrap()..=split[1].trim().parse::<u64>().unwrap()
})
.collect();
ranges
}
51 changes: 51 additions & 0 deletions Day-03/rust/duckulus/day03.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use crate::common::{InputType, read_input};

const DAY: usize = 3;

pub fn part_one(input_type: &InputType) {
let value: u64 = get_banks(input_type)
.into_iter()
.map(|bank| max_joltage(bank, 2, 0))
.sum();
println!("{}", value)
}

pub fn part_two(input_type: &InputType) {
let value: u64 = get_banks(&input_type)
.into_iter()
.map(|bank| max_joltage(bank, 12, 0))
.sum();
println!("{}", value)
}

fn max_joltage(bank: Vec<u8>, digits: usize, start: usize) -> u64 {
if digits == 0 {
return 0;
}
let mut max = bank[start];
let mut maxi = start;
for i in start + 1..=bank.len() - digits {
if bank[i] > max {
max = bank[i];
maxi = i;
}
}

let current_digit_value = (bank[maxi] as u64) * 10u64.pow((digits-1) as u32);
let remaining_digits_value = max_joltage(bank, digits - 1, maxi + 1);

current_digit_value + remaining_digits_value
}

fn get_banks(input_type: &InputType) -> Vec<Vec<u8>> {
read_input(DAY, input_type)
.as_str()
.trim()
.split("\n")
.map(|line| {
line.chars()
.map(|c| c.to_digit(10).unwrap() as u8)
.collect()
})
.collect()
}
86 changes: 86 additions & 0 deletions Day-04/rust/duckulus/day04.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use crate::common::{InputType, read_input};

const DAY: usize = 4;

pub fn part_one(input_type: &InputType) {
let mut grid = get_grid(input_type);
let value = remove_unaccessible(&mut grid);
println!("{}", value)
}

pub fn part_two(input_type: &InputType) {
let mut grid = get_grid(input_type);
let mut sum = 0;
let mut value = 0;
value = remove_unaccessible(&mut grid);
while value > 0 {
sum += value;
value = remove_unaccessible(&mut grid);
}
println!("{}", sum)
}

fn remove_unaccessible(grid: &mut Vec<Vec<Cell>>) -> usize {
let mut remove: Vec<(usize, usize)> = Vec::new();

let rows = grid.len() as i32;
for y in 0..rows {
let cols = grid[y as usize].len() as i32;
for x in 0..cols {
if grid[y as usize][x as usize] != Cell::Roll {
continue;
}
let mut neighbors = 0;
for dy in -1..=1 {
let ny = (y) + dy;
if (ny < 0 || ny >= rows) {
continue;
}
for dx in -1..=1 {
if (dx == 0 && dy == 0) {
continue;
}
let nx = (x) + dx;
if (nx < 0 || nx >= cols) {
continue;
}
if grid[ny as usize][nx as usize] == Cell::Roll {
neighbors += 1;
}
}
}
if neighbors < 4 {
remove.push((x as usize, y as usize));
}
}
}

for (x, y) in remove.iter() {
grid[*y][*x] = Cell::Empty;
}

remove.len()
}

#[derive(PartialEq)]
enum Cell {
Roll,
Empty,
}

fn to_cell(c: char) -> Cell {
match c {
'.' => Cell::Empty,
'@' => Cell::Roll,
_ => panic!("Found invalid cell: {}", c),
}
}

fn get_grid(input_type: &InputType) -> Vec<Vec<Cell>> {
read_input(DAY, input_type)
.as_str()
.trim()
.lines()
.map(|line| line.trim().chars().map(to_cell).collect())
.collect()
}
93 changes: 93 additions & 0 deletions Day-05/rust/duckulus/day05.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use crate::common::{InputType, read_input, Range};
use std::cmp::{max, min};
use std::collections::HashSet;

const DAY: usize = 5;

fn parse(input_type: &InputType) -> (Vec<Range>, Vec<i64>) {
let input = read_input(DAY, input_type);
let (ranges, ingredients) = input.split_once("\n\n").unwrap();

let ranges: Vec<_> = ranges
.lines()
.map(|line: &str| {
let (l, h) = line.split_once("-").unwrap();
Range {
l: l.parse::<i64>().unwrap(),
h: h.parse::<i64>().unwrap(),
}
})
.collect();

let ingredients: Vec<_> = ingredients
.lines()
.map(|line: &str| line.parse::<i64>().unwrap())
.collect();

(ranges, ingredients)
}

pub fn part_one(input_type: &InputType) {
let (ranges, ingredients) = parse(input_type);

let mut sum = 0;
for ingredient in ingredients {
for range in &ranges {
if range.l <= ingredient && range.h >= ingredient {
sum += 1;
break;
}
}
}
println!("{}", sum);
}

pub fn part_two(input_type: &InputType) {
let (ranges, _) = parse(input_type);

let mut range_list = Vec::new();

for range in ranges {
if range_list.is_empty() {
range_list.push(range);
continue;
}

let mut intersecting: Vec<Range> = range_list
.iter()
.filter(|r| r.intersection(&range).is_some())
.map(|r| r.clone())
.collect();
intersecting.push(range.clone());

let l = intersecting.iter().map(|r| r.l).min().unwrap();
let h = intersecting.iter().map(|r| r.h).max().unwrap();
let union = Range { l, h };

range_list.retain(|r| r.intersection(&range).is_none());
range_list.push(union);
}

let sum: i64 = range_list.iter().map(|r| r.len()).sum();
println!("{}", sum);
}

#[cfg(test)]
mod test {
use crate::day05::Range;

#[test]
fn test_range_intersect() {
let r1 = Range { l: 3, h: 5 };
let r2 = Range { l: 6, h: 8 };
let r3 = Range { l: 4, h: 9 };
let r4 = Range { l: 1, h: 4 };
let r5 = Range { l: 5, h: 6 };

assert_eq!(0, r1.intersect(&r2));
assert_eq!(2, r1.intersect(&r3));
assert_eq!(2, r1.intersect(&r4));
assert_eq!(1, r1.intersect(&r5));
assert_eq!(3, r1.intersect(&r1));
}
}
Loading