Compare commits
No commits in common. "15c19ef6c63c846267a603a0aa26854696db9391" and "b48c1e67efe308f99cf199f313658df93da963e7" have entirely different histories.
15c19ef6c6
...
b48c1e67ef
1
01/day01.py
Executable file → Normal file
1
01/day01.py
Executable file → Normal file
@ -1,4 +1,3 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
import re
|
import re
|
||||||
# part 1
|
# 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()]]))
|
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()]]))
|
||||||
|
@ -5,8 +5,6 @@ Today's language: **Rust**
|
|||||||
|
|
||||||
My very first rust program!
|
My very first rust program!
|
||||||
|
|
||||||
<!-- My very first rust program -->
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
rustc day02.rs
|
rustc day02.rs
|
||||||
./day02
|
./day02
|
||||||
|
@ -5,8 +5,6 @@ Today's language: **Rust**
|
|||||||
|
|
||||||
Super fast, it runs both tasks in 0,002s on my pc.
|
Super fast, it runs both tasks in 0,002s on my pc.
|
||||||
|
|
||||||
<!-- very efficient -->
|
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
rustc day05.rs
|
rustc day05.rs
|
||||||
./day05
|
./day05
|
||||||
|
13
06/README.md
13
06/README.md
@ -1,13 +0,0 @@
|
|||||||
# [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
83
06/day06.rs
@ -1,83 +0,0 @@
|
|||||||
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()
|
|
||||||
};
|
|
||||||
}
|
|
13
README.md
13
README.md
@ -1,13 +1,2 @@
|
|||||||
# Advent of code 2024 :christmas_tree:
|
# Advent of code 2023 :christmas_tree:
|
||||||
This is a repository for my solutions for the [advent of code 2023](https://adventofcode.com/2023).
|
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* |
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user