-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
124c8d0
commit 7a39802
Showing
4 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
program problem_08 | ||
|
||
use aoc_utilities | ||
|
||
implicit none | ||
|
||
integer :: i,j,nrows,ncols,k,n,idel,jdel | ||
character(len=1),dimension(:,:),allocatable :: array | ||
logical,dimension(:,:),allocatable :: antinodes, antinodes2 | ||
character(len=1),dimension(:),allocatable :: unique_antennas | ||
integer,dimension(:),allocatable :: iant, jant | ||
logical :: found1, found2 | ||
|
||
call clk%tic() | ||
|
||
! array = read_file_to_char_array('inputs/day8_test.txt') | ||
array = read_file_to_char_array('inputs/day8.txt') | ||
nrows = size(array,1) | ||
ncols = size(array,2) | ||
allocate(antinodes(nrows, ncols)); antinodes = .false. | ||
antinodes2 = antinodes ! for part 2 | ||
|
||
! identify all the unique antennas | ||
allocate(unique_antennas(0)) | ||
do i = 1, nrows | ||
do j = 1, ncols | ||
if (array(i,j)/='.' .and. .not. any(unique_antennas==array(i,j))) & | ||
unique_antennas = [unique_antennas, array(i,j)] | ||
end do | ||
end do | ||
|
||
! for all permutations of any pair | ||
! check for antinodes and accumulate them | ||
do i = 1, size(unique_antennas) | ||
! get the indices of all of these: | ||
call get_antenna_indices(unique_antennas(i), iant, jant) | ||
! 123456789 | ||
! ...A.A... | ||
! .#.A.A.#. | ||
do j = 1, size(iant) | ||
do k = 1, size(jant) | ||
if (j==k) cycle | ||
! offset from one to the other: | ||
idel = iant(j) - iant(k) | ||
jdel = jant(j) - jant(k) | ||
n = 0 ! for part 2, use a loop (part 1 is just the n=1 case) | ||
found1 = .false.; found2 = .false. | ||
do | ||
! does this pair have antinodes within the array bounds? | ||
found1 = (in_bounds(iant(j)+n*idel, jant(j)+n*jdel)) | ||
if (found1) then | ||
if (n==1) antinodes( iant(j)+n*idel, jant(j)+n*jdel) = .true. | ||
antinodes2(iant(j)+n*idel, jant(j)+n*jdel) = .true. | ||
end if | ||
found2 = (in_bounds(iant(k)-n*idel, jant(k)-n*jdel)) | ||
if (found2) then | ||
if (n==1) antinodes( iant(k)-n*idel, jant(k)-n*jdel) = .true. | ||
antinodes2(iant(k)-n*idel, jant(k)-n*jdel) = .true. | ||
end if | ||
if (.not. found1 .and. .not. found2) exit ! all have been found | ||
n = n + 1 | ||
end do | ||
end do | ||
end do | ||
|
||
end do | ||
|
||
write(*,*) '8a:', count(antinodes) | ||
write(*,*) '8b:', count(antinodes2) | ||
|
||
call clk%toc('8') | ||
|
||
contains | ||
|
||
pure logical function in_bounds(i,j) | ||
!! is this point in the array bounds? | ||
integer,intent(in) :: i,j | ||
in_bounds = (i>=1 .and. i<=nrows .and. j>=1 .and. j<=ncols) | ||
end function in_bounds | ||
|
||
subroutine get_antenna_indices(a, iant, jant) | ||
!! get the indices of the specified character (antenna) | ||
! can we use findloc or pack or something for this? | ||
character(len=1),intent(in) :: a !! antenna character | ||
integer,dimension(:),allocatable,intent(out) :: iant, jant !! i,j locations of c in the array | ||
integer :: i, j | ||
allocate(iant(0), jant(0)) | ||
do i = 1, nrows | ||
do j = 1, ncols | ||
if (array(i,j)==a) then | ||
iant = [iant, i] | ||
jant = [jant, j] | ||
end if | ||
end do | ||
end do | ||
end subroutine get_antenna_indices | ||
|
||
end program problem_08 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
.....................................O..V......... | ||
.................................................. | ||
................................O.........Z....... | ||
....W....................................V....v... | ||
........................m................8........ | ||
.....................................n........Z..v | ||
.............F.....3...n....5m.................... | ||
................................................V. | ||
................3............iv....Z.............V | ||
...........................O..n..i........p......H | ||
......W..6..............................i......... | ||
......................................b........... | ||
..................................n........p...... | ||
........M.......c...........m..5......1........... | ||
...M............................L..5..A........... | ||
...w...........9.............F5..................q | ||
.W.....................................q....p..... | ||
.......W........r.......H.....LA......q........... | ||
................4.F....................A.......... | ||
........3.......a.....F...................A..L.... | ||
....ME...............................Q..........q. | ||
.E..................ih...................Z........ | ||
................E...H...........h................. | ||
.........m.........X.............................. | ||
..................0......C.................h...... | ||
.M......l.................Q.h..................... | ||
..........C..............0........................ | ||
.............lX............3.c.................... | ||
......8.X.........c....r..a......H.....9.......... | ||
.................QE.....C......................... | ||
..R................a........Q...................7. | ||
...........................a...................... | ||
l..........X.R............1..I..........9......... | ||
.................0R..............b.....z......x... | ||
.......l.....w....r..........................b.... | ||
.8..........0...................P1z............... | ||
.............c.........................L.......... | ||
.................C..N............o............9... | ||
...........e..f..N................................ | ||
8.............................B................... | ||
...........4...............................x...... | ||
....w....RY..........4.......................P.... | ||
.........yw.....Y.............o2...............7.. | ||
..6y........4..............fo..............7...... | ||
.........Y..6............o......................x. | ||
.....Y....e.....y..I.r...........2................ | ||
....e.............................P.......z.bB.... | ||
.............6.................B........7......x.. | ||
..y.N........f...........1....I....z....B......... | ||
.....e....f.............I.................2....... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
............ | ||
........0... | ||
.....0...... | ||
.......0.... | ||
....0....... | ||
......A..... | ||
............ | ||
............ | ||
........A... | ||
.........A.. | ||
............ | ||
............ |