Added day 1 in Rust and Uiua

This commit is contained in:
Hans Goor 2024-04-11 20:03:52 +02:00
commit af727f4c76
Signed by: eyedevelop
SSH key fingerprint: SHA256:Td89veptDEwCV8J3fjqnknNk7SbwzedYhauyC2nFBYg
9 changed files with 187 additions and 0 deletions

101
2019/inputs/day1.txt Normal file
View file

@ -0,0 +1,101 @@
119031
111204
75773
95544
69987
147194
74024
100438
86116
89945
144856
123315
64102
55491
95959
149174
66810
134674
88921
124270
60833
125667
84885
57688
89059
126854
93633
103791
104295
137762
101216
138060
103271
95822
102000
66821
126916
104629
87710
79852
87852
149281
92055
50969
62626
112069
68560
66131
139961
89456
100536
51338
51075
112858
134878
137702
60091
111576
70517
131524
56162
148346
62696
110191
141106
54858
66248
86402
132012
96367
95319
133879
115031
77875
129470
146650
70048
147454
123076
74563
94228
59920
147986
92398
51890
92686
110452
85205
67482
87931
69535
73948
114576
65958
53081
132809
76088
74553
121820
121214

1
2019/rust/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7
2019/rust/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc2019"
version = "0.1.0"

11
2019/rust/Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "aoc2019"
version = "0.1.0"
edition = "2021"
[lib]
path = "src/lib.rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

41
2019/rust/src/bin/day1.rs Normal file
View file

@ -0,0 +1,41 @@
use aoc2019::util::read_file;
use std::io;
fn fuel(mass: i32) -> i32 {
mass / 3 - 2
}
fn total_fuel(mass: i32) -> i32 {
let f = fuel(mass);
if f <= 0 {
return 0;
}
f + total_fuel(f)
}
fn part1(lines: &Vec<String>) -> i32 {
lines
.iter()
.map(|l| str::parse::<i32>(l.as_str()))
.map(Result::unwrap)
.map(fuel)
.sum()
}
fn part2(lines: &Vec<String>) -> i32 {
lines
.iter()
.map(|l| str::parse::<i32>(l.as_str()))
.map(Result::unwrap)
.map(total_fuel)
.sum()
}
fn main() -> io::Result<()> {
let file = read_file()?;
println!("{}", part1(&file));
println!("{}", part2(&file));
Ok(())
}

2
2019/rust/src/lib.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod util;

13
2019/rust/src/util.rs Normal file
View file

@ -0,0 +1,13 @@
use std::{env, fs, io};
pub fn read_file() -> Result<Vec<String>, std::io::Error> {
let first_arg = env::args()
.nth(1)
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "No file given to read!"))?;
Ok(fs::read_to_string(first_arg)?
.lines()
.map(String::from)
.filter(|s| !s.is_empty())
.collect())
}

10
2019/uiua/day1.ua Normal file
View file

@ -0,0 +1,10 @@
~ "util.ua" ~ Lines
# Util
Mass ← -2⌊÷3
# Part 1
/+ Mass ⋕ Lines "../inputs/day1.txt"
# Part 2
/+∵(/+↘¯1◌⍢(⊃Mass⊂) (>0) :[]) ⋕ Lines "../inputs/day1.txt"

1
2019/uiua/util.ua Normal file
View file

@ -0,0 +1 @@
Lines ← ⊜□≠@\n.&fras