From e3ecc0429f1ce642d590ec62b781cec2943f37f9 Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Thu, 5 Dec 2019 11:35:38 +0100 Subject: [PATCH] initial commit --- .gitignore | 3 ++ .travis.yml | 26 +++++++++++ python/1/day1.py | 41 +++++++++++++++++ python/1/input.txt | 100 ++++++++++++++++++++++++++++++++++++++++++ python/1/test_day1.py | 21 +++++++++ python/2/day2.py | 55 +++++++++++++++++++++++ python/2/input.txt | 1 + python/2/test_day2.py | 20 +++++++++ rust/.gitignore | 2 + rust/Cargo.lock | 6 +++ rust/Cargo.toml | 9 ++++ rust/src/day1.rs | 58 ++++++++++++++++++++++++ rust/src/main.rs | 9 ++++ 13 files changed, 351 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 python/1/day1.py create mode 100644 python/1/input.txt create mode 100644 python/1/test_day1.py create mode 100644 python/2/day2.py create mode 100644 python/2/input.txt create mode 100644 python/2/test_day2.py create mode 100644 rust/.gitignore create mode 100644 rust/Cargo.lock create mode 100644 rust/Cargo.toml create mode 100644 rust/src/day1.rs create mode 100644 rust/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b7422a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.mypy_cache/ +.idea/ +__pycache__/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6d18553 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,26 @@ +jobs: + allow_failures: + - rust: nightly + include: + - language: python + python: 3.8 + before_script: + - cd python + script: + - pytest + + - language: rust + rust: stable + cache: cargo + before_script: + - cd rust + - language: rust + rust: beta + cache: cargo + before_script: + - cd rust + - language: rust + rust: nightly + cache: cargo + before_script: + - cd rust diff --git a/python/1/day1.py b/python/1/day1.py new file mode 100644 index 0000000..ed2db71 --- /dev/null +++ b/python/1/day1.py @@ -0,0 +1,41 @@ +def calculate_fuel(mass: int) -> int: + return mass // 3 - 2 + + +def part1() -> int: + total = 0 + + with open("1/input.txt") as f: + for line in f: + m = int(line) + fuel = calculate_fuel(m) + total += fuel + return total + + +def advanced_fuel(mass: int) -> int: + total = 0 + while True: + fuel = calculate_fuel(mass) + if fuel > 0: + total += fuel + mass = fuel + else: + break + return total + + +def part2() -> int: + total = 0 + + with open("1/input.txt") as f: + for line in f: + m = int(line) + fuel = advanced_fuel(m) + total += fuel + return total + + +if __name__ == '__main__': + print(part1()) + print(part2()) diff --git a/python/1/input.txt b/python/1/input.txt new file mode 100644 index 0000000..b441902 --- /dev/null +++ b/python/1/input.txt @@ -0,0 +1,100 @@ +103910 +133712 +82560 +91679 +98354 +89007 +93288 +132363 +91373 +83666 +55958 +90387 +100869 +98127 +120197 +86931 +60370 +143999 +71541 +115662 +51287 +81624 +58307 +60408 +141664 +89781 +127772 +132353 +101220 +104001 +140488 +58072 +75764 +120003 +82386 +77603 +130604 +86672 +120987 +80334 +67674 +52918 +98041 +102541 +97612 +50436 +129998 +84854 +101867 +82039 +108966 +80708 +54588 +86854 +89607 +71869 +126093 +89460 +86558 +77651 +53295 +132188 +137266 +97370 +114620 +86691 +147199 +147299 +72616 +142654 +88610 +104030 +64256 +54867 +76532 +145081 +102335 +72987 +72684 +148155 +59739 +85954 +141001 +125171 +107764 +141622 +89536 +92435 +69038 +84518 +119700 +119801 +81677 +125317 +72683 +128905 +93666 +75633 +117361 +82295 diff --git a/python/1/test_day1.py b/python/1/test_day1.py new file mode 100644 index 0000000..8524930 --- /dev/null +++ b/python/1/test_day1.py @@ -0,0 +1,21 @@ +from day1 import calculate_fuel, part1, advanced_fuel, part2 + + +def test_calculate_fuel(): + assert calculate_fuel(12) == 2 + assert calculate_fuel(14) == 2 + assert calculate_fuel(1969) == 654 + assert calculate_fuel(100756) == 33583 + + +def test_part1(): + assert part1() == 3226488 + + +def test_advanced_fuel(): + assert advanced_fuel(14) == 2 + assert advanced_fuel(1969) == 966 + assert advanced_fuel(100756) == 50346 + +def test_part2(): + assert part2()==4836845 diff --git a/python/2/day2.py b/python/2/day2.py new file mode 100644 index 0000000..b93fde3 --- /dev/null +++ b/python/2/day2.py @@ -0,0 +1,55 @@ +from typing import List + +intcode = List[int] + + +def opcode_parse(text: str) -> intcode: + return list(map(int, text.split(","))) + + +def run_intcode(codelist: intcode) -> intcode: + p = 0 # pointer + while True: + code = codelist[p] + if code == 99: + break + from1 = codelist[p + 1] + from2 = codelist[p + 2] + to = codelist[p + 3] + if code == 1: + codelist[to] = codelist[from1] + codelist[from2] + elif code == 2: + codelist[to] = codelist[from1] * codelist[from2] + else: + raise ValueError(f"invalid intcode: {code}") + p += 4 + return codelist + + +def part1() -> int: + with open("2/input.txt") as f: + cl = opcode_parse(f.read()) + cl[1] = 12 + cl[2] = 2 + cl = run_intcode(cl) + + return cl[0] + + +def part2() -> int: + with open("2/input.txt") as f: + initial_cl = opcode_parse(f.read()) + + for noun in range(99): + for verb in range(99): + cl = initial_cl[:] # make a copy + cl[1] = noun + cl[2] = verb + cl = run_intcode(cl) + if cl[0] == 19690720: + return 100 * noun + verb + + +if __name__ == '__main__': + print(part1()) + print(part2()) diff --git a/python/2/input.txt b/python/2/input.txt new file mode 100644 index 0000000..aa94710 --- /dev/null +++ b/python/2/input.txt @@ -0,0 +1 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,5,19,23,2,10,23,27,1,27,5,31,2,9,31,35,1,35,5,39,2,6,39,43,1,43,5,47,2,47,10,51,2,51,6,55,1,5,55,59,2,10,59,63,1,63,6,67,2,67,6,71,1,71,5,75,1,13,75,79,1,6,79,83,2,83,13,87,1,87,6,91,1,10,91,95,1,95,9,99,2,99,13,103,1,103,6,107,2,107,6,111,1,111,2,115,1,115,13,0,99,2,0,14,0 diff --git a/python/2/test_day2.py b/python/2/test_day2.py new file mode 100644 index 0000000..973a29c --- /dev/null +++ b/python/2/test_day2.py @@ -0,0 +1,20 @@ +from day2 import opcode_parse, run_intcode, part1, part2 + + +def test_opcode_parse(): + assert opcode_parse("2,2,3,45,5") == [2, 2, 3, 45, 5] + + +def test_run_intcode(): + assert run_intcode([1, 0, 0, 0, 99]) == [2, 0, 0, 0, 99] + assert run_intcode([2, 3, 0, 3, 99]) == [2, 3, 0, 6, 99] + assert run_intcode([2, 4, 4, 5, 99, 0]) == [2, 4, 4, 5, 99, 9801] + assert run_intcode([1, 1, 1, 4, 99, 5, 6, 0, 99]) == [30, 1, 1, 4, 2, 5, 6, 0, 99] + + +def test_part1(): + assert part1() == 4714701 + + +def test_part2(): + assert part2() == 5121 diff --git a/rust/.gitignore b/rust/.gitignore new file mode 100644 index 0000000..eccd7b4 --- /dev/null +++ b/rust/.gitignore @@ -0,0 +1,2 @@ +/target/ +**/*.rs.bk diff --git a/rust/Cargo.lock b/rust/Cargo.lock new file mode 100644 index 0000000..29558f3 --- /dev/null +++ b/rust/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adventofcode" +version = "0.1.0" + diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..99b5d87 --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "adventofcode" +version = "0.1.0" +authors = ["Lukas Winkler "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/src/day1.rs b/rust/src/day1.rs new file mode 100644 index 0000000..9ac6cca --- /dev/null +++ b/rust/src/day1.rs @@ -0,0 +1,58 @@ +use std::fs; + +fn calculate_fuel(mass: i32) -> i32 { + return mass / 3 - 2; +} + +fn advanced_fuel(mass: i32) -> i32 { + let mut mass = mass; + let mut total = 0; + let mut fuel; + loop { + fuel = calculate_fuel(mass); + if fuel > 0 { + total += fuel; + mass = fuel; + } else { + break; + } + } + return total; +} + +pub fn part1() -> i32 { + let data = fs::read_to_string("../python/1/input.txt").expect("Unable to read file"); + let ints = data + .lines() + .map(|line| calculate_fuel(line.parse().expect("error when parsing line as integer"))) + .sum(); + return ints; +} + +pub fn part2() -> i32 { + return 1; +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_calculate_fuel() { + assert_eq!(calculate_fuel(12), 2); + assert_eq!(calculate_fuel(14), 2); + assert_eq!(calculate_fuel(1969), 654); + assert_eq!(calculate_fuel(100756), 33583); + } + + #[test] + fn test_advanced_fuel() { + assert_eq!(advanced_fuel(14), 2); + assert_eq!(advanced_fuel(1969), 966); + assert_eq!(advanced_fuel(100756), 50346); + } + + #[test] + fn test_part1() { + assert_eq!(part1(), 3226488) + } +} diff --git a/rust/src/main.rs b/rust/src/main.rs new file mode 100644 index 0000000..06c6bf1 --- /dev/null +++ b/rust/src/main.rs @@ -0,0 +1,9 @@ +mod day1; + +fn main() { + let res1 = day1::part1(); + let res2 = day1::part2(); + + println!("part 1: {}", res1); + println!("part 1: {}", res2); +}