diff --git a/2019/Makefile b/2019/Makefile index 99c610c..b7fedae 100644 --- a/2019/Makefile +++ b/2019/Makefile @@ -2,7 +2,7 @@ FC:=gfortran FFLAGS:=-Wall -Wno-maybe-uninitialized BIN:=./bin SRC:=./src -BINS:=./bin/test.bin ./bin/day01.bin ./bin/day02.bin ./bin/day02b.bin +BINS:=./bin/test.bin ./bin/day01.bin ./bin/day02.bin ./bin/day02b.bin ./bin/day03.bin all: aoc19 diff --git a/2019/data/day03a.txt b/2019/data/day03a.txt new file mode 100644 index 0000000..31fb84e --- /dev/null +++ b/2019/data/day03a.txt @@ -0,0 +1,3 @@ +R999,U626,R854,D200,R696,D464,R54,D246,L359,U57,R994,D813,L889,U238,L165,U970,L773,D904,L693,U512,R126,D421,R732,D441,R453,D349,R874,D931,R103,D794,R934,U326,L433,D593,L984,U376,R947,U479,R533,U418,R117,D395,L553,D647,R931,D665,L176,U591,L346,D199,L855,D324,L474,U251,R492,D567,L97,D936,L683,U192,R198,U706,L339,U66,R726,D102,R274,U351,R653,D602,L695,U921,R890,D654,R981,U351,R15,U672,R856,D319,R102,D234,R248,U169,L863,U375,L412,U75,L511,U298,L303,U448,R445,U638,L351,D312,R768,D303,L999,D409,L746,U266,L16,U415,L951,D763,L976,U342,L505,U770,L228,D396,L992,U3,R243,D794,L496,U611,R587,U772,L306,D119,L470,D490,L336,U518,L734,D654,L150,U581,L874,U691,L243,U94,L9,D582,L402,U563,R468,U96,L311,D10,R232,U762,R630,D1,L674,U685,R240,D907,R394,U703,L64,U397,L810,D272,L996,D954,R797,U789,R790,D526,R103,D367,R143,D41,L539,D735,R51,D172,L33,U241,R814,D981,R748,D699,L716,U647,L381,D351,L381,D121,L52,U601,R515,U713,L404,U45,R362,U670,L235,U102,R373,U966,L362,U218,R280,U951,R371,U378,L10,U670,R958,D423,L740,U888,R235,U899,L387,U167,R392,D19,L330,D916,R766,D471,L708,D83,R749,D696,L50,D159,R828,U479,L980,D613,L182,D875,L307,U472,L317,U999,R435,D364,R737,U550,L233,U190,L501,U610,R433,U470,L801,U52,L393,D596,L378,U220,L967,D807,R357,D179,L731,D54,L804,D865,L994,D151,L181,U239,R794,D378,L487,U408,R817,U809,R678,D599,L564,U480,R525,D189,L641,D771,L514,U72,L248,D334,L859,D318,R590,D571,R453,U732,R911,U632,R992,D80,R490,D234,L710,U816,L585,U180,L399,D238,L103,U605,R993,D539,R330 +L996,U383,L962,U100,L836,D913,R621,U739,R976,D397,L262,D151,L12,U341,R970,U123,L713,U730,L52,D223,L190,D81,R484,D777,R374,U755,R640,D522,R603,D815,R647,U279,R810,U942,R314,D19,L938,U335,R890,U578,R273,U338,R186,D271,L230,U90,R512,U672,R666,D328,L970,U17,R368,D302,L678,D508,L481,U12,L783,D409,L315,D579,L517,D729,R961,D602,R253,D746,R418,D972,R195,D270,L46,D128,L124,U875,R632,D788,L576,U695,R159,U704,R599,D597,R28,D703,L18,D879,L417,U633,L56,U302,R289,U916,R820,D55,R213,U712,R250,D265,L935,D171,L680,U738,L361,D939,R547,D606,L255,U880,R968,U255,R902,D624,L251,U452,L412,D60,L996,D140,L971,U196,R796,D761,L54,U54,L98,D758,L521,U578,L861,U365,L901,D495,L234,D124,L121,D329,L38,U481,L491,D938,L840,D311,L993,D954,R654,U925,L528,D891,L994,D681,L879,D476,L933,U515,L292,U626,R348,D963,L145,U230,L114,D11,R651,D929,R318,D672,R125,D827,L590,U338,L755,D925,L577,D52,R131,D465,R657,D288,R22,D363,R162,D545,L904,D457,R987,D389,L566,D931,L773,D53,R162,U271,L475,U666,L594,U733,R279,D847,R359,U320,R450,D704,L698,D173,R35,D267,L165,D66,L301,U879,R862,U991,R613,D489,L326,D393,R915,U718,R667,U998,R554,U199,R300,U693,R753,U938,R444,U12,L844,D912,R297,D668,R366,U710,L821,U384,R609,D493,R233,U898,R407,U683,R122,U790,L1,U834,L76,U572,R220,U752,L728,D85,L306,D805,R282,U507,R414,D687,L577,U174,L211,U308,L15,U483,R741,D828,L588,D192,L409,D605,L931,U260,L239,D424,L846,U429,L632,U122,L266,D544,R248,U188,R465,U721,R621,U3,L884,U361,L322,U504,R999,U381,R327,U555,L467,D849,R748,U175,R356 + diff --git a/2019/src/day03.f90 b/2019/src/day03.f90 new file mode 100644 index 0000000..fd81e30 --- /dev/null +++ b/2019/src/day03.f90 @@ -0,0 +1,108 @@ +program day3 + implicit none + integer, parameter :: path_length = 100 + integer, parameter :: n_data_lines = 2 + integer, parameter :: max_chars = 2000 + character(path_length) :: fname + integer :: n_arguments + character(max_chars), allocatable :: data_strs(:) + !integer :: i + + 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 + data_strs = read_instruction_strings(fname) + print *, data_strs + print *, data_strs(1) + print *, data_strs(1)(5:) + + deallocate(data_strs) + + contains + function read_instruction_strings(fname) result(strs) + implicit none + character(path_length) :: fname + character(max_chars), allocatable :: strs(:) + integer :: i_read + + allocate(strs(n_data_lines)) + open(10, file=fname) + do i_read=1,n_data_lines + read(10, "(A)") strs(i_read) + end do + close(10) + end function read_instruction_strings + + function all_occupied_points(istring) result(occupied) + implicit none + integer, parameter :: imax = 6 + character(max_chars), allocatable :: istring + integer :: n_cmds = 1 + integer :: i_chars, i1, i2, i_s + integer :: t_strlen + integer :: t_mvmnt, s_mvmnt + integer :: d_x, d_y, c_x, c_y + character(imax), allocatable :: tcmds(:) + character(1), allocatable :: dirs(:) + integer, allocatable :: mvmnts(:) + logical, allocatable :: occupied(:,:) + + t_strlen = len_trim(istring) + do i_chars=1,t_strlen + if (istring(i_chars:i_chars) .eq. ',') then + n_cmds = n_cmds + 1 + end if + end do + + allocate(tcmds(1:n_cmds)) + allocate(dirs(1:n_cmds)) + allocate(mvmnts(1:n_cmds)) + + read(istring, *) tcmds + + s_mvmnt = 0 + do i1 = 1,n_cmds + dirs(i1) = tcmds(i1)(1:1) + read(tcmds(i1)(2:), *) t_mvmnt + s_mvmnt = s_mvmnt + t_mvmnt + mvmnts(i1) = t_mvmnt + end do + allocate(occupied(1:2, 1:s_mvmnt)) + + c_x = 0 + c_y = 0 + i_s = 1 + do i1=1,n_cmds + if (dirs(i1) .eq. 'U') then + d_y = 1 + d_x = 0 + else if (dirs(i1) .eq. 'D') then + d_y = -1 + d_x = 0 + else if (dirs(i1) .eq. 'L') then + d_y = 0 + d_x = -1 + else + d_y = 0 + d_x = 1 + end if + t_mvmnt = mvmnts(i1) + do i2 = i, t_mvmnt + c_x = c_x + d_x + c_y = c_y + d_y + occupied(1, i_s) = c_x + occupied(2, i_s) = c_y + i_s = i_s + 1 + end do + end do + deallocate(tcmds) + deallocate(dirs) + deallocate(mvmnts) + + end function all_occupied_points +end program day3