From 52e88e6cab57805c9ed72acb5facb5f2b3f74340 Mon Sep 17 00:00:00 2001 From: Petra Date: Wed, 29 Nov 2023 21:55:55 +1300 Subject: [PATCH] 2019 Day 1 + part of day 2 --- 2019/Makefile | 2 +- 2019/data/day1a.txt | 101 ++++++++++++++++++++++++++++++++++++++++++++ 2019/data/day2a.txt | 2 + 2019/src/day1.f90 | 65 ++++++++++++++++++++++++++++ 2019/src/day2.f90 | 44 +++++++++++++++++++ 5 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 2019/data/day1a.txt create mode 100644 2019/data/day2a.txt create mode 100644 2019/src/day1.f90 create mode 100644 2019/src/day2.f90 diff --git a/2019/Makefile b/2019/Makefile index bde9b35..5897ce0 100644 --- a/2019/Makefile +++ b/2019/Makefile @@ -2,7 +2,7 @@ FC:=gfortran FFLAGS:=-Wall BIN:=./bin SRC:=./src -BINS:=./bin/test.bin +BINS:=./bin/test.bin ./bin/day1.bin ./bin/day2.bin all: aoc19 diff --git a/2019/data/day1a.txt b/2019/data/day1a.txt new file mode 100644 index 0000000..45fdd7b --- /dev/null +++ b/2019/data/day1a.txt @@ -0,0 +1,101 @@ +134492 +88713 +84405 +148193 +95951 +63545 +137840 +65558 +124836 +95431 +77622 +91864 +108677 +116871 +119496 +97172 +86115 +105704 +68613 +77114 +114013 +52766 +57048 +80814 +73888 +58253 +135934 +97409 +112439 +98262 +116047 +57456 +124261 +83006 +101495 +133449 +111372 +56146 +87818 +92209 +149259 +124559 +141838 +147988 +65703 +125566 +59650 +139564 +92430 +126307 +120406 +147383 +84362 +51529 +146366 +131840 +53270 +71886 +118767 +104311 +126181 +76964 +129430 +95489 +91098 +54133 +110057 +107276 +118226 +96104 +135382 +85152 +61697 +143417 +148879 +126846 +130205 +111170 +86687 +113729 +123330 +56976 +148470 +66028 +129715 +75686 +74964 +148258 +72669 +88809 +78173 +92699 +124806 +67217 +139066 +136002 +135730 +145708 +142054 +135772 + diff --git a/2019/data/day2a.txt b/2019/data/day2a.txt new file mode 100644 index 0000000..36ed620 --- /dev/null +++ b/2019/data/day2a.txt @@ -0,0 +1,2 @@ +1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,9,19,23,2,23,13,27,1,27,9,31,2,31,6,35,1,5,35,39,1,10,39,43,2,43,6,47,1,10,47,51,2,6,51,55,1,5,55,59,1,59,9,63,1,13,63,67,2,6,67,71,1,5,71,75,2,6,75,79,2,79,6,83,1,13,83,87,1,9,87,91,1,9,91,95,1,5,95,99,1,5,99,103,2,13,103,107,1,6,107,111,1,9,111,115,2,6,115,119,1,13,119,123,1,123,6,127,1,127,5,131,2,10,131,135,2,135,10,139,1,13,139,143,1,10,143,147,1,2,147,151,1,6,151,0,99,2,14,0,0 + diff --git a/2019/src/day1.f90 b/2019/src/day1.f90 new file mode 100644 index 0000000..c69f89a --- /dev/null +++ b/2019/src/day1.f90 @@ -0,0 +1,65 @@ +program day1 + implicit none + integer, parameter :: max_masses = 10000 + integer :: n_arguments + integer :: f_sum = 0 + integer :: f_full_sum = 0 + integer :: this_fuel, this_full_fuel, this_mass + integer :: i, istat + character(200) :: fname + n_arguments = command_argument_count() + if (n_arguments .eq. 1) then + call get_command_argument(1, fname) + print *, "File: ", fname + else + print *, "Wrong number of arguments: ", n_arguments + stop + end if + open(10, file=fname) + do i=1,max_masses + read (10, *, iostat = istat) this_mass + if (is_iostat_end(istat)) then + exit + end if + this_fuel = fuelcalc(this_mass) + this_full_fuel = fullfuelcalc(this_mass) + f_sum = f_sum + this_fuel + f_full_sum = f_full_sum + this_full_fuel + print *, i, this_mass, this_fuel, f_sum, this_full_fuel, f_full_sum + end do + close(10) + print * + print *, f_sum, f_full_sum + + contains + function fuelcalc(mass) + implicit none + integer, intent(in) :: mass + integer :: fuelcalc + fuelcalc = mass / 3 - 2 + end function fuelcalc + + function fullfuelcalc(mass) + implicit none + integer, parameter :: max_iterations = 10000 + integer, intent(in) :: mass + integer :: fullfuelcalc + integer :: ftemp + integer :: i + ftemp = mass + fullfuelcalc = 0 + do i=1,max_iterations + ftemp = fuelcalc(ftemp) + if (ftemp .le. 0) then + exit + else + fullfuelcalc = fullfuelcalc + ftemp + end if + end do + if (ftemp .gt. 0) then + print *, "Too many iterations for mass", mass + stop + end if + end function fullfuelcalc +end program day1 + diff --git a/2019/src/day2.f90 b/2019/src/day2.f90 new file mode 100644 index 0000000..00a1585 --- /dev/null +++ b/2019/src/day2.f90 @@ -0,0 +1,44 @@ +program day2 + implicit none + integer, parameter :: path_length = 100 + character(path_length) :: fname + integer :: n_arguments + integer, allocatable :: intcode(:) + integer :: n_codes + + n_arguments = command_argument_count() + if (n_arguments .eq. 1) then + call get_command_argument(1, fname) + print *, "File: ", fname + else + print *, "Wrong number of arguments: ", n_arguments + stop + end if + + intcode = read_intcode(fname) + n_codes = size(intcode) + print "(4i5)", intcode + print *, n_codes + deallocate(intcode) + + contains + function read_intcode(fname) + integer, parameter :: fstr_len = 10000 + character(*), intent(in) :: fname + integer, allocatable :: read_intcode(:) + character(fstr_len) :: fstr + integer :: fstr_true_len + integer :: n_values = 0 + integer :: i + open(10, file=fname) + read(10, "(A)") fstr + close(10) + fstr_true_len = len_trim(fstr) + do i=1, fstr_true_len + if (fstr(i:i) .eq. ',') n_values = n_values + 1 + end do + allocate(read_intcode(0:n_values)) + print *, n_values + read(fstr, *) read_intcode + end function read_intcode +end program day2