Skip to main content
deleted 1 character in body
Source Link
kops
  • 1.3k
  • 4
  • 7

Haskell, 60 bytes

f q|(a,~(_:t))<-span(>24)q=sum[1|x<-a,x>29,take 4a<a]>2||f t

Try it online!

This combines some ideas from previous Haskell answers in a new form along with exit by error code to edgecome out ahead.

How?

(a,~(_:t))<-span(>24)q puts the prefix of the list which is entirely >24 into a, and then tries to match the remainder of the list to the pattern h:t (dropping one value <= 24 and putting the remaining suffix into t). This pattern match might fail even on truthy inputs, if the heat wave is at the end of the list, which is where the ~ comes in: it forces Haskell to match the rest of the pattern even if _:t can't be matched (andan "irrefutable pattern"), in which case t is undefined (i.e. set to _|_) and will error out if we ever try to use it.

Since Haskell has the usual short circuiting on ||, we will only try to evaluate f t on undefined t if there was no heat wave in the list, in which case we exit high. If there was a heat wave, we'll find it and short circuit out, returning True and exiting low.

The irrefutable pattern saves one byte compared to tail, which will similarly fail if called on an empty list:

Haskell, 61 bytes

f q|(a,b)<-span(>24)q=sum[1|x<-a,x>29,take 4a<a]>2||f(tail b)

Try it online!

Haskell, 60 bytes

f q|(a,~(_:t))<-span(>24)q=sum[1|x<-a,x>29,take 4a<a]>2||f t

Try it online!

This combines some ideas from previous Haskell answers in a new form along with exit by error code to edge out ahead.

How?

(a,~(_:t))<-span(>24)q puts the prefix of the list which is entirely >24 into a, and then tries to match the remainder of the list to the pattern h:t (dropping one value <= 24 and putting the remaining suffix into t). This pattern match might fail even on truthy inputs, if the heat wave is at the end of the list, which is where the ~ comes in: it forces Haskell to match the rest of the pattern even if _:t can't be matched (and "irrefutable pattern"), in which case t is undefined (i.e. set to _|_) and will error out if we ever try to use it.

Since Haskell has the usual short circuiting on ||, we will only try to evaluate f t on undefined t if there was no heat wave in the list, in which case we exit high. If there was a heat wave, we'll find it and short circuit out, returning True and exiting low.

The irrefutable pattern saves one byte compared to tail, which will similarly fail if called on an empty list:

Haskell, 61 bytes

f q|(a,b)<-span(>24)q=sum[1|x<-a,x>29,take 4a<a]>2||f(tail b)

Try it online!

Haskell, 60 bytes

f q|(a,~(_:t))<-span(>24)q=sum[1|x<-a,x>29,take 4a<a]>2||f t

Try it online!

This combines some ideas from previous Haskell answers in a new form along with exit by error code to come out ahead.

How?

(a,~(_:t))<-span(>24)q puts the prefix of the list which is entirely >24 into a, and then tries to match the remainder of the list to the pattern h:t (dropping one value <= 24 and putting the remaining suffix into t). This pattern match might fail even on truthy inputs, if the heat wave is at the end of the list, which is where the ~ comes in: it forces Haskell to match the rest of the pattern even if _:t can't be matched (an "irrefutable pattern"), in which case t is undefined (i.e. set to _|_) and will error out if we ever try to use it.

Since Haskell has the usual short circuiting on ||, we will only try to evaluate f t on undefined t if there was no heat wave in the list, in which case we exit high. If there was a heat wave, we'll find it and short circuit out, returning True and exiting low.

The irrefutable pattern saves one byte compared to tail, which will similarly fail if called on an empty list:

Haskell, 61 bytes

f q|(a,b)<-span(>24)q=sum[1|x<-a,x>29,take 4a<a]>2||f(tail b)

Try it online!

Source Link
kops
  • 1.3k
  • 4
  • 7

Haskell, 60 bytes

f q|(a,~(_:t))<-span(>24)q=sum[1|x<-a,x>29,take 4a<a]>2||f t

Try it online!

This combines some ideas from previous Haskell answers in a new form along with exit by error code to edge out ahead.

How?

(a,~(_:t))<-span(>24)q puts the prefix of the list which is entirely >24 into a, and then tries to match the remainder of the list to the pattern h:t (dropping one value <= 24 and putting the remaining suffix into t). This pattern match might fail even on truthy inputs, if the heat wave is at the end of the list, which is where the ~ comes in: it forces Haskell to match the rest of the pattern even if _:t can't be matched (and "irrefutable pattern"), in which case t is undefined (i.e. set to _|_) and will error out if we ever try to use it.

Since Haskell has the usual short circuiting on ||, we will only try to evaluate f t on undefined t if there was no heat wave in the list, in which case we exit high. If there was a heat wave, we'll find it and short circuit out, returning True and exiting low.

The irrefutable pattern saves one byte compared to tail, which will similarly fail if called on an empty list:

Haskell, 61 bytes

f q|(a,b)<-span(>24)q=sum[1|x<-a,x>29,take 4a<a]>2||f(tail b)

Try it online!