Compare commits

..

5 Commits

Author SHA1 Message Date
15c19ef6c6 add table 2023-12-06 11:42:14 +01:00
d1c21a4c0a day06: rust 2023-12-06 11:34:50 +01:00
3640329eec day05: rust 2023-12-06 11:32:08 +01:00
0090f6c379 day02: rust 2023-12-06 11:32:01 +01:00
e656a6f8b3 day01: python 2023-12-06 11:31:51 +01:00
6 changed files with 113 additions and 1 deletions

1
01/day01.py Normal file → Executable file
View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3
import re
# part 1
with open("input.txt", "r") as file: print("Part 1:", sum([int(match[0]+match[-1]) for match in [re.findall(r"\d", line) for line in file.readlines()]]))

View File

@ -5,6 +5,8 @@ Today's language: **Rust**
My very first rust program!
<!-- My very first rust program -->
```shell
rustc day02.rs
./day02

View File

@ -5,6 +5,8 @@ Today's language: **Rust**
Super fast, it runs both tasks in 0,002s on my pc.
<!-- very efficient -->
```shell
rustc day05.rs
./day05

13
06/README.md Normal file
View File

@ -0,0 +1,13 @@
# [Day 6](https://adventofcode.com/2023/day/6)
:gift::gift::gift::gift::gift::gift:
Today's language: **Rust**
*Midnight formula go brrrrr*
<!-- quadratic formula go *brrrr* -->
```shell
rustc day06.rs
./day06
```

83
06/day06.rs Normal file
View File

@ -0,0 +1,83 @@
use std::io::{self, BufRead};
use std::fmt::Debug;
fn main() {
task1();
task2();
}
// x = (T - t) * t = T * t - t^2
// 0 = t^2 - T * t + x
fn task1() {
let input = "input.txt";
let mut lines = read_lines(&input);
let times = split_line(&lines.next().unwrap().expect("No line found"));
let distances = split_line(&lines.next().unwrap().expect("No line found"));
assert_eq!(times.len(), distances.len(), "uneqal array lengths");
let mut total_ways_mult: i32 = 1;
for i in 0..times.len() {
let mut n_ways: i32 = 0;
for t in 1..times[i] {
let x: i32 = times[i] * t - t.pow(2);
// println!("T={}, t={}, x={}", times[i], t, x);
if x > distances[i] {
n_ways += 1;
}
}
total_ways_mult *= n_ways;
}
println!("Product of number of ways: {}", total_ways_mult);
}
fn task2() {
let input = "input.txt";
let mut lines = read_lines(&input);
let time = parse2(&lines.next().unwrap().expect("No line found"));
let distance = parse2(&lines.next().unwrap().expect("No line found"));
// println!("time={}, distance={}", time, distance);
let t1 = solve_squared(1., -time, distance, 1.);
let t2 = solve_squared(1., -time, distance, -1.);
// println!("t1={}, t2={}", t1, t2);
let lower = t1.min(t2) as u64;
let upper = t1.max(t2) as u64;
// println!("lower={}, upper={}", lower, upper);
println!("Number of possible ways: {}", upper-lower);
// validation
// let distance = |t: u64| (time as u64 - t) * t;
// println!("distance(t1)={}, distance(t2)={}", distance(lower), distance(upper));
}
fn solve_squared(a: f64, b: f64, c: f64, sign: f64) -> f64 {
return (-b + sign * (b * b - 4. * a * c).sqrt() as f64) / (2. * a);
}
fn parse2(line: &str) -> f64 {
let l = rm_char(&line, ' ');
// println!("{}", l);
return l[l.find(':').unwrap()+1..].parse::<f64>().unwrap();
}
fn rm_char(original: &str, c: char) -> String {
return original.chars().filter(|x| x != &c).collect();
}
fn split_line(line: &str) -> Vec<i32> {
let colon = line.find(':').unwrap_or(0);
return split_into_numbers::<i32>(&line[colon+2..]).collect();
}
fn split_into_numbers<T: std::str::FromStr>(x: &str) -> impl Iterator<Item = T> + '_ where <T as std::str::FromStr>::Err: Debug {
return x.split(' ').filter(|&n| {n != "" && n != " "}).map(|n| n.parse::<T>().unwrap());
}
fn read_lines<P>(filename: P) -> io::Lines<io::BufReader<std::fs::File>>
where P: AsRef<std::path::Path>, {
return match std::fs::File::open(filename) {
Err(why) => panic!("Could not open file. {}", why),
Ok(file) => std::io::BufReader::new(file).lines()
};
}

View File

@ -1,2 +1,13 @@
# Advent of code 2023 :christmas_tree:
# Advent of code 2024 :christmas_tree:
This is a repository for my solutions for the [advent of code 2023](https://adventofcode.com/2023).
<!-- table begin -->
| Day | Language | Execuction time |
| --- | --- | --- |
| 01 | Python | 0m0,015s | one-liners |
| 02 | Rust | 0m0,001s | My very first rust program |
| 03 | Rust | 0m0,001s | |
| 04 | Rust | 0m0,001s | |
| 05 | Rust | 0m0,001s | very efficient |
| 06 | Rust | 0m0,001s | quadratic formula go *brrrr* |