Skip to content

Commit

Permalink
day8
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwilliams committed Dec 8, 2024
1 parent 124c8d0 commit 7a39802
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Problem | Stars | Solution | Runtime
[5](https://adventofcode.com/2024/day/5) | ⭐⭐ | [problem_05.f90](https://github.com/jacobwilliams/AoC-2024/blob/master/app/problem_05.f90) | 9 ms
[6](https://adventofcode.com/2024/day/6) | ⭐⭐ | [problem_06.f90](https://github.com/jacobwilliams/AoC-2024/blob/master/app/problem_06.f90) | 33 ms
[7](https://adventofcode.com/2024/day/7) | ⭐⭐ | [problem_07.f90](https://github.com/jacobwilliams/AoC-2024/blob/master/app/problem_07.f90) | 242 ms
[8](https://adventofcode.com/2024/day/8) | ☆☆ | [problem_08.f90](https://github.com/jacobwilliams/AoC-2024/blob/master/app/problem_08.f90) |
[8](https://adventofcode.com/2024/day/8) | ⭐⭐ | [problem_08.f90](https://github.com/jacobwilliams/AoC-2024/blob/master/app/problem_08.f90) | <1 ms
[9](https://adventofcode.com/2024/day/9) | ☆☆ | [problem_09.f90](https://github.com/jacobwilliams/AoC-2024/blob/master/app/problem_09.f90) |
[10](https://adventofcode.com/2024/day/10)| ☆☆ | [problem_10.f90](https://github.com/jacobwilliams/AoC-2024/blob/master/app/problem_10.f90) |
[11](https://adventofcode.com/2024/day/11)| ☆☆ | [problem_11.f90](https://github.com/jacobwilliams/AoC-2024/blob/master/app/problem_11.f90) |
Expand Down
98 changes: 98 additions & 0 deletions app/problem_08.f90
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
50 changes: 50 additions & 0 deletions inputs/day8.txt
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.......
12 changes: 12 additions & 0 deletions inputs/day8_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

0 comments on commit 7a39802

Please sign in to comment.