-
Notifications
You must be signed in to change notification settings - Fork 1
/
AOC18B.f90
180 lines (158 loc) · 4.82 KB
/
AOC18B.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
program AOC18_2
implicit none
integer, parameter :: I8 = selected_int_kind(15)
character(*), parameter :: inpfile = 'input.txt'
type sn_t
type(sn_t), pointer :: left => null()
type(sn_t), pointer :: right => null()
type(sn_t), pointer :: up => null()
integer(I8) :: val = 0, depth = 0, curnum = 0
end type sn_t
type(sn_t), pointer :: curval, newval
type nums_t
type(sn_t), pointer :: num
end type nums_t
type(nums_t) :: nums(500)
integer :: curnum, lastnum
character(100) :: line
integer :: curpos, ios
logical :: action_taken
character(60) :: inputs(100)
integer :: i,j,maxmag,newmag
open (unit=1,file=inpfile,form='formatted',status='old')
do i=1,100
read (1,'(A)') inputs(i)
end do
close(1)
maxmag = 0
do i=1,100
do j=1,100
if (i == j) cycle
line = inputs(i)
curpos = 0
curval => read_nums()
line = inputs(j)
curpos = 0
newval => read_nums()
curval => add(curval,newval)
action_taken = .true.
do while (action_taken)
action_taken = .false.
lastnum = 0
call traverse(curval,1)
call explode(curval)
if (action_taken) cycle
call split(curval)
end do
newmag = magnitude(curval)
maxmag = max(maxmag,newmag)
end do
end do
print *, maxmag
contains
recursive function read_nums() result(res)
type(sn_t), pointer :: res
allocate (res)
curpos = curpos + 1
select case (line(curpos:curpos))
case ('[')
res%left => read_nums()
curpos = curpos + 1
res%right => read_nums()
curpos = curpos + 1
res%left%up => res
res%right%up => res
case ('0':'9')
res%val = ichar(line(curpos:curpos)) - ichar('0')
case default
error stop
end select
return
end function read_nums
function add (left,right) result (res)
type(sn_t), pointer :: res
type(sn_t), pointer, intent(in) :: left, right
allocate (res)
res%left => left
res%right => right
end function add
recursive subroutine traverse (val,depth)
type(sn_t), pointer, intent(inout) :: val
integer, intent(in) :: depth
val%depth = depth
if (.not. associated(val%left)) then
lastnum = lastnum + 1
nums(lastnum)%num => val
val%curnum = lastnum
else
call traverse(val%left,depth+1)
call traverse(val%right,depth+1)
end if
end subroutine traverse
recursive subroutine explode (pair)
type(sn_t), pointer, intent(in) :: pair
type(sn_t), pointer :: left,right
integer :: pos
left => pair%left
right => pair%right
if (.not. associated(left)) return
if ((pair%depth > 4) .and. (.not. associated(left%left)) .and. (.not. associated(right%right))) then
! Explode this pair
pos = left%curnum - 1
if (pos > 0) then
nums(pos)%num%val = nums(pos)%num%val + left%val
end if
pos = right%curnum + 1
if (pos <= lastnum) then
nums(pos)%num%val = nums(pos)%num%val + right%val
end if
deallocate (pair%left)
deallocate (pair%right)
pair%val = 0
action_taken = .true.
else
if (.not. action_taken) call explode(left)
if (.not. action_taken) call explode(right)
end if
end subroutine explode
subroutine split (cv)
type(sn_t), pointer, intent(inout) :: cv
type(sn_t), pointer :: sn
integer :: i
lastnum = 0
call traverse(cv,1)
do i=1,lastnum
sn => nums(i)%num
if (sn%val >= 10) then
allocate (sn%left)
allocate (sn%right)
sn%left%val = sn%val/2
sn%right%val = ceiling(real(sn%val)/2.0)
sn%val = 0
action_taken = .true.
return
end if
end do
end subroutine split
recursive function magnitude (pair)
integer :: magnitude
type (sn_t), pointer, intent(in) :: pair
if (.not. associated(pair%left)) then
magnitude = pair%val
else
magnitude = (3*magnitude(pair%left)) + (2*magnitude(pair%right))
end if
end function magnitude
recursive subroutine printval (val)
type(sn_t), intent(in) :: val
if (.not. associated(val%left)) then
write (*,'(I0)',advance='no') val%val
else
write (*,'(A)',advance='no') '['
call printval(val%left)
write (*,'(A)',advance='no') ','
call printval(val%right)
write (*,'(A)',advance='no') ']'
end if
end subroutine printval
end program AOC18_2