commit af727f4c7601e08959a1f954b3b508618be52bda Author: Hans Goor Date: Thu Apr 11 20:03:52 2024 +0200 Added day 1 in Rust and Uiua diff --git a/2019/inputs/day1.txt b/2019/inputs/day1.txt new file mode 100644 index 0000000..7103b54 --- /dev/null +++ b/2019/inputs/day1.txt @@ -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 + diff --git a/2019/rust/.gitignore b/2019/rust/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/2019/rust/.gitignore @@ -0,0 +1 @@ +/target diff --git a/2019/rust/Cargo.lock b/2019/rust/Cargo.lock new file mode 100644 index 0000000..8d2617a --- /dev/null +++ b/2019/rust/Cargo.lock @@ -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" diff --git a/2019/rust/Cargo.toml b/2019/rust/Cargo.toml new file mode 100644 index 0000000..b630693 --- /dev/null +++ b/2019/rust/Cargo.toml @@ -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] diff --git a/2019/rust/src/bin/day1.rs b/2019/rust/src/bin/day1.rs new file mode 100644 index 0000000..9c296db --- /dev/null +++ b/2019/rust/src/bin/day1.rs @@ -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) -> i32 { + lines + .iter() + .map(|l| str::parse::(l.as_str())) + .map(Result::unwrap) + .map(fuel) + .sum() +} + +fn part2(lines: &Vec) -> i32 { + lines + .iter() + .map(|l| str::parse::(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(()) +} diff --git a/2019/rust/src/lib.rs b/2019/rust/src/lib.rs new file mode 100644 index 0000000..cace8fa --- /dev/null +++ b/2019/rust/src/lib.rs @@ -0,0 +1,2 @@ +pub mod util; + diff --git a/2019/rust/src/util.rs b/2019/rust/src/util.rs new file mode 100644 index 0000000..240ce85 --- /dev/null +++ b/2019/rust/src/util.rs @@ -0,0 +1,13 @@ +use std::{env, fs, io}; + +pub fn read_file() -> Result, 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()) +} diff --git a/2019/uiua/day1.ua b/2019/uiua/day1.ua new file mode 100644 index 0000000..d2d3040 --- /dev/null +++ b/2019/uiua/day1.ua @@ -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" diff --git a/2019/uiua/util.ua b/2019/uiua/util.ua new file mode 100644 index 0000000..89e7b5b --- /dev/null +++ b/2019/uiua/util.ua @@ -0,0 +1 @@ +Lines ← ⊜□≠@\n.&fras